2012-07-09 3 views
5

이미지의 윤곽선 수를 검색하고 윤곽선 수를 얻는 간단한 코드를 작성했습니다. 하지만 항상 잘못된 대답을합니다. 이 문제에 대해 설명해 줄 수 있습니까?javacv의 윤곽선 수가 잘못 되었습니까?

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

    } 
} 

이것은 내가

enter image description here

를 사용하는 샘플 이미지입니다 그러나 코드는 항상 8로 출력 누군가가 이것을 설명 할 수 제발 반환?

+0

나는이 이미지에서 파이썬 API를 사용하여 19 점을 얻는다. –

+0

@AbidRahmank 그 의미에 대해 알고 싶습니까? –

+1

나는 9 개의 외부 경계와 9 개의 내부 경계를 가지고 있기 때문에 19 개를 얻었습니다. 당신이 이미지를 뒤집 으면 이미지의 경계가 사라지기 때문에 18이 될 것이라고 생각합니다. 방문 : http://opencvpython.blogspot.com/2012/06/hi-this-article-is-tutorial-which-try.html –

답변

1

cvSeq.elem_size()는 윤곽 수가 아닌 바이트 단위의 시퀀스 요소 크기를 반환합니다. 이것이 매회 출력이 8 인 이유입니다. 자세한 내용은 다음 링크를 참조하십시오. 당신이 조각을 이미지에 이미지 경계를 당신이 CV_RETR_EXTERNAL는 외부 윤곽을 제공 할 것입니다 제공 한 매개 변수와

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

에 따라 사용할 수 있습니다 윤곽의 수를 찾는 http://opencv.willowgarage.com/documentation/dynamic_structures.html#cvseq

(이미지를 반전되지 않습니다 제공). CV_RETR_LIST를 사용하여 모든 윤곽선을 얻을 수 있습니다. 매개 변수에 대한 자세한 내용은 다음 링크를 방문하십시오. http://opencv.willowgarage.com/documentation/structural_analysis_and_shape_descriptors.html#findcontours

관련 문제