Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Text Detection and Extraction using OpenCV and OCR
Next article icon
OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. In this article, we will try to draw on images with the help of the mouse. Before learning how to draw on images using a mouse, we need to understand what is a callback.<

Callback

A callback in programming means to call this function (the callback) when a process completes. The same applies to event-oriented programming in general. When a mouse button is clicked (an event), call a function. We don’t know when the button will be clicked. All we can do is tell the button to “call me back” or call this function when the mouse button is clicked.

Mouse Callbacks

A callback can happen when a user performs an operation using the mouse; this operation is usually known as an event. Only one callback is present for a mouse, which issetMouseCallback(), all mouse operation will call this function only.

We can have conditional blocks to execute something based on the event/operation performed using the mouse. The mouse events/operations could be:

  • EVENT_MOUSEMOVE
  • EVENT_LBUTTONDOWN
  • EVENT_RBUTTONDOWN
  • EVENT_LBUTTONUP
  • EVENT_RBUTTONUP

When should this callback occur :

We want to have this call back only when we use the mouse on the pop-up window, which has the title as"Title of Popup Window."
cv2.namedWindow("Title of Popup Window")
Example 1:Draw Circle when we left-click on a popup with OpenCV :Python3
importcv2img=cv2.imread("flower.jpg")defdraw_circle(event,x,y,flags,param):ifevent==cv2.EVENT_LBUTTONDOWN:print("hello")cv2.circle(img,(x,y),100,(0,255,0),-1)cv2.namedWindow(winname="Title of Popup Window")cv2.setMouseCallback("Title of Popup Window",draw_circle)whileTrue:cv2.imshow("Title of Popup Window",img)ifcv2.waitKey(10)&0xFF==27:breakcv2.destroyAllWindows()
Output:Example 2:Drawing a Rectangle by dragging on Images with OpenCVPython3
importcv2img=cv2.imread("flower.jpg")# variablesix=-1iy=-1drawing=Falsedefdraw_rectangle_with_drag(event,x,y,flags,param):globalix,iy,drawing,imgifevent==cv2.EVENT_LBUTTONDOWN:drawing=Trueix=xiy=yelifevent==cv2.EVENT_MOUSEMOVE:ifdrawing==True:cv2.rectangle(img,pt1=(ix,iy),pt2=(x,y),color=(0,255,255),thickness=-1)elifevent==cv2.EVENT_LBUTTONUP:drawing=Falsecv2.rectangle(img,pt1=(ix,iy),pt2=(x,y),color=(0,255,255),thickness=-1)cv2.namedWindow(winname="Title of Popup Window")cv2.setMouseCallback("Title of Popup Window",draw_rectangle_with_drag)whileTrue:cv2.imshow("Title of Popup Window",img)ifcv2.waitKey(10)==27:breakcv2.destroyAllWindows()
Output:

What does (cv2.waitKey(10) & 0xFF == 27) do ?

cv2.waitKey() returns a 32 Bit integer value (might be dependent on the platform). The key input is in ASCII which is an 8 Bit integer value. So you only care about these 8 bits and want all other bits to be 0. This you can achieve with:cv2.waitKey(10) & 0xFF == 27

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp