Tuesday, 2 January 2024

Crop the image in OpenCV

Images are internally represented as arrays of numbers. By doing array slicing, we can crop the specific portion of the image.

Example

cropped_image = image[20:300, 100: 350]

 

Find the below working application.

 

crop_image.py

 

import cv2 as cv

# Read the image as matrix of pixels
image = cv.imread('photos/tiger.png')

print('\nResizing image to (500, 500)')
image = cv.resize(image, (500, 500), interpolation=cv.INTER_AREA)

# Display the image in new window
cv.imshow('Tiger', image)

cropped_image = image[20:300, 100: 350]
# Display the image in new window
cv.imshow('cropped_image', cropped_image)

# Wait for Infinite amount of time for a keyboard key to be pressed
cv.waitKey(0)

# Close the OpenCV windows
cv.destroyAllWindows()

 

Output

Original image

 

 


Cropped image



 

Previous                                                    Next                                                    Home

No comments:

Post a Comment