Ask Your Question
1

Having troubles with cvFindContours and cvSnakeImage

asked 2014-01-05 18:05:03 -0600

I'm using the following code to find contours and running the snake algorith:

#include "cv.h" 
#include "opencv2\objdetect\objdetect.hpp"
#include "opencv2\core\core.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\features2d\features2d.hpp"
#include "opencv2\calib3d\calib3d.hpp"
#include "opencv2\nonfree\nonfree.hpp"
#include "opencv2\legacy\legacy.hpp"
#include "highgui.h" 
using namespace cv;

void readme();

/** @function main */
Mat src_gray;
int thresh = 20;
int max_thresh = 255;
RNG rng(12345);
int ialpha = 20;
int ibeta=20; 
int igamma=20; 
IplImage *image = 0 ;

/// Function header
void thresh_callback(int, void* );
int main() 
{ 
    for (int fcount=1;fcount<52;fcount++){
        if(image) cvReleaseImage(&image);
        char filename[256];
        sprintf(filename,"C:\\OIM\\PersonDetectionResults\\original_frames\\image%d.jpg",fcount);

        image=cvLoadImage(filename);
        IplImage *im_gray = cvCreateImage(cvGetSize(image),IPL_DEPTH_8U,1);
        cvCvtColor(image,im_gray,CV_RGB2GRAY);

        // Do some Edge detection
        IplImage* out = cvCreateImage(cvGetSize(im_gray), IPL_DEPTH_8U, 1); 
        cvCanny(im_gray, out, 10, 20, 3);

        /*cvNamedWindow( "Image", CV_WINDOW_AUTOSIZE );
        cvShowImage("Image", out);
        cvWaitKey(0);*/


        IplImage *original_image=cvCloneImage(im_gray);
        /// Find contours
        CvSeq* contours = 0;
        CvMemStorage* storage = cvCreateMemStorage(0);

        cvFindContours( out, storage, &contours, sizeof(CvContour), 
            CV_RETR_EXTERNAL , CV_CHAIN_APPROX_SIMPLE );

        if(!contours) return -1 ; 
        int length = contours->total;   
        if(length<3) continue ; 
        CvPoint* point = new CvPoint[length]; 

        CvSeqReader reader;
        CvPoint pt= cvPoint(0,0);;  
        CvSeq *contour2=contours;   

        cvStartReadSeq(contour2, &reader);
        for (int i = 0; i < length; i++)
        {
            CV_READ_SEQ_ELEM(pt, reader);
            point[i]=pt;
        }
        cvReleaseMemStorage(&storage);


        float alpha=ialpha/100.0f; 
        float beta=ibeta/100.0f; 
        float gamma=igamma/100.0f; 


        CvSize size; 
        size.width=3; 
        size.height=3; 
        CvTermCriteria criteria; 
        criteria.type=CV_TERMCRIT_ITER; 
        criteria.max_iter=1000; 
        criteria.epsilon=0.1; 
        cvSnakeImage( out, point,length,&alpha,&beta,&gamma,CV_VALUE,size,criteria,0 ); 

        for(int i=0;i<length;i++)
        {
            int j = (i+1)%length;
            cvLine( original_image, point[i],point[j],CV_RGB( 0, 255, 0 ),1,8,0 ); 
        }
        delete []point;

        sprintf(filename,"C:\\Test\\image%d.jpg",fcount);
        cvSaveImage(filename, &(IplImage(*original_image)));
        /*cvNamedWindow( "Image", CV_WINDOW_AUTOSIZE );
        cvShowImage("Image", im_gray);
        cvWaitKey(0);*/
    }

    return 0;
}

Here is the original image: image description

And here is the output image: image description As you can see, no contour is drawn.

I think that the problem is that I'm getting very few contours from cvFindContours, is there any method of lowering the thresholds to get more contours?

Thanks in advance,

Gil.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-06-12 03:24:53 -0600

sxt gravatar image

use CvInvoke.cvCanny(,,,,) for finding edges in the image. Then apply cvfindcontours over the image containing edge, you can control number of contours or length of contours in the image.

edit flag offensive delete link more

Comments

Thanks for your help!

GilLevi gravatar imageGilLevi ( 2014-06-12 03:30:05 -0600 )edit

Question Tools

Stats

Asked: 2014-01-05 18:05:03 -0600

Seen: 1,889 times

Last updated: Jun 12 '14