-
Notifications
You must be signed in to change notification settings - Fork 0
/
contours.py
56 lines (51 loc) · 2.12 KB
/
contours.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
#This is the code for the robot to detect contours
from time import sleep
import imutils
import numpy as np
from picamera.array import PiRGBArray
from picamera import PiCamera
from gpiozero import LED
#import the LED eye
devastator_eye = LED(25)
#Let the LED blink 4 times before the code runs
for x in range(1, 5):
devastator_eye.off()
sleep(0.5)
devastator_eye.on()
sleep(0.5)
#Setup the camera
def setup_camera():
camera = PiCamera()
camera.resolution = (128, 128)
#camera.rotation = 180 #Set this variable only if your camera is upside down
capture_buffer = PiRBGArray(camera, size=(128, 128))
camera.start_preview() #This starts the preview of the camera
sleep(2)
return camera, capture_buffer
#Define the saturated colors function
def get_saturated_colors(image):
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) #This converts a BGR format to an HSV format
#Mask for vivid colors
cnts = cv2.findContours(masked.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(cnts) #Find the contours from cnts
contours = sorted(contours, key=cv2.contourArea, reverse=True)
color = [0, 0, 0]
m = cv2.moments(contours[0])
if m["m00"] > 0:
#cx is the average of taking the sum of x-coordinates m10 and dividing them by the count m00.
#cy is the average of taking the sum of y-coordinates m01 and dividing them by the count m00.
cx = int(m["m10"] / m["m00"])
cy = int(m["m01"] / m["m00"])
color = hsv[cy, cx]
return masked, contours, color
#The camera will take a contour, original and masked picture and save them as original.png, masked.png and with_contours.jpg
if __name__ == '__main__':
camera, capture_buffer = setup_camera()
camera.capture(capture_buffer, format="bgr") #This captures the image in bgr format
image = capture_buffer.array
masked_contours, found_color = get_saturated_colors(image)
cv2.imwrite('original.jpg' , image)
cv2.imwrite('masked.jpg', masked)
cv2.drawContours(image, contours[:1], -1, (0, 255, 0), 1) #This draws the contour with a green line (0, 255, 0) with width 1
cv2.imwrite('with_contours.png', image)
print(found_color)