Updated April 18, 2023
Introduction to OpenCV crop image
The following article provides an outline for OpenCV crop image. It is an inbuilt function which is present in the OpenCV Python language library. There are several functions like crop image() that are present in the Python image library or commonly known as the PIL. The library is responsible for adding and in the editing and formatting of the images reading the formatting features to the output platform through Python interpreter. It has several in-build functions that are designed to manually perform analysis of the have been provided by the user. It serves the purpose of identifying and cropping individual images in order to save each of the image present image file which has been provided by the coder save them as separate objects different folder.
Syntax of OpenCV crop image()
Following is the syntax which aids in the usage of the OpenCV crop image function:
image[start_x:end_x, start_y:end_y]
(Using the Open CV Library)
Image.crop(left, top, right, bottom)
(Using the PIL Library)
Parameters:
Following are the parameters that are used while making use of the OpenCV crop image function, which enable the cropping of the source image to be accurately executed:
Parameters | Description of the parameter |
Source Image | The parameter represents the original image that has to be processed and further to be cropped. |
start_x: end_x | The parameter represents the start and end coordinates in the upper end of the image where the image needs to be cropped. |
start_y: end_y | The parameter represents the start and end coordinates in the lower end of the image where the image needs to be cropped. |
Top | The parameter left represents the top coordinates that are present in the image where the image needs to be cropped. (i.e., (a, b) = (left, top). |
Left | The parameter left represents the left coordinates that are present in the image where the image needs to be cropped. (i.e., (a, b) = (left, top). |
Bottom | The parameter left represents the bottom coordinates that are present in the image where the image needs to be cropped. (i.e., (c, d) = (right, bottom). |
Right | The parameter left represents the right coordinates that are present in the image where the image needs to be cropped. (i.e., (c, d) = (right, bottom). |
Return Value | It output image which has been processed after the correct coordinates are user for cropping the image after the function is executed upon the original image. |
How does it Work?
- The image which has been provided by the user actually is sliced to form various sets of arrays through the process of passing the start index and end index with respect to the x & y coordinates.
- The image has the coordinates in the form of pixels represented to find the final output to give the cropped image.
Examples
Given below are the examples mentioned:
The following examples demonstrate the utilization of the OpenCV crop image function:
Example #1
Code:
# importing the class library cv2 in order perform the usage of crop image()
import cv2
# defining the variable which read the image path for the image to be
Processed
img_1 = Image.open(r’C:\Users\data\Desktop\educba logo1.png’;
# defining the coordinated for the four corners represented y,x,h and w which are used to crop the source image appropriately
y1=0
x1=0
h1=3000
w1=5100
cropimage_1 = image[x1:w1, y1:h1]
# Displaying the output image which has been cropped as per the provided coordinates
cv2.imshow("Cropped", crop_image)
cv2.waitKey(0)
Output:
The final output of the above image where the image has been roped using the OpenCV crop image function is:
Original Image:
Cropped Image:
Example #2
A program written in Python coding language aimed at explaining the OpenCV crop image() in built method.
Code:
# importing the class library PIL in order perform the usage of crop the
image from PIL import Image
# defining the variable which read the image path for the image to be
Processed
img_1 = Image.open(r’C:\Users\data\Desktop\edu cba logo2.png’;
# defining the coordinated for the four corners left, top, right and bottom which are used to crop the source image appropriately
left_1 = 0
top_1 = 500
right_1 = 5100
bottom_1 = 2920
imgres_1 = img.crop((left_1, top_1, right_1, bottom_1))
# Displaying the output image which has been cropped as per the provided coordinates
imgres_1.show()
Output:
The final output of the above image where the image has been roped using the OpenCV crop image function, is:
Original Image:
Cropped Image:
Conclusion
It is a very important inbuilt function which is present in the Python library. Being an inbuilt function, it reduces the velocity of the code which is written is generally utilised for cutting off images. This function is preferable uses in image detection jobs that involve image processing. One of the primary parameters which are needed for application of cropping for each of the individual images the source image which has provided by the contours which would be required for the specific. The OpenCV image crop function helps in reducing the overall dimension of the provided Numpy array which is presented as a representation of the pixels present in the image that has been sourced by the coder.
Recommended Articles
This is a guide to OpenCV crop image. Here we discuss the introduction, how does OpenCV crop image works? and examples respectively. You may also have a look at the following articles to learn more –