Beispiel #1
0
 def _extractData(self,seq,color,minsize,maxsize):
     """
     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( seq is None or not len(seq)):
         return None
     area = cv.ContourArea(seq)
     if( area < minsize or area > maxsize):
         return None
     retVal = Blob()
     retVal.image = color 
     retVal.mArea = area
     retVal.mMinRectangle = cv.MinAreaRect2(seq)
     retVal.mBoundingBox = cv.BoundingRect(seq)
     retVal.x = retVal.mBoundingBox[0]+(retVal.mBoundingBox[2]/2)
     retVal.y = retVal.mBoundingBox[1]+(retVal.mBoundingBox[3]/2)
     retVal.mPerimeter = cv.ArcLength(seq)
     
     if( seq is not None):  #KAS 
         retVal.mContour = list(seq)
     chull = cv.ConvexHull2(seq,cv.CreateMemStorage(),return_points=1)
     retVal.mConvexHull = list(chull)
     retVal.mHullMask = self._getHullMask(chull,retVal.mBoundingBox)
     del chull
     
     moments = cv.Moments(seq)
     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 = cv.GetHuMoments(moments)
     retVal.mMask = self._getMask(seq,retVal.mBoundingBox)
     mask = retVal.mMask
     retVal.mAvgColor = self._getAvg(color.getBitmap(),retVal.mBoundingBox,mask)
     retVal.mAvgColor = retVal.mAvgColor[0:3]
     retVal.mAvgColor = self._getAvg(color.getBitmap(),retVal.mBoundingBox,mask)
     retVal.mAvgColor = retVal.mAvgColor[0:3]
     retVal.mImg = self._getBlobAsImage(seq,retVal.mBoundingBox,color.getBitmap(),mask)
     retVal.mHoleContour = self._getHoles(seq)
     retVal.mAspectRatio = retVal.mMinRectangle[1][0]/retVal.mMinRectangle[1][1]
     bb = retVal.mBoundingBox
     retVal.points.append((bb[0], bb[1]))
     retVal.points.append((bb[0] + bb[2], bb[1]))
     retVal.points.append((bb[0] + bb[2], bb[1] + bb[3]))
     retVal.points.append((bb[0], bb[1] + bb[3]))
     
     return retVal
Beispiel #2
0
    def _extractData(self,seq,color,minsize,maxsize):
        """
        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( seq is None or not len(seq)):
            return None
        area = cv.ContourArea(seq)
        if( area < minsize or area > maxsize):
            return None

        retVal = Blob()
        retVal.image = color 
        retVal.mArea = area
        
        retVal.mMinRectangle = cv.MinAreaRect2(seq)
        bb = cv.BoundingRect(seq)
        retVal.x = bb[0]+(bb[2]/2)
        retVal.y = bb[1]+(bb[3]/2)
        retVal.mPerimeter = cv.ArcLength(seq)
        if( seq is not None):  #KAS 
            retVal.mContour = list(seq)
            #retVal.points = list(seq) KAS 4/30

        # 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 = cv.ConvexHull2(seq,cv.CreateMemStorage(),return_points=1)
        retVal.mConvexHull = list(chull)
        hullMask = self._getHullMask(chull,bb)
        retVal.mHullImg = self._getBlobAsImage(chull,bb,color.getBitmap(),hullMask)
        retVal.mHullMask = Image(hullMask)
        
        del chull
        
        moments = cv.Moments(seq)

        #This is a hack for a python wrapper bug that was missing
        #the constants required from the ctype
        retVal.m00 = area
        try: 
            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
        except:
            retVal.m10 = cv.GetSpatialMoment(moments,1,0)
            retVal.m01 = cv.GetSpatialMoment(moments,0,1)
            retVal.m11 = cv.GetSpatialMoment(moments,1,1)
            retVal.m20 = cv.GetSpatialMoment(moments,2,0)
            retVal.m02 = cv.GetSpatialMoment(moments,0,2)
            retVal.m21 = cv.GetSpatialMoment(moments,2,1)
            retVal.m12 = cv.GetSpatialMoment(moments,1,2)
            
        retVal.mHu = cv.GetHuMoments(moments)
        mask = self._getMask(seq,bb)
        retVal.mMask = Image(mask)

        retVal.mAvgColor = self._getAvg(color.getBitmap(),bb,mask)
        retVal.mAvgColor = retVal.mAvgColor[0:3]
        #retVal.mAvgColor = self._getAvg(color.getBitmap(),retVal.mBoundingBox,mask)
        #retVal.mAvgColor = retVal.mAvgColor[0:3]
        retVal.mImg = self._getBlobAsImage(seq,bb,color.getBitmap(),mask)

        retVal.mHoleContour = self._getHoles(seq)
        retVal.mAspectRatio = retVal.mMinRectangle[1][0]/retVal.mMinRectangle[1][1]

        return retVal
