-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdetectApple.py
47 lines (32 loc) · 1.3 KB
/
detectApple.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
import cv2 as cv
import numpy as np
cap = cv.VideoCapture(0)
while (1):
_, frame = cap.read()
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
lowerRed = np.array([90, 100, 100])
upperRed = np.array([100, 255, 255])
mask = cv.inRange(hsv, lowerRed, upperRed)
kernel = np.ones((5, 5), np.uint8)
dilate = cv.dilate(mask, kernel, iterations=2)
ret, thresh = cv.threshold(dilate, 15, 275, cv.THRESH_BINARY)
M = cv.moments(thresh)
if (M['m00'] > 0):
# calculate x,y coordinate of center
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
else:
cX, cY = 700, 700
# put text and highlight the center
cv.circle(frame, (cX, cY), 5, (255, 255, 255), -1)
# cv.putText(frame, "centroid", (cX - 60, cY - 60),cv.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
# parameters: input, contours to be passed in, draw all contours (-1) or index to a specific one, color, thickness
img = cv.drawContours(frame, contours, -1, (0, 255, 0), 3)
cv.imshow("Frame", frame)
cv.imshow("hsv", hsv)
cv.imshow("Mask", mask)
k = cv.waitKey(5) & 0xff
if k == 27:
break
cv.destroyAllWindows()