How To Draw A Rect On Mouse In Python
Being raised on the east coast of the United States I guess I should exist used to the snow by at present — merely I'm not. As a child, I loved the snow. I loved sledding, snowboarding, and snowball fights.
But now, every bit an adult, snow just means an inconvenience. It ways travel is going to be terrible. It ways not being able to drive to the gym. And it means beingness stuck in the apartment.
That said, I'g taking this snow twenty-four hours on the east declension and writing a web log post on capturing mouse click events with Python and OpenCV.
In this example we'll click and drag a rectangular Region of Interest (ROI) and crop it from our paradigm. This technique is especially helpful if yous are labeling data as input to an image nomenclature algorithm.
And then if you're trying to understand how mouse click events work with Python and OpenCV, and so look no further! This blog post volition evidence you exactly that!
Looking for the source lawmaking to this post?
Spring Right To The Downloads Section OpenCV and Python versions:
In club to run this case, you'll need Python ii.seven and OpenCV 2.4.X.
Capturing mouse click events with Python and OpenCV
Let's go ahead and go this case started. Open up a new file, name it click_and_crop.py , and nosotros'll get to work:
# import the necessary packages import argparse import cv2 # initialize the list of reference points and boolean indicating # whether cropping is being performed or not refPt = [] cropping = Faux def click_and_crop(effect, x, y, flags, param): # take hold of references to the global variables global refPt, cropping # if the left mouse button was clicked, record the starting # (ten, y) coordinates and point that cropping is being # performed if event == cv2.EVENT_LBUTTONDOWN: refPt = [(x, y)] cropping = Truthful # check to see if the left mouse button was released elif event == cv2.EVENT_LBUTTONUP: # record the ending (x, y) coordinates and indicate that # the cropping operation is finished refPt.append((10, y)) cropping = False # depict a rectangle effectually the region of interest cv2.rectangle(image, refPt[0], refPt[1], (0, 255, 0), 2) cv2.imshow("image", prototype) We'll showtime by importing our 2 necessary packages: argparse for parsing command line arguments and cv2 for our OpenCV bindings.
We also define 2 global variables on Lines 7 and eight: refPt , which is a list of 2 (ten, y)-coordinates specifying the rectangular region we are going to crop from our image, and cropping , a boolean indicating whether we are in cropping fashion or non.
To process the mouse click events we define the click_and_crop callback function on Line 10. Anytime a mouse event happens, OpenCV will relay the pertinent details to our click_and_crop function.
In lodge for our function to handle the relay, we need to accept 5 arguments:
- consequence: The event that took identify (left mouse push pressed, left mouse button released, mouse movement, etc).
- x: The x-coordinate of the outcome.
- y: The y-coordinate of the event.
- flags: Any relevant flags passed past OpenCV.
- params: Any extra parameters supplied by OpenCV.
From in that location we grab reference to our refPt list and cropping flag on Line 12.
We and so make a cheque on Line 17 to see if our left mouse button was pressed. If information technology was, nosotros record the (ten, y)-coordinates of the event and signal that nosotros are now in "cropping mode".
At this signal we would drag out the rectangular region of the image that nosotros want to crop. Later we are done dragging out the region, nosotros release the left mouse button — Line 22 handles when the left mouse push button is released and updates the listing of points containing our ROI. We also depict the rectangle representing the ROI on Lines 29 and thirty.
Now, let'due south see how nosotros tin can use this function to crop an image:
# construct the statement parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required=Truthful, help="Path to the image") args = vars(ap.parse_args()) # load the image, clone it, and setup the mouse callback office image = cv2.imread(args["prototype"]) clone = prototype.copy() cv2.namedWindow("prototype") cv2.setMouseCallback("prototype", click_and_crop) # go on looping until the 'q' key is pressed while True: # display the epitome and await for a keypress cv2.imshow("image", image) cardinal = cv2.waitKey(one) & 0xFF # if the 'r' key is pressed, reset the cropping region if key == ord("r"): image = clone.copy() # if the 'c' key is pressed, pause from the loop elif key == ord("c"): break # if in that location are two reference points, then ingather the region of interest # from teh image and display it if len(refPt) == ii: roi = clone[refPt[0][i]:refPt[i][1], refPt[0][0]:refPt[1][0]] cv2.imshow("ROI", roi) cv2.waitKey(0) # close all open windows cv2.destroyAllWindows() Nosotros start by parsing our control line arguments on Lines 33-35. Nosotros need but a unmarried switch here, --image , which is the path to the epitome we want to crop. This prototype is so loaded and cloned on Lines 38 and 39. We make a deep re-create of the prototype so nosotros tin can depict the rectangular bounding box on Line 29 without destroying the original image.
Lines xl and 41 handle registering our click_and_crop callback part. Nosotros first telephone call cv2.namedWindow to create a window named "image". And nosotros and so set the mouse callback by calling cv2.setMouseCallback , supplying our named "image" window and our click_and_crop callback part.
From here, the residual of this case just ties the pieces together.
Nosotros start a loop on Line 44 which (ane) displays our image on screen and (2) waits for a keypress.
This is the point where we select the rectangular region we want to ingather from the image.
If we printing the r cardinal subsequently selecting the region to crop, nosotros reset our cropping
And if nosotros press the c primal, and then we intermission from the loop and perform the actual cropping on Lines 59-62.
Finally, we cleanup and close all open up windows on Line 65.
OpenCV mouse events in action
At present that nosotros have our example coded up, permit's try it out. Open up up a last and execute the post-obit command:
$ python click_and_crop.py --paradigm jurassic_park_kitchen.jpg
You'll first be presented with the paradigm on your screen:
Select the region you want to crop by clicking, dragging, and releasing:
And finally press the c primal to perform the crop:
As you can see, we have successfully cropped Tim's face from the image.
Here is another case where we crop the Velociraptor:
So there you lot take information technology — a unproblematic method to capture mouse events to ingather an image using Python and OpenCV.
What'due south adjacent? I recommend PyImageSearch University.
Course data:
35+ total classes • 39h 44m video • Terminal updated: February 2022
★★★★★ 4.84 (128 Ratings) • iii,000+ Students Enrolled
I strongly believe that if you had the correct teacher y'all could master computer vision and deep learning.
Exercise y'all think learning computer vision and deep learning has to exist time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a caste in reckoner science?
That'southward not the case.
All yous demand to master reckoner vision and deep learning is for someone to explain things to you in simple, intuitive terms. And that'south exactly what I do. My mission is to alter instruction and how complex Bogus Intelligence topics are taught.
If you're serious about learning computer vision, your next stop should exist PyImageSearch University, the most comprehensive reckoner vision, deep learning, and OpenCV course online today. Here y'all'll larn how to successfully and confidently apply figurer vision to your work, inquiry, and projects. Join me in computer vision mastery.
Inside PyImageSearch University y'all'll find:
- ✓ 35+ courses on essential computer vision, deep learning, and OpenCV topics
- ✓ 35+ Certificates of Completion
- ✓ 39h 44m on-demand video
- ✓ Brand new courses released every month , ensuring you can go on upwardly with land-of-the-art techniques
- ✓ Pre-configured Jupyter Notebooks in Google Colab
- ✓ Run all code examples in your spider web browser — works on Windows, macOS, and Linux (no dev environs configuration required!)
- ✓ Admission to centralized code repos for all 500+ tutorials on PyImageSearch
- ✓ Easy one-click downloads for code, datasets, pre-trained models, etc.
- ✓ Admission on mobile, laptop, desktop, etc.
Click here to join PyImageSearch University
Summary
In this blog postal service we learned how to capture mouse events using OpenCV and Python. While this post did not provide a complete and exhaustive overview ofall the mouse events you can capture, it laid the background for what is possible.
This post too demonstrated how y'all can chop-chop crop and extract regions of an image, which is especially useful when creating preparation data for your own custom object detectors.
Download the Source Code and Costless 17-page Resource Guide
Enter your electronic mail accost beneath to get a .zip of the code and a FREE 17-page Resources Guide on Estimator Vision, OpenCV, and Deep Learning. Inside you'll find my manus-picked tutorials, books, courses, and libraries to aid you master CV and DL!
Source: https://pyimagesearch.com/2015/03/09/capturing-mouse-click-events-with-python-and-opencv/
Posted by: sandersundiers.blogspot.com

0 Response to "How To Draw A Rect On Mouse In Python"
Post a Comment