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 cv2
img=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