-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessing
More file actions
179 lines (143 loc) · 5.86 KB
/
processing
File metadata and controls
179 lines (143 loc) · 5.86 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import cv2 as cv
import numpy as np
import PIL
from matplotlib import pyplot as plt
from picamera import PiCamera
from time import sleep
from PIL import Image, ImageOps
camera = PiCamera()
camera.rotation = 180
camera.capture('/home/silicon-leandra/Desktop/raspcam.jpg')
camera.stop_preview()
rasp_cam = Image.open('/home/silicon-leandra/Desktop/raspcam.jpg')
left = 180 ; top = 70 ; right = 1200 ; bottom = 900
img = rasp_cam.crop((left,top,right,bottom))
img.save('/home/silicon-leandra/Desktop/raspcam.jpg')
planets = cv.imread('/home/silicon-leandra/Desktop/raspcam.jpg')
gray_img = cv.cvtColor(planets, cv.COLOR_BGR2GRAY)
def color(image):
image = cv.cvtColor(image,cv.COLOR_BGR2GRAY)
return image
def gamma_and_threshold(image):
blur = cv.medianBlur(color(image),7)
gamma = np.array(255* (blur/255)**5.5,dtype='uint8')
threshold = cv.adaptiveThreshold(gamma,255,cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY_INV,871,0.1)
threshold = cv.bitwise_not(threshold)
return threshold
def dilatation_and_erosion(threshold):
kernel = np.ones((15,15), np.uint8)
img_dilation = cv.dilate(gamma_and_threshold(threshold), kernel, iterations=1)
img_erode = cv.erode(img_dilation,kernel, iterations=1)
return img_erode
minDist = 150 ; param1 = 100 ; param2 = 30 ; minRadius = 0 ; maxRadius = 0
circles = cv.HoughCircles(
cv.medianBlur(gray_img, 5), #8-bit, single-channel, grayscale input image.
cv.HOUGH_GRADIENT, #Detection method
1, #Inverse ratio of the accumulator resolution to the image resolution
minDist, #Minimum distance between the centers of the detected circles
param1=param1, #First method-specific parameter
param2=param2, #Second method-specific parameter.
minRadius=minRadius,
maxRadius=maxRadius)
if circles is not None:
n_circulos = 0
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
cv.circle(
planets, #image
(i[0], i[1]), #center
i[2], #radius
(255,255, 255), #color
1) #shift
cv.circle(
planets,
(i[0],i[1]),
3,
(255,255, 255),
1)
n_circulos = n_circulos + 1
cv.putText(planets, f"circles found: {n_circulos:.0f}", (600,100),
cv.FONT_HERSHEY_SIMPLEX,
1.0, (255,255,255))
print('circles found:', n_circulos)
else:
print("No circles found")
imagem = dilatation_and_erosion(planets)
ret, labels = cv.connectedComponents(imagem)
label_hue = np.uint8(179 * labels / np.max(labels))
blank_ch = 255 * np.ones_like(label_hue)
labeled_img = cv.merge([label_hue, blank_ch, blank_ch])
labeled_img = cv.cvtColor(labeled_img,cv.COLOR_BGR2GRAY)
labeled_img[label_hue == 0] = 0
cv.putText(planets, f"Objects: {ret-1:.0f}", (600,150),
cv.FONT_HERSHEY_SIMPLEX,
1.0, (255,255,255))
hsv = cv.cvtColor(planets, cv.COLOR_BGR2HSV)
#green mask
green_low = np.array([40,50,50], np.uint8)
green_up = np.array([80,255,255], np.uint8)
green_mask = cv.inRange(hsv, green_low, green_up)
#blue mask
blue_low = np.array([100,50,20], np.uint8)
blue_up = np.array([140,255,255], np.uint8)
blue_mask = cv.inRange(hsv, blue_low, blue_up)
#red mask
red_low = np.array([170,50,50], np.uint8)
red_up = np.array([180,255,255], np.uint8)
red_mask = cv.inRange(hsv, red_low, red_up)
kernal = np.ones((5,5), 'uint8')
green_mask = cv.dilate(green_mask, kernal)
res_green = cv.bitwise_and(planets, planets,
mask = green_mask)
blue_mask = cv.dilate(blue_mask, kernal)
res_blue = cv.bitwise_and(planets, planets,
mask = blue_mask)
red_mask = cv.dilate(red_mask, kernal)
res_red = cv.bitwise_and(planets, planets,
mask = red_mask)
#green
contours, hierarchy = cv.findContours(green_mask,
cv.RETR_TREE,
cv.CHAIN_APPROX_SIMPLE)
for pic, contour in enumerate(contours):
area = cv.contourArea(contour)
if(area>300):
x,y,w,h = cv.boundingRect(contour)
planets = cv.rectangle(planets, (x,y),
(x + w, y + h),
(0,255,0), 2)
cv.putText(planets, "green", (x,y),
cv.FONT_HERSHEY_SIMPLEX,
1.0, (0,255,0))
#blue
contours, hierarchy = cv.findContours(blue_mask,
cv.RETR_TREE,
cv.CHAIN_APPROX_SIMPLE)
for pic, contour in enumerate(contours):
area = cv.contourArea(contour)
if(area>300):
x,y,w,h = cv.boundingRect(contour)
planets = cv.rectangle(planets, (x,y),
(x + w, y + h),
(255,0,0), 2)
cv.putText(planets, "blue", (x,y),
cv.FONT_HERSHEY_SIMPLEX,
1.0, (255,0,0))
#red
contours, hierarchy = cv.findContours(red_mask,
cv.RETR_TREE,
cv.CHAIN_APPROX_SIMPLE)
for pic, contour in enumerate(contours):
area = cv.contourArea(contour)
if(area>300):
x,y,w,h = cv.boundingRect(contour)
planets = cv.rectangle(planets, (x,y),
(x + w, y + h),
(0,0,255), 2)
cv.putText(planets, "red", (x,y),
cv.FONT_HERSHEY_SIMPLEX,
1.0, (0,0,255))
cv.imshow("colors", planets)
cv.waitKey(0)
#cv.destroyAllWindows()
#create a track bar -> thresold values, gamma corection value, hsv values