forked from OpenCVChina/DeepRobotDog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhsv_picker.py
More file actions
44 lines (35 loc) · 1.43 KB
/
hsv_picker.py
File metadata and controls
44 lines (35 loc) · 1.43 KB
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
import cv2 as cv
import numpy as np
# Read source image
source_img = cv.imread("bill bottle.png")
# Read target image
target_img = cv.imread("bill bottle.png")
# Convert to HSV colorspace
source_hsv = cv.cvtColor(source_img, cv.COLOR_BGR2HSV)
# Convert target image to HSV colorspace
target_hsv = cv.cvtColor(target_img, cv.COLOR_BGR2HSV)
# Get average values of each channel
h_mean = np.mean(target_hsv[:, :, 0])
s_mean = np.mean(target_hsv[:, :, 1])
v_mean = np.mean(target_hsv[:, :, 2])
# Define lower and upper bounds for color
lower_blue = np.array([h_mean - 10, s_mean - 50, v_mean - 50])
upper_blue = np.array([h_mean + 10, s_mean + 50, v_mean + 50])
lower_blue = np.maximum(lower_blue, 0)
upper_blue = np.maximum(upper_blue, 0)
lower_blue = np.minimum(lower_blue, [179, 255, 255])
upper_blue = np.minimum(upper_blue, [179, 255, 255])
print("When the 'Mask' shows almost 'white' pixels, it means that the color can be detected.")
print("Lower Color: {}, Upper Color: {}".format(lower_blue, upper_blue))
# Apply inRange() function
mask = cv.inRange(source_hsv, lower_blue, upper_blue)
# Find contours on mask image
contours, _ = cv.findContours(mask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
# Draw bounding rectangles for each contour
for cnt in contours:
x, y, w, h = cv.boundingRect(cnt)
cv.rectangle(mask, (x, y), (x + w, y + h), (255, 255, 255), 1)
# Display mask image
cv.imshow("Mask", mask)
cv.waitKey(0)
cv.destroyAllWindows()