Wrong number of loops on javacv?

I have a simple code to extract the number of outlines in an image and get the number of outlines in an image. But he always gives the wrong answer. Can someone explain about this?

 import com.googlecode.javacpp.Loader;
 import com.googlecode.javacv.CanvasFrame;
 import static com.googlecode.javacpp.Loader.*;
 import static com.googlecode.javacv.cpp.opencv_core.*;
 import static com.googlecode.javacv.cpp.opencv_imgproc.*;
 import static com.googlecode.javacv.cpp.opencv_highgui.*;
 import java.io.File;
 import javax.swing.JFileChooser;

 public class TestBeam {
     public static void main(String[] args) {
         CvMemStorage storage=CvMemStorage.create();
         CvSeq squares = new CvContour();
         squares = cvCreateSeq(0, sizeof(CvContour.class), sizeof(CvSeq.class), storage);
         JFileChooser f=new JFileChooser();
         int result=f.showOpenDialog(f);//show dialog box to choose files
             File myfile=null;
             String path="";
         if(result==0){
             myfile=f.getSelectedFile();//selected file taken to myfile
             path=myfile.getAbsolutePath();//get the path of the file
         }
         IplImage src = cvLoadImage(path);//hear path is actual path to image
         IplImage grayImage    = IplImage.create(src.width(), src.height(), IPL_DEPTH_8U, 1);
         cvCvtColor(src, grayImage, CV_RGB2GRAY);
         cvThreshold(grayImage, grayImage, 127, 255, CV_THRESH_BINARY);
         CvSeq cvSeq=new CvSeq();
         CvMemStorage memory=CvMemStorage.create();
         cvFindContours(grayImage, memory, cvSeq, Loader.sizeof(CvContour.class), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
         System.out.println(cvSeq.elem_size());
         CanvasFrame cnvs=new CanvasFrame("Beam");
         cnvs.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
         cnvs.showImage(src);
         //cvShowImage("Final ", src);

     }
 } 

This is the image of the sample that I used.

enter image description here

But the code always returns the result as 8. Please, can anyone explain this?

+5
source share
1 answer

cvSeq.elem_size () will return the size of the sequence element in bytes, not the number of contours. That is why output 8. each time. For more information, see the following link. http://opencv.willowgarage.com/documentation/dynamic_structures.html#cvseq

int i = 0;
while(cvSeq != null){
i = i + 1;
cvSeq = cvSeq.h_next();
}
System.out.println(i);

, CV_RETR_EXTERNAL, , ( , ). CV_RETR_LIST, . . http://opencv.willowgarage.com/documentation/structural_analysis_and_shape_descriptors.html#findcontours

+1

All Articles