def _extractData(self, contour, hierarchy, contourNum, color, minsize, maxsize, appx_level): """ Extract the bulk of the data from a give blob. If the blob's are is too large or too small the method returns none. """ if (contour is None or not len(contour)): return None area = cv2.contourArea(contour) if (area < minsize or area > maxsize): return None retVal = Blob() retVal.image = color retVal.mArea = area retVal.mMinRectangle = cv2.minAreaRect(contour) bb = cv2.boundingRect(contour) retVal.x = bb[0] + (bb[2] / 2) retVal.y = bb[1] + (bb[3] / 2) retVal.mPerimeter = cv2.arcLength(contour, True) retVal.mContour = contour if (retVal.mContour is not None): retVal.mContourAppx = [] appx = cv2.approxPolyDP(np.array(retVal.mContour, 'float32'), appx_level, True) for p in appx: retVal.mContourAppx.append((int(p[0][0]), int(p[0][1]))) # so this is a bit hacky.... # For blobs that live right on the edge of the image OpenCV reports the position and width # height as being one over for the true position. E.g. if a blob is at (0,0) OpenCV reports # its position as (1,1). Likewise the width and height for the other corners is reported as # being one less than the width and height. This is a known bug. xx = bb[0] yy = bb[1] ww = bb[2] hh = bb[3] retVal.points = [(xx, yy), (xx + ww, yy), (xx + ww, yy + hh), (xx, yy + hh)] retVal._updateExtents() chull = cv2.convexHull(contour, returnPoints=1) retVal._mConvexHullnp = chull retVal.mConvexHull = list(tuple(pt[0]) for pt in list(chull)) # KAS -- FLAG FOR REPLACE 6/6/2012 #hullMask = self._getHullMask(chull,bb) # KAS -- FLAG FOR REPLACE 6/6/2012 #retVal.mHullImg = self._getBlobAsImage(chull,bb,color.getBitmap(),hullMask) # KAS -- FLAG FOR REPLACE 6/6/2012 #retVal.mHullMask = Image(hullMask) moments = cv2.moments(contour) #This is a hack for a python wrapper bug that was missing #the constants required from the ctype retVal.m00 = area retVal.m10 = moments['m10'] retVal.m01 = moments['m01'] retVal.m11 = moments['m11'] retVal.m20 = moments['m20'] retVal.m02 = moments['m02'] retVal.m21 = moments['m21'] retVal.m12 = moments['m12'] retVal.mHu = cv2.HuMoments(moments) # KAS -- FLAG FOR REPLACE 6/6/2012 mask = self._getMask(contour, bb) #retVal.mMask = Image(mask) retVal.mAvgColor = self._getAvg(color.getNumpy(), bb, mask) retVal.mAvgColor = retVal.mAvgColor[0:3] # KAS -- FLAG FOR REPLACE 6/6/2012 #retVal.mImg = self._getBlobAsImage(seq,bb,color.getBitmap(),mask) retVal.mHoleContour = self._getHoles(contourNum) retVal.mAspectRatio = retVal.mMinRectangle[1][ 0] / retVal.mMinRectangle[1][1] return retVal
def _extractData(self, contour, hierarchy, contourNum, color, minsize, maxsize, appx_level): """ Extract the bulk of the data from a give blob. If the blob's are is too large or too small the method returns none. """ if contour is None or not len(contour): return None area = cv2.contourArea(contour) if area < minsize or area > maxsize: return None retVal = Blob() retVal.image = color retVal.mArea = area retVal.mMinRectangle = cv2.minAreaRect(contour) bb = cv2.boundingRect(contour) retVal.x = bb[0] + (bb[2] / 2) retVal.y = bb[1] + (bb[3] / 2) retVal.mPerimeter = cv2.arcLength(contour, True) retVal.mContour = contour if retVal.mContour is not None: retVal.mContourAppx = [] appx = cv2.approxPolyDP(np.array(retVal.mContour, "float32"), appx_level, True) for p in appx: retVal.mContourAppx.append((int(p[0][0]), int(p[0][1]))) # so this is a bit hacky.... # For blobs that live right on the edge of the image OpenCV reports the position and width # height as being one over for the true position. E.g. if a blob is at (0,0) OpenCV reports # its position as (1,1). Likewise the width and height for the other corners is reported as # being one less than the width and height. This is a known bug. xx = bb[0] yy = bb[1] ww = bb[2] hh = bb[3] retVal.points = [(xx, yy), (xx + ww, yy), (xx + ww, yy + hh), (xx, yy + hh)] retVal._updateExtents() chull = cv2.convexHull(contour, returnPoints=1) retVal._mConvexHullnp = chull retVal.mConvexHull = list(tuple(pt[0]) for pt in list(chull)) # KAS -- FLAG FOR REPLACE 6/6/2012 # hullMask = self._getHullMask(chull,bb) # KAS -- FLAG FOR REPLACE 6/6/2012 # retVal.mHullImg = self._getBlobAsImage(chull,bb,color.getBitmap(),hullMask) # KAS -- FLAG FOR REPLACE 6/6/2012 # retVal.mHullMask = Image(hullMask) moments = cv2.moments(contour) # This is a hack for a python wrapper bug that was missing # the constants required from the ctype retVal.m00 = area retVal.m10 = moments["m10"] retVal.m01 = moments["m01"] retVal.m11 = moments["m11"] retVal.m20 = moments["m20"] retVal.m02 = moments["m02"] retVal.m21 = moments["m21"] retVal.m12 = moments["m12"] retVal.mHu = cv2.HuMoments(moments) # KAS -- FLAG FOR REPLACE 6/6/2012 mask = self._getMask(contour, bb) # retVal.mMask = Image(mask) retVal.mAvgColor = self._getAvg(color.getNumpy(), bb, mask) retVal.mAvgColor = retVal.mAvgColor[0:3] # KAS -- FLAG FOR REPLACE 6/6/2012 # retVal.mImg = self._getBlobAsImage(seq,bb,color.getBitmap(),mask) retVal.mHoleContour = self._getHoles(contourNum) retVal.mAspectRatio = retVal.mMinRectangle[1][0] / retVal.mMinRectangle[1][1] return retVal