在JavaCV或OPENCV中查找轮廓

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) 有关,但我不明白为什么。 有什么好的想法吗?

嗨,你有找到任何解决方案吗?如果有,请友善地分享一下。谢谢。 - Gum Slashy
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;
}

请问您能否解释一下如何使用上述方法特别是长度来识别多边形形状? - SL_User
这段代码正确吗?因为cvRectangle()方法的参数不正确,对吗? - SL_User

0

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

OpenCV在OpenCV 2.x中引入了更新的C++ API,这个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文档


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接