- Python: 3.8.11
- Opencv – python: 4.1.2.30
- Opencv – contrib – python: 4.1.2.30
- OS: Ubuntu Kylin 20.04
- Conda: 4.10.1
- Pycharm: 2021.1.3
Code sample
import cv2
import numpy as np
def draw_circle(event, x, y, flags, param) :
global flag_mouse_lbutton_down, point_x, point_y, image
# Left mouse button down
if event == cv2.EVENT_LBUTTONDOWN:
print("EVENT_LBUTTONDOWN", event, x, y, flags, param)
flag_mouse_lbutton_down = True
point_x = x
point_y = y
cv2.circle(image, (x, y), 1, (0.0.255), thickness=2)
if event == cv2.EVENT_LBUTTONUP:
print("EVENT_LBUTTONUP", event, x, y, flags, param)
flag_mouse_lbutton_down = False
cv2.rectangle(image, (point_x, point_y), (x, y), (0.0.255), 2)
if event == cv2.EVENT_MOUSEMOVE and flags == cv2.EVENT_FLAG_LBUTTON:
print("EVENT_MOUSEMOVE+EVENT_FLAG_LBUTTON", event, x, y, flags, param)
# This is wrong
# image and temp_image refer to the same data and can be detected using the id function
# image = temp_image
image[:] = temp_image
print(id(image))
print(id(temp_image))
cv2.rectangle(image, (point_x, point_y), (x, y), (0.0.255), 2)
# Flag to check whether the left mouse button is pressed
flag_mouse_lbutton_down = False
# rectangle point
point_x = -1
point_y = -1
image_path = "opencv-image.png"
image = cv2.imread(image_path)
Note the deep copy of numpy
temp_image = image.copy()
cv2.namedWindow("image")
cv2.setMouseCallback("image", draw_circle)
while True:
cv2.imshow("image", image)
# waitKey cannot be 0 and must have a value to refresh the image
# Mouse dot × can not be turned off, only keyboard input Q
if cv2.waitKey(10) = =ord("q") :break
cv2.destroyAllWindows()
Copy the code
The results
/ home/coder/anaconda3 / bin/python3.8 / home/coder/PycharmProjects pythonProject2 / main py EVENT_LBUTTONDOWN 1 94 74 1 None EVENT_MOUSEMOVE+EVENT_FLAG_LBUTTON 0 105 90 1 None 140072728071696 140072281407376 EVENT_MOUSEMOVE+EVENT_FLAG_LBUTTON 0 109 97 1 None 140072728071696 140072281407376 EVENT_MOUSEMOVE+EVENT_FLAG_LBUTTON 0 114 105 1 None 140072728071696 140072281407376 EVENT_MOUSEMOVE+EVENT_FLAG_LBUTTON 0 121 119 1 None 140072728071696 140072281407376 EVENT_MOUSEMOVE+EVENT_FLAG_LBUTTON 0 130 131 1 None 140072728071696 140072281407376 EVENT_MOUSEMOVE+EVENT_FLAG_LBUTTON 0 155 162 1 None 140072728071696 140072281407376 EVENT_MOUSEMOVE+EVENT_FLAG_LBUTTON 0 168 179 1 None 140072728071696 140072281407376 EVENT_MOUSEMOVE+EVENT_FLAG_LBUTTON 0 191 207 1 None 140072728071696 140072281407376 EVENT_MOUSEMOVE+EVENT_FLAG_LBUTTON 0 205 224 1 None 140072728071696 140072281407376 EVENT_MOUSEMOVE+EVENT_FLAG_LBUTTON 0 218 240 1 None 140072728071696 140072281407376 EVENT_MOUSEMOVE+EVENT_FLAG_LBUTTON 0 235 260 1 None 140072728071696 140072281407376 EVENT_MOUSEMOVE+EVENT_FLAG_LBUTTON 0 248 271 1 None 140072728071696 140072281407376 EVENT_MOUSEMOVE+EVENT_FLAG_LBUTTON 0 251 274 1 None 140072728071696 140072281407376 EVENT_MOUSEMOVE+EVENT_FLAG_LBUTTON 0 252 274 1 None 140072728071696 140072281407376 EVENT_LBUTTONUP 4 252 274 1 None Process finished withexit code 0
Copy the code
Learning to recommend
- Python documentation – English
- Python documentation – Chinese
- Python code PEP
- Google version of the Python specification
- opencv
- Opencv help manual
- Python source code
- Python PEP
- Optimal kirin
- The nuggets platform
- Gitee platform
Python is open source, cross-platform, interpretive, interactive, and worth learning. Python’s design philosophy: elegant, unambiguous, simple. Advocate one way, preferably only one way to do one thing. Code should be written in accordance with specifications to facilitate communication and understanding. Every language has its own unique ideas. Beginners need to change their thinking, practice and accumulate.