-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColor_Detection.py
65 lines (49 loc) · 2.16 KB
/
Color_Detection.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
import numpy as np
# Capture from WebCam
cap = cv2.VideoCapture(0)
while True:
# read camera
ret, frame = cap.read()
# blurred the image with gaussian and median blur
blurred_frame = cv2.GaussianBlur(frame, (5, 5), 0)
blurred_frame = cv2.medianBlur(blurred_frame, 5)
# Convert BGR to HSV
hsv = cv2.cvtColor(blurred_frame, cv2.COLOR_BGR2HSV)
# Set color
colors = {"Turuncu": (255,255,255)}
lower = {"Turuncu": (5,105,105)}
upper = {"Turuncu": (15,255,255)}
for key, value in upper.items():
# create a kernel
kernel = np.ones((7, 7), np.uint8)
# Apply Threshold with inRange
PrimaryMask = cv2.inRange(hsv, lower[key], upper[key])
# Apply morphologyEx mask
mask = cv2.morphologyEx(PrimaryMask, cv2.MORPH_OPEN, kernel)
mask = cv2.morphologyEx(PrimaryMask, cv2.MORPH_CLOSE, kernel)
center = None
_ ,contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for pic, contour in enumerate(contours):
approx = cv2.approxPolyDP(contour, 0.02 * cv2.arcLength(contour, True), True)
# Set central points
x = approx.ravel()[0]
y = approx.ravel()[1]
if len(contours) > 0:
area = cv2.contourArea(contour)
c = max(contours, key=cv2.contourArea)
((x, y), radius) = cv2.minEnclosingCircle(c)
if (area > 300): # for removing small noises
M = cv2.moments(c)
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
cv2.drawContours(frame, [approx], -1, colors[key], 2)
cv2.circle(frame, center, 3, (0, 0, 0), 2)
print("merkez",center)
cv2.circle(frame, center, 7, (0, 0, 255), -1)
cv2.putText(frame, key, (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, 0.7, colors[key])
# cv2.imshow('Maske', mask)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()