4

我有问题,但我不知道是什么!我有下一个代码,当我调试它时,调试器在

IplImage iplGray = cvCreateImage(cvGetSize(iplUltima), 8, 1 );
CvMemStorage g_storage = null;
CvSeq contours = new CvSeq(iplGray);

opencv_imgproc.cvCvtColor(iplUltima, iplGray, opencv_imgproc.CV_BGR2GRAY);
opencv_imgproc.cvThreshold(iplGray, iplGray, 100, 255, opencv_imgproc.CV_THRESH_BINARY);

//HERE, the next line:
opencv_imgproc.cvFindContours(iplGray, g_storage, contours, CV_C, CV_C, CV_C);
cvZero(iplGray);
if(contours != null){
    opencv_core.cvDrawContours(iplGray, contours, CvScalar.ONE, CvScalar.ONE, CV_C, CV_C, CV_C);             
}
cvShowImage( "Contours", iplGray );

我认为它与 CvSeq contours = new CvSeq(iplGray); 但我不明白为什么。有什么有用的想法吗?

4

2 回答 2

4

对于轮廓检测,我使用了这种方法。它执行得很好。

public static IplImage detectObjects(IplImage srcImage){

    IplImage resultImage = cvCloneImage(srcImage);

    CvMemStorage mem = CvMemStorage.create();
    CvSeq contours = new CvSeq();
    CvSeq ptr = new CvSeq();

    cvFindContours(srcImage, mem, contours, Loader.sizeof(CvContour.class) , CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));

    CvRect boundbox;

    for (ptr = contours; ptr != null; ptr = ptr.h_next()) {
        boundbox = cvBoundingRect(ptr, 0);

            cvRectangle( resultImage , cvPoint( boundbox.x(), boundbox.y() ), 
                cvPoint( boundbox.x() + boundbox.width(), boundbox.y() + boundbox.height()),
                cvScalar( 0, 255, 0, 0 ), 1, 0, 0 );
    }

    return resultImage;
}
于 2012-05-25T08:03:40.610 回答
0

这里的默认示例和另一个答案使用类似于旧 OpenCV 1.x C API 的语法(以 cv* 为前缀的函数和类)。

OpenCV 在 OpenCV 2.x 中引入了更新的 C++ API,它更加简单易懂。JavaCV 在其最新版本中也添加了这种语法。

对于想要使用更新语法(类似于 OpenCV C++ API)的人,这里是用于轮廓检测的 JavaCV 片段 - (使用 JavaCV 1.3.2)

Mat img = imread("/path/to/image.jpg",CV_LOAD_IMAGE_GRAYSCALE);

MatVector result = new MatVector(); // MatVector is a JavaCV list of Mats

findContours(img, result, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);

// The contours are now available in "result"

// You can access them using result.get(index), check the docs linked below for more info

MatVector 文档

于 2017-05-09T08:24:46.493 回答