We start a loop on Line 44 which (1) displays our image on screen and (2) waits for a keypress. ![]() And we then set the mouse callback by calling cv2.setMouseCallback, supplying our named “image” window and our click_and_crop callback function.įrom here, the rest of this example just ties the pieces together. We first call cv2.namedWindow to create a window named “image”. Lines 40 and 41 handle registering our click_and_crop callback function. We make a deep copy of the image so we can draw the rectangular bounding box on Line 29 without destroying the original image. This image is then loaded and cloned on Lines 38 and 39. ![]() We need only a single switch here, -image, which is the path to the image we want to crop. We start by parsing our command line arguments on Lines 33-35. # if there are two reference points, then crop the region of interest # if the 'c' key is pressed, break from the loop # if the 'r' key is pressed, reset the cropping region # display the image and wait for a keypress # keep looping until the 'q' key is pressed # load the image, clone it, and setup the mouse callback functionĬv2.setMouseCallback("image", click_and_crop) Now, let’s see how we can use this function to crop an image: # construct the argument parser and parse the argumentsĪp.add_argument("-i", "-image", required=True, help="Path to the image") We also draw the rectangle representing the ROI on Lines 29 and 30. After we are done dragging out the region, we release the left mouse button - Line 22 handles when the left mouse button is released and updates the list of points containing our ROI. If it was, we record the (x, y)-coordinates of the event and indicate that we are now in “cropping mode”.Īt this point we would drag out the rectangular region of the image that we want to crop. We then make a check on Line 17 to see if our left mouse button was pressed.
0 Comments
Leave a Reply. |