forked from spmallick/learnopencv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractiveColorDetect.py
65 lines (52 loc) · 2.6 KB
/
interactiveColorDetect.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
import cv2,argparse,glob
import numpy as np
# mouse callback function
def showPixelValue(event,x,y,flags,param):
global img, combinedResult, placeholder
if event == cv2.EVENT_MOUSEMOVE:
# get the value of pixel from the location of mouse in (x,y)
bgr = img[y,x]
# Convert the BGR pixel into other colro formats
ycb = cv2.cvtColor(np.uint8([[bgr]]),cv2.COLOR_BGR2YCrCb)[0][0]
lab = cv2.cvtColor(np.uint8([[bgr]]),cv2.COLOR_BGR2Lab)[0][0]
hsv = cv2.cvtColor(np.uint8([[bgr]]),cv2.COLOR_BGR2HSV)[0][0]
# Create an empty placeholder for displaying the values
placeholder = np.zeros((img.shape[0],400,3),dtype=np.uint8)
# fill the placeholder with the values of color spaces
cv2.putText(placeholder, "BGR {}".format(bgr), (20, 70), cv2.FONT_HERSHEY_COMPLEX, .9, (255,255,255), 1, cv2.LINE_AA)
cv2.putText(placeholder, "HSV {}".format(hsv), (20, 140), cv2.FONT_HERSHEY_COMPLEX, .9, (255,255,255), 1, cv2.LINE_AA)
cv2.putText(placeholder, "YCrCb {}".format(ycb), (20, 210), cv2.FONT_HERSHEY_COMPLEX, .9, (255,255,255), 1, cv2.LINE_AA)
cv2.putText(placeholder, "LAB {}".format(lab), (20, 280), cv2.FONT_HERSHEY_COMPLEX, .9, (255,255,255), 1, cv2.LINE_AA)
# Combine the two results to show side by side in a single image
combinedResult = np.hstack([img,placeholder])
cv2.imshow('PRESS P for Previous, N for Next Image',combinedResult)
if __name__ == '__main__' :
# load the image and setup the mouse callback function
global img
files = glob.glob('images/rub*.jpg')
files.sort()
img = cv2.imread(files[0])
img = cv2.resize(img,(400,400))
cv2.imshow('PRESS P for Previous, N for Next Image',img)
# Create an empty window
cv2.namedWindow('PRESS P for Previous, N for Next Image')
# Create a callback function for any event on the mouse
cv2.setMouseCallback('PRESS P for Previous, N for Next Image',showPixelValue)
i = 0
while(1):
k = cv2.waitKey(1) & 0xFF
# check next image in the folder
if k == ord('n'):
i += 1
img = cv2.imread(files[i%len(files)])
img = cv2.resize(img,(400,400))
cv2.imshow('PRESS P for Previous, N for Next Image',img)
# check previous image in folder
elif k == ord('p'):
i -= 1
img = cv2.imread(files[i%len(files)])
img = cv2.resize(img,(400,400))
cv2.imshow('PRESS P for Previous, N for Next Image',img)
elif k == 27:
cv2.destroyAllWindows()
break