-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcolour_query.py
97 lines (56 loc) · 2.71 KB
/
colour_query.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#####################################################################
# Example : displaying and interact with image from file
# Author : Toby Breckon, [email protected]
# Copyright (c) 2015 School of Engineering & Computing Science,
# Durham University, UK
# License : LGPL - http://www.gnu.org/licenses/lgpl.html
#####################################################################
import cv2
#####################################################################
# mouse callback function - displays or sets image colour at the click
# location of the mouse
def colour_query_mouse_callback(event, x, y, flags, param):
# records mouse events at postion (x,y) in the image window
# left button click prints colour information at click location to stdout
if event == cv2.EVENT_LBUTTONDOWN:
print("BGR colour @ position (%d,%d) = %s" %
(x, y, ', '.join(str(i) for i in img[y, x])))
# right button sets colour information at click location to white
elif event == cv2.EVENT_RBUTTONDOWN:
img[y, x] = [255, 255, 255]
#####################################################################
# define display window name
window_name = "Displayed Image" # window name
# read an image from the specified file (in colour)
img = cv2.imread('example.jpg', cv2.IMREAD_COLOR)
# check it has loaded
if img is not None:
# create a named window object
cv2.namedWindow(window_name)
# set the mouse call back function that will be called every time
# the mouse is clicked inside the associated window
cv2.setMouseCallback(window_name, colour_query_mouse_callback)
# set a loop control flag
keep_processing = True
while (keep_processing):
# display this blurred image in a named window
cv2.imshow(window_name, img)
# start the event loop - essential
# cv2.waitKey() is a keyboard binding function (argument is the time in
# ms). It waits for specified milliseconds for any keyboard event.
# If you press any key in that time, the program continues.
# If 0 is passed, it waits indefinitely for a key stroke.
# (bitwise and with 0xFF to extract least significant byte of
# multi-byte response)
# wait 40ms (i.e. 1000ms / 25 fps = 40 ms)
key = cv2.waitKey(40) & 0xFF
# It can also be set to detect specific key strokes by recording which
# key is pressed
# e.g. if user presses "x" then exit
if (key == ord('x')):
keep_processing = False
else:
print("No image file successfully loaded.")
# ... and finally close all windows
cv2.destroyAllWindows()
#####################################################################