Exemplo n.º 1
0
    def fit_to_largest_object(self):
        """
        fit an ellipse to the segmented, cleaned, and selected object
        """
        if self.large_obj is None:
            self.find_largest_object()

        obj_mask = self.large_obj > 0
        h, w = obj_mask.shape
        xcoor, ycoor = xy_features(w,h)
        obj_mask = obj_mask.flatten()
        xcoor = xcoor[obj_mask]
        ycoor = ycoor[obj_mask]
        points = np.concatenate((xcoor, ycoor), axis=1).transpose()
        points = np.matrix(points)

        print "points shape:"
        print points.shape
        
        start = gm.simple_start(1, points)
        gmm = gm.GMM(points, start)
        gmm.fit(iter_limit=self.iter_limit)
        
        #weights = np.matrix(np.ones(points.shape[0]))
        #gauss = pb.Gaussian(0,0,dimension=2)

        self.fg_object_ellipse = gmm2ellipses(gmm)[0] 

        if False:
            tmp = points.astype(np.float32).T
            tmp = np.reshape(tmp, [1, tmp.shape[1], 2])
            cvpoints = ut.np2cv(tmp)
            ellipse = cv.cvFitEllipse2(cvpoints)
            self.fg_object_ellipse = ut.Ellipse()
            self.fg_object_ellipse.set_from_cvbox2d(ellipse)
Exemplo n.º 2
0
    def fit_to_largest_object(self):
        """
        fit an ellipse to the segmented, cleaned, and selected object
        """
        if self.large_obj is None:
            self.find_largest_object()

        obj_mask = self.large_obj > 0
        h, w = obj_mask.shape
        xcoor, ycoor = xy_features(w, h)
        obj_mask = obj_mask.flatten()
        xcoor = xcoor[obj_mask]
        ycoor = ycoor[obj_mask]
        points = np.concatenate((xcoor, ycoor), axis=1).transpose()
        points = np.matrix(points)

        print "points shape:"
        print points.shape

        start = gm.simple_start(1, points)
        gmm = gm.GMM(points, start)
        gmm.fit(iter_limit=self.iter_limit)

        #weights = np.matrix(np.ones(points.shape[0]))
        #gauss = pb.Gaussian(0,0,dimension=2)

        self.fg_object_ellipse = gmm2ellipses(gmm)[0]

        if False:
            tmp = points.astype(np.float32).T
            tmp = np.reshape(tmp, [1, tmp.shape[1], 2])
            cvpoints = ut.np2cv(tmp)
            ellipse = cv.cvFitEllipse2(cvpoints)
            self.fg_object_ellipse = ut.Ellipse()
            self.fg_object_ellipse.set_from_cvbox2d(ellipse)
Exemplo n.º 3
0
def returnEllipses(contours):
	ellipses = []
	for c in contours.hrange():
		count = c.total;
		if( count < 6 ):
			continue;
		PointArray = cv.cvCreateMat(1, count, cv.CV_32SC2)
		PointArray2D32f= cv.cvCreateMat( 1, count, cv.CV_32FC2)
		cv.cvCvtSeqToArray(c, PointArray, cv.cvSlice(0, cv.CV_WHOLE_SEQ_END_INDEX));
		cv.cvConvert( PointArray, PointArray2D32f )
		
		box = cv.CvBox2D()
		box = cv.cvFitEllipse2(PointArray2D32f);
		#cv.cvDrawContours(frame, c, cv.CV_RGB(255,255,255), cv.CV_RGB(255,255,255),0,1,8,cv.cvPoint(0,0));

		center = cv.CvPoint()
		size = cv.CvSize()
		center.x = cv.cvRound(box.center.x);
		center.y = cv.cvRound(box.center.y);
		size.width = cv.cvRound(box.size.width*0.5);
		size.height = cv.cvRound(box.size.height*0.5);
		box.angle = -box.angle;
		ellipses.append({'center':center, 'size':size, 'angle':box.angle})
	return ellipses