Beispiel #3
0
    def _extractData(self,seq,color,minsize,maxsize):
        """
        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( seq is None or not len(seq)):
            return None
        area = cv.ContourArea(seq)
        if( area < minsize or area > maxsize):
            return None

        retVal = Blob()
        retVal.image = color 
        retVal.mArea = area
        
        retVal.mMinRectangle = cv.MinAreaRect2(seq)
        retVal.mBoundingBox = cv.BoundingRect(seq)
        retVal.x = retVal.mBoundingBox[0]+(retVal.mBoundingBox[2]/2)
        retVal.y = retVal.mBoundingBox[1]+(retVal.mBoundingBox[3]/2)
        retVal.mPerimeter = cv.ArcLength(seq)
        
        if( seq is not None):  #KAS 
            retVal.mContour = list(seq)
            retVal.points = list(seq)

        # so this is a bit hacky.... need to refactor blobs
        xx = retVal.mBoundingBox[0]
        yy = retVal.mBoundingBox[1]
        ww = retVal.mBoundingBox[2]
        hh = retVal.mBoundingBox[3]
        retVal.boundingBox = [(xx,yy),(xx+ww,yy),(xx+ww,yy+hh),(xx,yy+hh)]

        chull = cv.ConvexHull2(seq,cv.CreateMemStorage(),return_points=1)
        retVal.mConvexHull = list(chull)
        hullMask = self._getHullMask(chull,retVal.mBoundingBox)
        retVal.mHullImg = self._getBlobAsImage(chull,retVal.mBoundingBox,color.getBitmap(),hullMask)
        retVal.mHullMask = Image(hullMask)
        
        del chull
        
        moments = cv.Moments(seq)

        #This is a hack for a python wrapper bug that was missing
        #the constants required from the ctype
        retVal.m00 = area
        try: 
            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
        except:
            retVal.m10 = cv.GetSpatialMoment(moments,1,0)
            retVal.m01 = cv.GetSpatialMoment(moments,0,1)
            retVal.m11 = cv.GetSpatialMoment(moments,1,1)
            retVal.m20 = cv.GetSpatialMoment(moments,2,0)
            retVal.m02 = cv.GetSpatialMoment(moments,0,2)
            retVal.m21 = cv.GetSpatialMoment(moments,2,1)
            retVal.m12 = cv.GetSpatialMoment(moments,1,2)
            
        retVal.mHu = cv.GetHuMoments(moments)
        mask = self._getMask(seq,retVal.mBoundingBox)
        retVal.mMask = Image(mask)

        retVal.mAvgColor = self._getAvg(color.getBitmap(),retVal.mBoundingBox,mask)
        retVal.mAvgColor = retVal.mAvgColor[0:3]
        #retVal.mAvgColor = self._getAvg(color.getBitmap(),retVal.mBoundingBox,mask)
        #retVal.mAvgColor = retVal.mAvgColor[0:3]
        retVal.mImg = self._getBlobAsImage(seq,retVal.mBoundingBox,color.getBitmap(),mask)

        retVal.mHoleContour = self._getHoles(seq)
        retVal.mAspectRatio = retVal.mMinRectangle[1][0]/retVal.mMinRectangle[1][1]
    
        
        return retVal