-
Notifications
You must be signed in to change notification settings - Fork 17
/
HSV Color Picker.py
81 lines (66 loc) · 2.36 KB
/
HSV Color Picker.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
import cv2
import numpy as np
import sys
import tkinter as tk
from tkinter import filedialog
image_hsv = None
pixel = (0,0,0) #RANDOM DEFAULT VALUE
ftypes = [
("JPG", "*.jpg;*.JPG;*.JPEG"),
("PNG", "*.png;*.PNG"),
("GIF", "*.gif;*.GIF"),
("All files", "*.*")
]
def check_boundaries(value, tolerance, ranges, upper_or_lower):
if ranges == 0:
# set the boundary for hue
boundary = 180
elif ranges == 1:
# set the boundary for saturation and value
boundary = 255
if(value + tolerance > boundary):
value = boundary
elif (value - tolerance < 0):
value = 0
else:
if upper_or_lower == 1:
value = value + tolerance
else:
value = value - tolerance
return value
def pick_color(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDOWN:
pixel = image_hsv[y,x]
#HUE, SATURATION, AND VALUE (BRIGHTNESS) RANGES. TOLERANCE COULD BE ADJUSTED.
# Set range = 0 for hue and range = 1 for saturation and brightness
# set upper_or_lower = 1 for upper and upper_or_lower = 0 for lower
hue_upper = check_boundaries(pixel[0], 10, 0, 1)
hue_lower = check_boundaries(pixel[0], 10, 0, 0)
saturation_upper = check_boundaries(pixel[1], 10, 1, 1)
saturation_lower = check_boundaries(pixel[1], 10, 1, 0)
value_upper = check_boundaries(pixel[2], 40, 1, 1)
value_lower = check_boundaries(pixel[2], 40, 1, 0)
upper = np.array([hue_upper, saturation_upper, value_upper])
lower = np.array([hue_lower, saturation_lower, value_lower])
print(lower, upper)
#A MONOCHROME MASK FOR GETTING A BETTER VISION OVER THE COLORS
image_mask = cv2.inRange(image_hsv,lower,upper)
cv2.imshow("Mask",image_mask)
def main():
global image_hsv, pixel
#OPEN DIALOG FOR READING THE IMAGE FILE
root = tk.Tk()
root.withdraw() #HIDE THE TKINTER GUI
file_path = filedialog.askopenfilename(filetypes = ftypes)
root.update()
image_src = cv2.imread(file_path)
cv2.imshow("BGR",image_src)
#CREATE THE HSV FROM THE BGR IMAGE
image_hsv = cv2.cvtColor(image_src,cv2.COLOR_BGR2HSV)
cv2.imshow("HSV",image_hsv)
#CALLBACK FUNCTION
cv2.setMouseCallback("HSV", pick_color)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__=='__main__':
main()