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

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

这是原始图片:

结果如下:

如您所见,没有绘制轮廓。

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

提前致谢,

吉尔

最佳答案

findContours倾向于“吞噬”图像(请看那里的第一个音符)

如果需要二进制图像进行进一步处理,则必须对其进行克隆(),然后再将其提供给findContours()

10-08 09:24