147 def detect(self, imggray, outimg = None):
152 if not hasattr(self,
'detector'):
156 if not hasattr(self,
'refkp'):
157 refimg = cv2.imread(self.
fname, 0)
161 refh, refw = refimg.shape
162 self.
refcorners = np.float32([ [ 0.0, 0.0 ], [ 0.0, refh ], [refw, refh ], [ refw, 0.0 ] ]).reshape(-1,1,2)
168 jevois.LINFO(
"Extracted {} keypoints and descriptors from {}".format(len(self.
refkp), self.
fname))
171 kp, des = self.
detector.detectAndCompute(imggray,
None)
172 str =
"{} keypoints".format(len(kp))
175 if not hasattr(self,
'matcher'):
176 self.
matcher = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck =
True)
180 matches = sorted(matches, key =
lambda x:x.distance)
181 str +=
", {} matches".format(len(matches))
186 if m.distance < self.
distth: lastidx += 1
188 matches = matches[0:lastidx]
189 str +=
", {} good".format(len(matches))
194 if len(matches) >= 10:
200 obj.append(self.
refkp[m.trainIdx].pt)
201 scene.append(kp[m.queryIdx].pt)
204 hmg, mask = cv2.findHomography(np.array(obj), np.array(scene), cv2.RANSAC, 5.0)
207 u, s, v = np.linalg.svd(hmg, full_matrices =
False)
212 if s[-1] > 0.001
and s[0] / s[-1] > 100:
214 corners = cv2.perspectiveTransform(self.
refcorners, hmg)
215 wincorners = cv2.perspectiveTransform(self.
wincorners, hmg)
218 if outimg
is not None and outimg.valid():
219 if len(corners) == 4:
220 jevois.drawLine(outimg, int(corners[0][0,0] + 0.5), int(corners[0][0,1] + 0.5),
221 int(corners[1][0,0] + 0.5), int(corners[1][0,1] + 0.5),
222 2, jevois.YUYV.LightPink)
223 jevois.drawLine(outimg, int(corners[1][0,0] + 0.5), int(corners[1][0,1] + 0.5),
224 int(corners[2][0,0] + 0.5), int(corners[2][0,1] + 0.5),
225 2, jevois.YUYV.LightPink)
226 jevois.drawLine(outimg, int(corners[2][0,0] + 0.5), int(corners[2][0,1] + 0.5),
227 int(corners[3][0,0] + 0.5), int(corners[3][0,1] + 0.5),
228 2, jevois.YUYV.LightPink)
229 jevois.drawLine(outimg, int(corners[3][0,0] + 0.5), int(corners[3][0,1] + 0.5),
230 int(corners[0][0,0] + 0.5), int(corners[0][0,1] + 0.5),
231 2, jevois.YUYV.LightPink)
232 jevois.writeText(outimg, str, 3, h+4, jevois.YUYV.White, jevois.Font.Font6x10)
236 if len(wincorners) == 4: hlist.append(wincorners)
296 empty = np.array([ (0.0), (0.0), (0.0) ])
302 if np.array_equal(rvecs[i], empty):
309 axisPoints = np.array([ (0.0, 0.0, 0.0), (hw, 0.0, 0.0), (0.0, hh, 0.0), (0.0, 0.0, dd) ])
310 imagePoints, jac = cv2.projectPoints(axisPoints, rvecs[i], tvecs[i], self.
camMatrix, self.
distCoeffs)
313 jevois.drawLine(outimg, int(imagePoints[0][0,0] + 0.5), int(imagePoints[0][0,1] + 0.5),
314 int(imagePoints[1][0,0] + 0.5), int(imagePoints[1][0,1] + 0.5),
315 2, jevois.YUYV.MedPurple)
316 jevois.drawLine(outimg, int(imagePoints[0][0,0] + 0.5), int(imagePoints[0][0,1] + 0.5),
317 int(imagePoints[2][0,0] + 0.5), int(imagePoints[2][0,1] + 0.5),
318 2, jevois.YUYV.MedGreen)
319 jevois.drawLine(outimg, int(imagePoints[0][0,0] + 0.5), int(imagePoints[0][0,1] + 0.5),
320 int(imagePoints[3][0,0] + 0.5), int(imagePoints[3][0,1] + 0.5),
321 2, jevois.YUYV.MedGrey)
325 cubePoints = np.array([ (-hw, -hh, 0.0), (hw, -hh, 0.0), (hw, hh, 0.0), (-hw, hh, 0.0),
326 (-hw, -hh, -dd), (hw, -hh, -dd), (hw, hh, -dd), (-hw, hh, -dd) ])
327 cu, jac2 = cv2.projectPoints(cubePoints, rvecs[i], tvecs[i], self.
camMatrix, self.
distCoeffs)
333 jevois.drawLine(outimg, int(cu[0][0,0]), int(cu[0][0,1]), int(cu[1][0,0]), int(cu[1][0,1]),
334 1, jevois.YUYV.LightGreen)
335 jevois.drawLine(outimg, int(cu[1][0,0]), int(cu[1][0,1]), int(cu[2][0,0]), int(cu[2][0,1]),
336 1, jevois.YUYV.LightGreen)
337 jevois.drawLine(outimg, int(cu[2][0,0]), int(cu[2][0,1]), int(cu[3][0,0]), int(cu[3][0,1]),
338 1, jevois.YUYV.LightGreen)
339 jevois.drawLine(outimg, int(cu[3][0,0]), int(cu[3][0,1]), int(cu[0][0,0]), int(cu[0][0,1]),
340 1, jevois.YUYV.LightGreen)
341 jevois.drawLine(outimg, int(cu[4][0,0]), int(cu[4][0,1]), int(cu[5][0,0]), int(cu[5][0,1]),
342 1, jevois.YUYV.LightGreen)
343 jevois.drawLine(outimg, int(cu[5][0,0]), int(cu[5][0,1]), int(cu[6][0,0]), int(cu[6][0,1]),
344 1, jevois.YUYV.LightGreen)
345 jevois.drawLine(outimg, int(cu[6][0,0]), int(cu[6][0,1]), int(cu[7][0,0]), int(cu[7][0,1]),
346 1, jevois.YUYV.LightGreen)
347 jevois.drawLine(outimg, int(cu[7][0,0]), int(cu[7][0,1]), int(cu[4][0,0]), int(cu[4][0,1]),
348 1, jevois.YUYV.LightGreen)
349 jevois.drawLine(outimg, int(cu[0][0,0]), int(cu[0][0,1]), int(cu[4][0,0]), int(cu[4][0,1]),
350 1, jevois.YUYV.LightGreen)
351 jevois.drawLine(outimg, int(cu[1][0,0]), int(cu[1][0,1]), int(cu[5][0,0]), int(cu[5][0,1]),
352 1, jevois.YUYV.LightGreen)
353 jevois.drawLine(outimg, int(cu[2][0,0]), int(cu[2][0,1]), int(cu[6][0,0]), int(cu[6][0,1]),
354 1, jevois.YUYV.LightGreen)
355 jevois.drawLine(outimg, int(cu[3][0,0]), int(cu[3][0,1]), int(cu[7][0,0]), int(cu[7][0,1]),
356 1, jevois.YUYV.LightGreen)