Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javacv:在Java中迭代CvSeq_Java_Opencv_Javacv - Fatal编程技术网

javacv:在Java中迭代CvSeq

javacv:在Java中迭代CvSeq,java,opencv,javacv,Java,Opencv,Javacv,我使用: cvFindContours(灰色、mem、等高线、加载器尺寸(CvContour.class)、CV_RETR_CCOMP、CV_CHAIN_近似值、cvPoint(0,0)); 因此我有CvSeq等高线进行迭代(据我所知)。 所以我这样使用它: if(contours!=null) { for (ptr = contours; ptr != null; ptr = ptr.h_next()) { //..do sth with ptr } } 它是有效的,但我

我使用:


cvFindContours(灰色、mem、等高线、加载器尺寸(CvContour.class)、CV_RETR_CCOMP、CV_CHAIN_近似值、cvPoint(0,0));


因此我有
CvSeq等高线
进行迭代(据我所知)。
所以我这样使用它:

if(contours!=null) {
  for (ptr = contours; ptr != null; ptr = ptr.h_next()) {
    //..do sth with ptr
  }
}
它是有效的,但我时不时(经常)得到:

Exception in thread "Thread-3" java.lang.NullPointerException: This pointer address is NULL.
at com.googlecode.javacv.cpp.opencv_core$CvSeq.h_next(Native Method)
at pl..filter(FullFilter.java:69)
at pl..Window$1.run(Window.java:41)
at java.lang.Thread.run(Unknown Source)

引发异常的行是带有ptr.h_next()的行。
我试图检查空值,但无效:

System.out.println("PTR="+ptr); // it's not null here!
if(ptr.h_next()==null) //exception in this line!
    System.out.println("NULL");
System.out.println(ptr.h_next());
第一行显示:
PTR=com.googlecode.javacv.cpp.opencv_core$CvSeq[address=0x0,position=0,limit=1,capacity=1,deallocator=com.googlecode.javacpp.Pointer$NativeDeallocator@66d53ea4]

我还尝试调用了
轮廓.total()
,但它总是抛出相同的异常。
那么,在Java中使用类似C的序列的正确方法是什么呢

编辑: 我的完整方法:

    public IplImage filter(IplImage image) {
        IplConvKernel kernel = cvCreateStructuringElementEx(2,2,1,1,CV_SHAPE_RECT, null);
        cvDilate(image, image, kernel, 1);
        kernel = cvCreateStructuringElementEx(5,5,2,2,CV_SHAPE_RECT, null);
        cvErode(image, image, kernel, 1);
        IplImage resultImage = cvCloneImage(image);
        IplImage gray = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U, 1); 
        cvCvtColor(image, gray, CV_BGR2GRAY);
        CvMemStorage mem = CvMemStorage.create();
        CvSeq contours = new CvSeq();
        CvSeq ptr = new CvSeq();
        cvThreshold(gray, gray, 20, 255, opencv_imgproc.CV_THRESH_BINARY);
        double thresh = 20;
        Canny( gray, gray, thresh, thresh*2, 3 ,true);
        cvFindContours(gray, mem, contours, Loader.sizeof(CvContour.class) , CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));

        int i=0;
        CvRect boundbox;
        if(contours!=null) {
            for (ptr = contours; ptr != null; ptr = ptr.h_next()) { //EXCEPTION HERE!
                System.out.println((i++)+"\t"+ptr);
                cvDrawContours( resultImage, ptr, CvScalar.BLUE, CvScalar.RED, -1, 3, 8, cvPoint(0,0) );
                System.out.println("PTR="+ptr);
            }

        }       
        return resultImage;
    }
它在一段时间内工作正常,但突然(可能是在没有找到轮廓时?)结束时出现以下异常:

Exception in thread "Thread-3" java.lang.NullPointerException: This pointer address is NULL.
    at com.googlecode.javacv.cpp.opencv_core$CvSeq.h_next(Native Method)
    at pl.kasprowski.eyetracker.FullFilter2.filter(FullFilter2.java:39)
    at pl.kasprowski.eyetracker.Window$1.run(Window.java:42)
    at java.lang.Thread.run(Unknown Source)
我直接用相机拍摄的图像(每秒一次)给这个方法输入信息

编辑:


在一些实验之后,当我像上面那样调用
cvFindContours
时,我会得到一个
contour
对象,该对象不是空的,但调用任何方法,如
contour.h_next()
contour.total()
会导致上述异常。有什么不对劲?或者-如何检查
轮廓
对象是否正常?!当然,我可以捕获
NullPointerException
,但我认为这不是解决问题的正确方法…

尝试使用
cvgetsequelm(轮廓,I)

例如:

for (int i = 0; i < contours.total(); i++) {
   CvRect rect = cvBoundingRect(cvGetSeqElem(contours, i),0);
    //....... Your code ....//

}
for(int i=0;i
问题已解决。
我增加了附加条件。而不是:

if(contours!=null) {
我写

if(contours!=null && !contours.isNull()) {

它是有效的。我不明白为什么有必要这样做,但我认为这与Java C语义鸿沟有关。

您好,您能告诉我们完整的“run”函数,它到底在做什么,以及异常行号吗。?因为你所做的是正确的。可能是其他原因导致了这个问题。我添加了完整的代码。使用时:
CvRect rect=cvBoundingRect(cvgetsekelem(contours,j),0)我得到:
类型opencv\u imgproc中的方法cvBoundingRect(opencv\u core.CvArr,int)不适用于参数(指针,int)