调整灰度图像的大小,而无需在Python中使用任何内置函数
In this program, we will be using two functions of OpenCV-python (cv2) module. Let's see their syntax and descriptions first.
在此程序中,我們將使用OpenCV-python(cv2)模塊的兩個功能。 首先讓我們看看它們的語法和說明。
1) imread():
It takes an absolute path/relative path of your image file as an argument and returns its corresponding image matrix.
1)imread():
它以圖像文件的絕對路徑/相對路徑作為參數,并返回其對應的圖像矩陣。
2) imshow():
It takes window name and image matrix as an argument in order to display an image in a display window with a specified window name.
2)imshow():
它以窗口名稱和圖像矩陣為參數,以便在具有指定窗口名稱的顯示窗口中顯示圖像。
Also In this program, we will be using one attribute of an image matrix:
同樣在此程序中,我們將使用圖像矩陣的一個屬性:
shape: This is the attribute of an image matrix which return shape of an image i.e. consisting of number of rows ,columns and number of planes.
形狀:這是圖像矩陣的屬性,該屬性返回圖像的形狀,即由行數,列數和平面數組成。
In the case of a Grayscale image, only one plane is needed. If the number of planes is 1 then shape attribute only return number of rows and columns.
在灰度圖像的情況下,僅需要一個平面。 如果平面數為1,則shape屬性僅返回行數和列數。
Also, here we are using the concept of array slicing
另外,這里我們使用數組切片的概念
Let, A is 1-d array:
A[start:stop:step]
設A為一維數組:
A [開始:停止:步驟]
start: Starting number of the sequence.
start:序列的起始編號。
stop: Generate numbers up to, but not including this number.
停止:生成不超過此數字的數字,但不包括此數字。
step: Difference between each number in the sequence.
步驟:序列中每個數字之間的差。
Example:
例:
A = [1,2,3,4,5,6,7,8,9,10]print(A[ 1 : : 2])Output:[2, 4, 6, 9]Python程序,無需使用任何內置函數即可調整灰度圖像的大小 (Python program to resize a grayscale image without using any inbuilt functions )
# open-cv library is installed as cv2 in python # import cv2 library into this program import cv2# give value by which you want to resize an image # here we want to resize an image as one half of the original image x,y= 2,2# read an image using imread() function of cv2 # we have to pass only the path of the image img = cv2.imread(r'C:/Users/user/Desktop/pic2.jpg',0)# displaying the image using imshow() function of cv2 # In this : 1st argument is name of the frame # 2nd argument is the image matrix cv2.imshow('original image',img)# print shape of the image matrix # using shape attribute print("original image shape:",img.shape)# here we take alternate row,column pixel. # we take half pixel of rows and columns respectively # so that it is one half of image matrix. resize_img = img[::x,::y]cv2.imshow('resize image',resize_img)# print shape of the image matrix # using shape attribute print("resize image shape:",resize_img.shape)Output
輸出量
翻譯自: https://www.includehelp.com/python/resize-a-grayscale-image-without-using-any-inbuilt-functions.aspx
總結
以上是生活随笔為你收集整理的调整灰度图像的大小,而无需在Python中使用任何内置函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实战:隐藏SpringBoot中的私密数
- 下一篇: websocket python爬虫_p