Skip to content

Commit 3273fc7

Browse files
committed
circle mouse
1 parent 1576510 commit 3273fc7

File tree

4 files changed

+74
-8
lines changed

4 files changed

+74
-8
lines changed
9.05 KB
Loading

ch06-绘图函数/画圆圈.py

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,46 @@
77
"""
88
画圆圈.py:随机覆盖,不同颜色,
99
"""
10-
10+
from time import sleep
1111
import cv2
1212
import numpy as np
1313

14+
15+
def click_event(event, x, y, flags, param):
16+
'''
17+
用左键点击屏幕,打印坐标
18+
:param event:
19+
:param x:
20+
:param y:
21+
:param flags:
22+
:param param:
23+
:return:
24+
'''
25+
if event == cv2.EVENT_LBUTTONDOWN:
26+
print(x, y, flags, param)
27+
28+
29+
cv2.namedWindow('Canvas', cv2.WINDOW_GUI_EXPANDED)
30+
cv2.setMouseCallback("Canvas", click_event)
31+
1432
canvas = np.zeros((300, 300, 3), dtype="uint8")
33+
while True:
34+
try:
35+
for i in range(0, 25):
36+
radius = np.random.randint(5, high=200)
37+
color = np.random.randint(0, high=256, size=(3,)).tolist()
38+
pt = np.random.randint(0, high=300, size=(2,))
39+
cv2.circle(canvas, tuple(pt), radius, color, -1)
1540

16-
for i in range(0, 25):
17-
radius = np.random.randint(5, high=200)
18-
color = np.random.randint(0, high=256, size=(3,)).tolist()
19-
pt = np.random.randint(0, high=300, size=(2,))
20-
cv2.circle(canvas, tuple(pt), radius, color, -1)
41+
cv2.imshow("Canvas", canvas)
2142

22-
cv2.imshow("Canvas", canvas)
23-
cv2.waitKey(0)
43+
key = cv2.waitKey(5)
44+
if key == ord('q'):
45+
break
46+
else:
47+
sleep(1)
48+
continue
49+
except KeyboardInterrupt as e:
50+
print('KeyboardInterrupt', e)
51+
finally:
52+
cv2.imwrite('random-circles2.jpg', canvas)
83.8 KB
Loading
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017/7/17 下午6:19
3+
# @Author : play4fun
4+
# @File : 鼠标左右键回调函数.py
5+
# @Software: PyCharm
6+
7+
"""
8+
鼠标左右键回调函数.py:
9+
"""
10+
11+
import cv2
12+
13+
14+
def click_event(event, x, y, flags, param):
15+
if event == cv2.EVENT_LBUTTONDOWN:
16+
print(x, y)
17+
18+
if event == cv2.EVENT_RBUTTONDOWN:
19+
red = img[y, x, 2]
20+
blue = img[y, x, 0]
21+
green = img[y, x, 1]
22+
print(red, green, blue)
23+
24+
strRGB = str(red) + "," + str(green) + "," + str(blue)
25+
font = cv2.FONT_HERSHEY_SIMPLEX
26+
cv2.putText(img, strRGB, (x, y), font, 1, (255, 255, 255), 2)
27+
cv2.imshow('original', img)
28+
29+
30+
img = cv2.imread('../data/messi5.jpg')
31+
cv2.imshow('original', img)
32+
33+
cv2.setMouseCallback("original", click_event)
34+
cv2.waitKey(0)
35+
cv2.imwrite('putText.jpg',img)
36+
37+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)