Exemplo n.º 4
0
def process_image(slider_pos):
    """
    Define trackbar callback functon. This function find contours,
    draw it and approximate it by ellipses.
    """
    stor = cv.cvCreateMemStorage(0)

    # Threshold the source image. This needful for cv.cvFindContours().
    cv.cvThreshold(image03, image02, slider_pos, 255, cv.CV_THRESH_BINARY)

    # Find all contours.
    nb_contours, cont = cv.cvFindContours(image02, stor, cv.sizeof_CvContour,
                                          cv.CV_RETR_LIST,
                                          cv.CV_CHAIN_APPROX_NONE,
                                          cv.cvPoint(0, 0))

    # Clear images. IPL use.
    cv.cvZero(image02)
    cv.cvZero(image04)

    # This cycle draw all contours and approximate it by ellipses.
    for c in cont.hrange():
        count = c.total
        # This is number point in contour

        # Number point must be more than or equal to 6 (for cv.cvFitEllipse_32f).
        if (count < 6):
            continue

        # Alloc memory for contour point set.
        PointArray = cv.cvCreateMat(1, count, cv.CV_32SC2)
        PointArray2D32f = cv.cvCreateMat(1, count, cv.CV_32FC2)

        # Get contour point set.
        cv.cvCvtSeqToArray(c, PointArray,
                           cv.cvSlice(0, cv.CV_WHOLE_SEQ_END_INDEX))

        # Convert CvPoint set to CvBox2D32f set.
        cv.cvConvert(PointArray, PointArray2D32f)

        box = cv.CvBox2D()

        # Fits ellipse to current contour.
        box = cv.cvFitEllipse2(PointArray2D32f)

        # Draw current contour.
        cv.cvDrawContours(image04, c, cv.CV_RGB(255, 255, 255),
                          cv.CV_RGB(255, 255, 255), 0, 1, 8, cv.cvPoint(0, 0))

        # Convert ellipse data from float to integer representation.
        center = cv.CvPoint()
        size = cv.CvSize()
        center.x = cv.cvRound(box.center.x)
        center.y = cv.cvRound(box.center.y)
        size.width = cv.cvRound(box.size.width * 0.5)
        size.height = cv.cvRound(box.size.height * 0.5)
        box.angle = -box.angle

        # Draw ellipse.
        cv.cvEllipse(image04, center, size, box.angle, 0, 360,
                     cv.CV_RGB(0, 0, 255), 1, cv.CV_AA, 0)

    # Show image. HighGUI use.
    highgui.cvShowImage("Result", image04)
Exemplo n.º 5
0
def process_image( slider_pos ): 
    """
    Define trackbar callback functon. This function find contours,
    draw it and approximate it by ellipses.
    """
    stor = cv.cvCreateMemStorage(0);
    
    # Threshold the source image. This needful for cv.cvFindContours().
    cv.cvThreshold( image03, image02, slider_pos, 255, cv.CV_THRESH_BINARY );
    
    # Find all contours.
    nb_contours, cont = cv.cvFindContours (image02,
            stor,
            cv.sizeof_CvContour,
            cv.CV_RETR_LIST,
            cv.CV_CHAIN_APPROX_NONE,
            cv.cvPoint (0,0))
    
    # Clear images. IPL use.
    cv.cvZero(image02);
    cv.cvZero(image04);
    
    # This cycle draw all contours and approximate it by ellipses.
    for c in cont.hrange():
        count = c.total; # This is number point in contour

        # Number point must be more than or equal to 6 (for cv.cvFitEllipse_32f).        
        if( count < 6 ):
            continue;
        
        # Alloc memory for contour point set.    
        PointArray = cv.cvCreateMat(1, count, cv.CV_32SC2)
        PointArray2D32f= cv.cvCreateMat( 1, count, cv.CV_32FC2)
        
        # Get contour point set.
        cv.cvCvtSeqToArray(c, PointArray, cv.cvSlice(0, cv.CV_WHOLE_SEQ_END_INDEX));
        
        # Convert CvPoint set to CvBox2D32f set.
        cv.cvConvert( PointArray, PointArray2D32f )
        
        box = cv.CvBox2D()

        # Fits ellipse to current contour.
        box = cv.cvFitEllipse2(PointArray2D32f);
        
        # Draw current contour.
        cv.cvDrawContours(image04, c, cv.CV_RGB(255,255,255), cv.CV_RGB(255,255,255),0,1,8,cv.cvPoint(0,0));
        
        # Convert ellipse data from float to integer representation.
        center = cv.CvPoint()
        size = cv.CvSize()
        center.x = cv.cvRound(box.center.x);
        center.y = cv.cvRound(box.center.y);
        size.width = cv.cvRound(box.size.width*0.5);
        size.height = cv.cvRound(box.size.height*0.5);
        box.angle = -box.angle;
        
        # Draw ellipse.
        cv.cvEllipse(image04, center, size,
                  box.angle, 0, 360,
                  cv.CV_RGB(0,0,255), 1, cv.CV_AA, 0);
    
    # Show image. HighGUI use.
    highgui.cvShowImage( "Result", image04 );