121    def detect(self, imgbgr, outimg = None):
 
  123        h, w, chans = imgbgr.shape
 
  126        imghsv = cv2.cvtColor(imgbgr, cv2.COLOR_BGR2HSV)
 
  130        str = 
"H={}-{} S={}-{} V={}-{} ".format(self.
HSVmin[0], self.
HSVmax[0], self.
HSVmin[1],
 
  134        if not hasattr(self, 
'erodeElement'):
 
  143        contours, hierarchy = cv2.findContours(imgth, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
 
  144        str += 
"N={} ".format(len(contours))
 
  147        contours = sorted(contours, key = cv2.contourArea, reverse = 
True)[:maxn]
 
  155            if len(str2) > len(beststr2): beststr2 = str2
 
  159            area = cv2.contourArea(c, oriented = 
False)
 
  162            rawhull = cv2.convexHull(c, clockwise = 
True)
 
  163            rawhullperi = cv2.arcLength(rawhull, closed = 
True)
 
  164            hull = cv2.approxPolyDP(rawhull, epsilon = self.
epsilon * rawhullperi * 3.0, closed = 
True)
 
  167            if (hull.shape != (4,1,2)): 
continue  
  170            huarea = cv2.contourArea(hull, oriented = 
False)
 
  174            hufill = area / huarea * 100.0
 
  179            peri = cv2.arcLength(c, closed = 
True)
 
  180            approx = cv2.approxPolyDP(c, epsilon = self.
epsilon * peri, closed = 
True)
 
  181            if len(approx) < 7 
or len(approx) > 9: 
continue   
  185            serr = 100.0 * cv2.matchShapes(c, approx, cv2.CONTOURS_MATCH_I1, 0.0)
 
  186            if serr > self.
ethresh: 
continue 
  198            if reject == 1: 
continue 
  210            v10p23 = complex(hull[0][0,0] - hull[1][0,0] + hull[3][0,0] - hull[2][0,0],
 
  211                             hull[0][0,1] - hull[1][0,1] + hull[3][0,1] - hull[2][0,1])
 
  212            len10p23 = abs(v10p23)
 
  213            v03p12 = complex(hull[3][0,0] - hull[0][0,0] + hull[2][0,0] - hull[1][0,0],
 
  214                             hull[3][0,1] - hull[0][0,1] + hull[2][0,1] - hull[1][0,1])
 
  215            len03p12 = abs(v03p12)
 
  218            momC = cv2.moments(c)
 
  219            momH = cv2.moments(hull)
 
  220            vCH = complex(momH[
'm10'] / momH[
'm00'] - momC[
'm10'] / momC[
'm00'],
 
  221                          momH[
'm01'] / momH[
'm00'] - momC[
'm01'] / momC[
'm00'])
 
  224            if len10p23 < 0.1 
or len03p12 < 0.1 
or lenCH < 0.1: 
continue 
  227            good = (v10p23.real * vCH.real + v10p23.imag * vCH.imag) / (len10p23 * lenCH)
 
  228            bad = (v03p12.real * vCH.real + v03p12.imag * vCH.imag) / (len03p12 * lenCH)
 
  231            if vCH.imag >= -2.0: 
continue 
  235            if bad > good: hull = np.roll(hull, shift = 1, axis = 0)
 
  241        if len(str2) > len(beststr2):  beststr2 = str2
 
  244        if outimg 
is not None and outimg.valid():
 
  245            if (outimg.width == w * 2): jevois.pasteGreyToYUYV(imgth, outimg, w, 0)
 
  246            jevois.writeText(outimg, str + beststr2, 3, h+1, jevois.YUYV.White, jevois.Font.Font6x10)
 
 
  306        empty = np.array([ (0.0), (0.0), (0.0) ])
 
  310            if np.array_equal(rvecs[i], empty):
 
  315            axisPoints = np.array([ (0.0, 0.0, 0.0), (hw, 0.0, 0.0), (0.0, hh, 0.0), (0.0, 0.0, dd) ])
 
  316            imagePoints, jac = cv2.projectPoints(axisPoints, rvecs[i], tvecs[i], self.
camMatrix, self.
distCoeffs)
 
  319            jevois.drawLine(outimg, int(imagePoints[0][0,0] + 0.5), int(imagePoints[0][0,1] + 0.5),
 
  320                            int(imagePoints[1][0,0] + 0.5), int(imagePoints[1][0,1] + 0.5),
 
  321                            2, jevois.YUYV.MedPurple)
 
  322            jevois.drawLine(outimg, int(imagePoints[0][0,0] + 0.5), int(imagePoints[0][0,1] + 0.5),
 
  323                            int(imagePoints[2][0,0] + 0.5), int(imagePoints[2][0,1] + 0.5),
 
  324                            2, jevois.YUYV.MedGreen)
 
  325            jevois.drawLine(outimg, int(imagePoints[0][0,0] + 0.5), int(imagePoints[0][0,1] + 0.5),
 
  326                            int(imagePoints[3][0,0] + 0.5), int(imagePoints[3][0,1] + 0.5),
 
  327                            2, jevois.YUYV.MedGrey)
 
  330            cubePoints = np.array([ (-hw, -hh, 0.0), (hw, -hh, 0.0), (hw, hh, 0.0), (-hw, hh, 0.0),
 
  331                                    (-hw, -hh, dd), (hw, -hh, dd), (hw, hh, dd), (-hw, hh, dd) ])
 
  332            cu, jac2 = cv2.projectPoints(cubePoints, rvecs[i], tvecs[i], self.
camMatrix, self.
distCoeffs)
 
  338            jevois.drawLine(outimg, int(cu[0][0,0]), int(cu[0][0,1]), int(cu[1][0,0]), int(cu[1][0,1]),
 
  339                            1, jevois.YUYV.LightGreen)
 
  340            jevois.drawLine(outimg, int(cu[1][0,0]), int(cu[1][0,1]), int(cu[2][0,0]), int(cu[2][0,1]),
 
  341                            1, jevois.YUYV.LightGreen)
 
  342            jevois.drawLine(outimg, int(cu[2][0,0]), int(cu[2][0,1]), int(cu[3][0,0]), int(cu[3][0,1]),
 
  343                            1, jevois.YUYV.LightGreen)
 
  344            jevois.drawLine(outimg, int(cu[3][0,0]), int(cu[3][0,1]), int(cu[0][0,0]), int(cu[0][0,1]),
 
  345                            1, jevois.YUYV.LightGreen)
 
  346            jevois.drawLine(outimg, int(cu[4][0,0]), int(cu[4][0,1]), int(cu[5][0,0]), int(cu[5][0,1]),
 
  347                            1, jevois.YUYV.LightGreen)
 
  348            jevois.drawLine(outimg, int(cu[5][0,0]), int(cu[5][0,1]), int(cu[6][0,0]), int(cu[6][0,1]),
 
  349                            1, jevois.YUYV.LightGreen)
 
  350            jevois.drawLine(outimg, int(cu[6][0,0]), int(cu[6][0,1]), int(cu[7][0,0]), int(cu[7][0,1]),
 
  351                            1, jevois.YUYV.LightGreen)
 
  352            jevois.drawLine(outimg, int(cu[7][0,0]), int(cu[7][0,1]), int(cu[4][0,0]), int(cu[4][0,1]),
 
  353                            1, jevois.YUYV.LightGreen)
 
  354            jevois.drawLine(outimg, int(cu[0][0,0]), int(cu[0][0,1]), int(cu[4][0,0]), int(cu[4][0,1]),
 
  355                            1, jevois.YUYV.LightGreen)
 
  356            jevois.drawLine(outimg, int(cu[1][0,0]), int(cu[1][0,1]), int(cu[5][0,0]), int(cu[5][0,1]),
 
  357                            1, jevois.YUYV.LightGreen)
 
  358            jevois.drawLine(outimg, int(cu[2][0,0]), int(cu[2][0,1]), int(cu[6][0,0]), int(cu[6][0,1]),
 
  359                            1, jevois.YUYV.LightGreen)
 
  360            jevois.drawLine(outimg, int(cu[3][0,0]), int(cu[3][0,1]), int(cu[7][0,0]), int(cu[7][0,1]),
 
  361                            1, jevois.YUYV.LightGreen)