【问题标题】:Incorrect number of Contours on javacv?javacv上的轮廓数不正确?
【发布时间】:2012-07-08 18:59:21
【问题描述】:

我编写了简单的代码来检索图像中的轮廓数并获取图像中的轮廓数。但它总是给出错误的答案。请问有人能解释一下吗?

 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);
         
     }
 } 

这是我使用的示例图片

但代码总是返回输出为 8。请有人解释一下吗?

【问题讨论】:

  • 我在这张图片中得到了 19,使用 Python API。
  • @AbidRahmank 你知道它的含义吗?
  • 我得到了 19 个,因为,一个完整的图像边界,然后是 9 个外部边界和 9 个内部边界的 9 个正方形。如果你反转图像,我想你会得到 18,因为图像边界会消失。访问:opencvpython.blogspot.com/2012/06/…

标签: java opencv javacv


【解决方案1】:

cvSeq.elem_size() 将返回序列元素的大小(以字节为单位)而不是轮廓数。这就是为什么每次输出都是 8 的原因。 请参阅以下链接以获取更多信息。 http://opencv.willowgarage.com/documentation/dynamic_structures.html#cvseq

要查找轮廓数,您可以使用以下 sn-p

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

【讨论】:

    猜你喜欢
    • 2012-07-17
    • 2021-10-29
    • 2014-04-05
    • 2012-07-12
    • 2012-02-03
    • 1970-01-01
    • 2011-06-04
    • 2016-11-20
    • 2015-06-25
    相关资源
    最近更新 更多