I have below code to crop a part of image:
import cv2
def on_mouse(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
print("X: {} | Y: {}".format(x, y))
win_name = "Image"
cv2.namedWindow(win_name)
cv2.setMouseCallback(win_name, on_mouse)
img = cv2.imread('park.jpg')
cropImg = img[179:470, 511:645]
cv2.imshow(win_name, img)
cv2.imshow("Crop", cropImg)
cv2.waitKey(0)
cv2.destroyAllWindows()
In the above code, you can see that I have defined a function call on_mouse which basically gives us the coordinates (x, y) wherever the mouse clicks on the image. This helps in getting the x1, y1 and x2, y2 coordinates of the area we want to crop. In below image, I am trying to crop the area of giraffe. So I clicked on top left corner near giraffe which gave me the coordinates as X: 470 | Y: 179 and then I clicked bottom right corner of the giraffe which gave me the coordinates of X: 645 | Y: 511. When using them in above code, it gives below output
Below is the original image
Can anyone please help me understand how can I crop it and what does these x1, y1 and x2, y2 denotes? Thanks


