This example will load an image and use the mouse to draw a rectangle, and then crop the image based on the rectangle as shown below.
When the mouse left button is clicked on the image, the first point is captured, normally it is the top-left of the rectangle however it depends on which direction the mouse is moving, we will resolve it later.
Then hold the left button and move the mouse, similar to previous sections a rectangle is drawn while the mouse is moving to indicate how the rectangle looks like, at the same time the coordinates of the first point and the mouse’s current location are shown, as Figure above.
When the left button is released, the second point is captured, and the final rectangle is drawn, at the same time a separate window is shown with cropped image.
The source code is similar to the previous examples of drawing circles and polygons. Now we only focus on the cropping image part of the source code. As mentioned earlier, when the left mouse button is down the first point is captured and recorded, and when it is released the second point is recorded.
To crop an image, an array operation will simply do the job.
cropped_image = image[y_start:y_end, x_start:x_end]
The image data is in image array, its first dimension is y coordinate and the second is x. y_start:y_end will select only rows between y_start and y_end, similarly x_start:x_end will select only columns between the two.
Depending on which direction the mouse is moving the first point might not always be the top-left, and the second point might not always be the bottom-right. We resolve this in the crop_image() function by comparing the x and y values of the first and second points, the smaller x and y will be the top-left point, the bigger ones will be the bottom right. The code snippets are show as below:
1 def crop_image(img, pt_first, pt_second):
2 # top-left point
x_tl, y_tl = pt_first
3 # bottom-right point
x_br, y_br = pt_second
4 # swap x value if opposite
if x_br < x_tl:
5 x_br, x_tl = x_tl, x_br
6 # swap y value if opposite
if y_br < y_tl:
7 y_br, y_tl = y_tl, y_br
8 cropped_image = img[y_tl:y_br, x_tl:x_br]
9 cv2.imshow("Cropped Image", cropped_image)
Execute the code in CropImageWithMouse.py file, you can crop the image with the mouse operations, looks something like Figure shown above.
0 comments:
Post a Comment