问题
I have a problem but I don't know what! I have the next code, and when I debug it, the debugger stops in the
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 );
I think it is related with CvSeq contours = new CvSeq(iplGray); but I don't understand why. Any helpful idea?
回答1:
For Contours Detection, I have used this method. It is performed well.
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;
}
回答2:
The default examples and another answer here use the syntax which is similar to the old OpenCV 1.x C API(functions and classes prefixed by cv*).
OpenCV introduced a newer C++ API in OpenCV 2.x, which is much simpler and easier to understand. JavaCV has also added this syntax in their recent versions.
For people who want to use the newer syntax(similar to the OpenCV C++ API), here is a JavaCV snippet for Contour Detection - (uses 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 documentation
来源:https://stackoverflow.com/questions/10668573/find-contours-in-javacv-or-opencv