在Python中使用OpenCV裁剪图像
What is Cropping?
什么是播種?
Cropping is the removal of unwanted outer areas from a photographic or illustrated image. The process usually consists of the removal of some of the peripheral areas of an image to remove extraneous trash from the picture, to improve its framing, to change the aspect ratio, or to accentuate or isolate the subject matter from its background.
裁剪是從攝影或插圖圖像中去除不需要的外部區域。 該過程通常包括去除圖像的某些外圍區域,以從圖片中去除多余的垃圾,以改善其取景,改變縱橫比,或者使主題與背景突出或隔離。
We will be using these functions of OpenCV - python(cv2),
我們將使用OpenCV的這些功能-python(cv2),
imread(): This function is like it takes an absolute path of the file and reads the whole image, and after reading the whole image it returns us the image and we will store that image in a variable.
imread() :此函數就像它采用文件的絕對路徑并讀取整個圖像一樣,在讀取整個圖像之后,它將返回圖像并將圖像存儲在變量中。
imshow(): This function will be displaying a window (with a specified window name) which contains the image that is read by the imread() function.
imshow() :此函數將顯示一個窗口(具有指定的窗口名稱),該窗口包含由imread()函數讀取的圖像。
shape: This function will return the height, width, and layer of the image
形狀 :此函數將返回圖像的高度 , 寬度和層
Let’s take an example,
讓我們舉個例子
Let there be a list a=[1,2,3,4,5,6,7,8,9]Now, here I just wanted the elements between 4 and 8 (including 4 and 8) so what we will do is :print(a[3:8]) The result will be like : [4,5,6,7,8]裁剪圖像的Python程序 (Python program to crop an image)
# importing the module import cv2img=cv2.imread("/home/abhinav/PycharmProjects/untitled1/a.jpg")# Reading the image with the help of # (specified the absolute path) # imread() function and storing it in the variable img cv2.imshow("Original Image",img)# Displaying the Original Image Window # named original image # with the help of imshow() function height,width=img.shape[:2]# storing height and width with the help # of shape function as shape return us # three things(height,width,layer) in the form of list # but we wanted only height and width start_row,start_col=int(width*0.25),int(height*0.25) end_row,end_col=int(width*0.75),int(height*0.75)# start_row and start_col are the cordinates # from where we will start cropping # end_row and end_col is the end coordinates # where we stop cropped=img[start_row:end_row,start_col:end_col]# using the idexing method cropping # the image in this way cv2.imshow("Cropped_Image",cropped) # using the imshow() function displaying # another window of # the cropped picture as cropped contains # the part of image cv2.waitKey(0) cv2.destroyAllWindows()Output:
輸出:
You can see the Cropped Image and the Original Image(the cropping is done like 0.25 to 0.75 with row and with column 0.25 to 0.75, and you can change the number).
您可以看到“裁剪的圖像”和“原始圖像”(裁剪的過程類似于0.25到0.75行和0.25到0.75列,并且可以更改數字)。
翻譯自: https://www.includehelp.com/python/cropping-an-image-using-opencv.aspx
總結
以上是生活随笔為你收集整理的在Python中使用OpenCV裁剪图像的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c#格式化字float_C#中的floa
- 下一篇: python饼形图_Python | 饼