-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect_color_data.py
More file actions
34 lines (25 loc) · 1006 Bytes
/
detect_color_data.py
File metadata and controls
34 lines (25 loc) · 1006 Bytes
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
import cv2
import numpy as np
#this function take in a color and capture the current cam image with detection of the color-object
def detect_color_data(lowerBound,upperBound):
cam = cv2.VideoCapture(0)
kernelOpen = np.ones((5, 5))
kernelClose = np.ones((20, 20))
font = cv2.FONT_HERSHEY_SIMPLEX
ret, img = cam.read()
img = cv2.resize(img, (1024, 768))
# convert BGR to HSV
imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# create the Mask
mask = cv2.inRange(imgHSV, lowerBound, upperBound)
# morphology
maskOpen = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernelOpen)
maskClose = cv2.morphologyEx(maskOpen, cv2.MORPH_CLOSE, kernelClose)
maskFinal = maskClose
lala, conts, h = cv2.findContours(maskFinal.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
if len(conts) == 0 :
x, y, w, h = [0, 0, 0, 0]
else:
x, y, w, h = cv2.boundingRect(conts[0])
return x, y, w, h
#print(detect_color_data(lowerBound1,upperBound1))