-
Notifications
You must be signed in to change notification settings - Fork 1
/
scale_calibrate.py
220 lines (183 loc) · 6.68 KB
/
scale_calibrate.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# scale_calibrate.py
# this file takes a bee video, and extracts the size of the checkerboard pattern on the
# reference card, then calculates the dimensions and area of bee abdomen in pixels
import cv2
import numpy as np
import argparse
import imutils
import statistics
import pandas as pd
from matplotlib import pyplot as plt
# take inputs
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to the input video")
ap.add_argument("-o", "--outdir", required=True,
help="path to the out directory")
ap.add_argument("-l", "--leftside", type=bool, required=True,
help="is the reference card on the left side?")
ap.add_argument("-s", "--size", type=int, default=1.5, required=False,
help="length of bee in cm")
ap.add_argument("-v", "--visualize", default=False, required=False,
help="show visualizations")
args = vars(ap.parse_args())
LEFTSIDE = args['leftside'] # is the reference card on the left side
BEE_SIZE = args['size'] # length of bee in cm
VISUALIZE = args['visualize'] # show visualizations
OUT_DIR = args['outdir'] # directory for output
label = args['image'].split('.')[0].split('/')[-1] # output filename
# Performs histogram equalization on a color image
def equalizeMe(img_in):
"""
Takes an image and performs histogram equalization.
Inputs:
- img_in: image to be equalized
Outputs:
- histogram equalized
"""
# From:
# https://towardsdatascience.com/histogram-equalization-a-simple-way-to-improve-the-contrast-of-your-image-bcd66596d815
# segregate color streams
b, g, r = cv2.split(img_in)
h_b, bin_b = np.histogram(b.flatten(), 256, [0, 256])
h_g, bin_g = np.histogram(g.flatten(), 256, [0, 256])
h_r, bin_r = np.histogram(r.flatten(), 256, [0, 256])
# calculate cdf
cdf_b = np.cumsum(h_b)
cdf_g = np.cumsum(h_g)
cdf_r = np.cumsum(h_r)
# mask all pixels with value=0 and replace it with mean of the pixel values
cdf_m_b = np.ma.masked_equal(cdf_b, 0)
cdf_m_b = (cdf_m_b - cdf_m_b.min())*255/(cdf_m_b.max()-cdf_m_b.min())
cdf_final_b = np.ma.filled(cdf_m_b, 0).astype('uint8')
cdf_m_g = np.ma.masked_equal(cdf_g, 0)
cdf_m_g = (cdf_m_g - cdf_m_g.min())*255/(cdf_m_g.max()-cdf_m_g.min())
cdf_final_g = np.ma.filled(cdf_m_g, 0).astype('uint8')
cdf_m_r = np.ma.masked_equal(cdf_r, 0)
cdf_m_r = (cdf_m_r - cdf_m_r.min())*255/(cdf_m_r.max()-cdf_m_r.min())
cdf_final_r = np.ma.filled(cdf_m_r, 0).astype('uint8')
# merge the images in the three channels
img_b = cdf_final_b[b]
img_g = cdf_final_g[g]
img_r = cdf_final_r[r]
img_out = cv2.merge((img_b, img_g, img_r))
# validation
equ_b = cv2.equalizeHist(b)
equ_g = cv2.equalizeHist(g)
equ_r = cv2.equalizeHist(r)
equ = cv2.merge((equ_b, equ_g, equ_r))
# print(equ)
#cv2.imwrite('output_name.png', equ)
return img_out
def detect(c):
"""
Based on contour, is it a rectangle or square?
Inputs:
- contour
Outputs:
- height, width, shape of contour
"""
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.04*peri, True)
h = 0
w = 0
shape = "nope"
# if it has 4 sides, must be a rectangle or square
if len(approx) == 4:
(x, y, w, h) = cv2.boundingRect(approx)
h = h
w = w
ar = w/float(h)
shape = "square" if ar >= 0.95 and ar <= 1.05 else "rectangle"
return (h, w, shape)
# open video
cap = cv2.VideoCapture(args['image'])
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# loop through video and save the 80th frame to do the calibration from
# this is a hacky solution but oh well
prev_frame = None
counter = 0 # Frame counter
while True:
counter += 1
ret, frame = cap.read()
# Break when video ends
if ret is False:
break
# save image
if counter == 80:
plt.imsave('image' + '.jpeg',
cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
break
# load image
image = equalizeMe(cv2.imread('image.jpeg'))
height = image.shape[0]
width = image.shape[1]
# crop to just the scale card.
# Assuming that it is in the bottom quarter of the image on either the left or right side
if LEFTSIDE:
crop = image[int(height*3/4):height, 0:width//2]
else:
crop = image[int(height*3/4):height, width//2:width]
if VISUALIZE:
cv2.imshow("crop", crop)
# resize it (but not really)
resized = imutils.resize(crop, width=width)
ratio = crop.shape[0] / float(resized.shape[0])
# convert the resized image to grayscale, blur it slightly,
# and threshold it
gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (1, 1), 0)
thresh = cv2.threshold(blurred, 128, 255, cv2.THRESH_BINARY_INV)[1]
# get contours from the thresholded images
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
if VISUALIZE:
cv2.imshow("threst", thresh)
# loop through all the contours, get the sqaures
squares = []
maxsquares = []
for c in cnts:
M = cv2.moments(c)
if M['m00'] == 0:
continue
cX = int((M['m10']/M['m00'])*ratio)
cY = int((M['m01']/M['m00'])*ratio)
(h_1, w_1, shape) = detect(c)
h = h_1 * ratio
w = w_1*ratio
if shape != 'square' or h < 10:
continue
squares.append(((cX, cY), h, w))
maxsquares.append(max(h, w))
# multiply the contour (x, y)-coordinates by the resize ratio,
# then draw the contours and the name of the shape on the image
c = c.astype('float')
c *= ratio
c = c.astype('int')
if VISUALIZE:
cv2.drawContours(crop, [c], -1, (0, 255, 0), 2)
cv2.putText(crop, shape + str(h) + " "+str(int(w)), (cX, cY), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (0, 255, 255), 2)
if VISUALIZE:
cv2.imshow("image", crop)
cv2.waitKey(0)
# the median largest square is probably a reference square (which is 1cm in width)
try:
pix_cm = statistics.median(maxsquares)
# calulate the size of a bee abdomen in pixels
# the abdomen is 0.47 * total length of bee
# width is 0.4 * length of abdomen
bee_l = BEE_SIZE*pix_cm*0.47
bee_w = bee_l*0.4
bee_area = int(bee_l*bee_w) # in pixels
out_dict = {'pixelspercm': [pix_cm], 'bee_len': [bee_l],
'bee_w': [bee_w], 'bee_area': [bee_area]}
out_df = pd.DataFrame(
out_dict)
out_df.to_pickle(OUT_DIR+label + '_scale.pkl')
print(squares, '\npixels per cm =', pix_cm, '\n bee length =',
bee_l, '\n bee width =', bee_w, '\n bee area =', bee_area)
except:
print("no reference squares detected")