对cvFindContours和cvSnakeImage有问题。

时间:2022-12-03 21:55:32

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_LIST , 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: 对cvFindContours和cvSnakeImage有问题。 And here is the result: 对cvFindContours和cvSnakeImage有问题。

这里是原始图像,这里是结果:

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?

我认为问题是我从cvFindContours得到的轮廓很少,有没有降低阈值以得到更多轮廓的方法?

Thanks in advance,

提前谢谢,

Gil.

吉尔。

1 个解决方案

#1


0  

findContours tends to 'eat up' the image ( look at the 1st note there )

findContours倾向于“吃掉”图像(看第一个注释)

if you need the binary image for further processing, you have to clone() it before feeding it into findContours()

如果需要进一步处理的二进制映像,则必须在将其放入findContours()之前克隆()

#1


0  

findContours tends to 'eat up' the image ( look at the 1st note there )

findContours倾向于“吃掉”图像(看第一个注释)

if you need the binary image for further processing, you have to clone() it before feeding it into findContours()

如果需要进一步处理的二进制映像,则必须在将其放入findContours()之前克隆()