遇到cvFindContours和cvSnakeImage问题

时间:2014-01-05 21:23:10

标签: opencv image-processing

我正在使用以下代码查找轮廓并运行snake算法:

#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;
}

这是原始图片: enter image description here 这是结果: enter image description here

如您所见,没有画出轮廓。

我认为问题在于我从cvFindContours得到的轮廓非常少,是否有任何降低阈值以获得更多轮廓的方法?

提前致谢,

吉尔。

1 个答案:

答案 0 :(得分:0)

findContours倾向于“吃掉”图像(看那里的第一个音符)

如果你需要二进制图像进行进一步处理,你必须克隆()它才能将它送入findContours()