2012-07-09 289 views
9

我正在开发一个使用JavaCV的形状识别项目,我发现了一些OpenCV代码来识别特定图像中的U形状。我试图将它转换成JavaCV,但它不提供相同的输出。你能帮我把这个OpenCV代码转换成JavaCV吗?opencv/javacv:如何迭代轮廓以进行形状识别?

这是OpenCV的代码:

import cv2 
import numpy as np 

img = cv2.imread('sofud.jpg') 
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 
ret,thresh = cv2.threshold(gray,127,255,1) 
contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) 

for cnt in contours: 
    x,y,w,h = cv2.boundingRect(cnt) 
    if 10 < w/float(h) or w/float(h) < 0.1: 
     cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2) 

cv2.imshow('res',img) 
cv2.waitKey(0) 
cv2.destroyAllWindows() 

这是预期的输出

enter image description here

这是转换后的代码:

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_CCOMP, CV_CHAIN_APPROX_SIMPLE); 
     System.out.println(cvSeq.total()); 
     for (int i = 0; i < cvSeq.total(); i++) { 
      CvRect rect=cvBoundingRect(cvSeq, i); 
      int x=rect.x(),y=rect.y(),h=rect.height(),w=rect.width(); 
      if (10 < (w/h) || (w/h) < 0.1){ 
       cvRectangle(src, cvPoint(x, y), cvPoint(x+w, y+h), CvScalar.RED, 1, CV_AA, 0); 
       //cvSeqPush(squares, rect); 
      } 
     } 
     CanvasFrame cnvs=new CanvasFrame("Beam"); 
     cnvs.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); 
     cnvs.showImage(src); 
     //cvShowImage("Final ", src); 

    } 
} 

这是输出,我得到。请有人可以帮我解决这个问题吗?

enter image description here

+0

我没有看到任何C++,所以我删除了标签。我假设第一个例子是Python。 – 2012-07-09 03:13:59

+1

我对这个问题有一个小问题。请在执行“cvFindContours()”方法后解释一下“cvSeq.total()”方法的值吗? – 2012-07-09 05:17:23

回答

5

编辑:这是最有趣的发现 - 我想你不通过轮廓正确地迭代 - 你应该这样做:

CvRect rect = cvBoundingRect(cvGetSeqElem(cvSeq, i),0); //python default? 

或者:

// ... 
CvSeq contours = new CvSeq(); 
CvSeq ptr = new CvSeq(); 
CvRect rect = null; 
// ... 
cvFindContours(..., contours, ...); 

for (ptr = contours; ptr != null; ptr = ptr.h_next()) { 
    rect = cvBoundingRect(ptr, 0); 
    // ... Draw the box if meets criteria 
} 

首先,我认为pst是正确的关于比率的计算 - 你必须把宽度浮动。

其次,我看到,当你在python中制作灰色图像时,使用COLOR_BGR2GRAY,而在java中,你使用的是CV_RGB2GRAY,这可能会导致完全不同的灰色图片。我会在两个程序中添加一些调试步骤以保存临时灰度图像,并将它们作为x,y,wh的值进行比较,当(10 < (w/h) || (w/h) < 0.1)为真时。

另一件事是,在Java解决方案,您使用CV_RETR_CCOMP获得的轮廓和在Python解决您根据文档中使用CV_RETR_LIST

CV_RETR_LIST检索所有的轮廓而没有建立任何 层次关系CV_RETR_CCOMP检索所有轮廓 并将它们组织为两级层次结构:顶层为 组件的外部边界,第二层为 孔的边界。如果连接的组件 的孔里面还有另外一个轮廓,它仍然会在顶层放

所以首先我会仔细检查所有的品种在这两个方案的参数是相同的,那么我想补充调试步骤以查看中间变量包含相同的数据。

+0

在javacv cvBoundingRect()中我们不能传递指针作为参数,因为它需要CvArr,所以我认为它不能成为问题。 – 2012-07-13 03:23:03

+0

@GumSlashy - 看我的编辑。根据javacv的README(http://code.google.com/p/javacv/source/browse/README.txt),这是迭代轮廓的正确方法。我还在SO http://stackoverflow.com/questions/9648482/ocr-with-javacv中发现了这个问题,它使用相同的方法获取轮廓,并绘制边界框。 – zenpoy 2012-07-13 09:31:06

+0

我尝试把cvBoundingRect(cvGetSeqElem(cvSeq,i),0);在这个问题上,但它并不适用于我,我已经把答案放在了正确的结果上。非常感谢你的答复。 – 2012-07-17 12:51:33

3

检查类型促销活动,例如:

if (10 < (w/h) || (w/h) < 0.1){ 

..是非常值得怀疑。要获得浮点数除法,一个(或两个)操作数必须至少为float(同样用于双重除法的double)。否则,在这种情况下,它是一个整数除法。 (请注意,代码具有推广float为好。)

例如:

float ratio = (float)w/h; // (float/int) => (float/float) -> float 
if (10 < ratio || ratio < 0.1) { 

(虽然我不能确定这是否是问题在这里。)

快乐编码!

+1

感谢您的快速回复,我已经尝试了你的sugetion,但它给出了同样的结果。 – 2012-07-09 03:36:19

+0

我认为这将有助于更新您的代码。那里可能还有一个整数部分? – Noremac 2012-07-12 20:50:16

+1

我已经提出了答案,并且我也尝试将w/h投入浮动值,但它没有产生显着的效果。但其确实在一些其他情况下可能会产生重大影响。非常感谢您的回复。 – 2012-07-17 12:47:46

2

此代码适用于我,我只是把cvSeq = cvSeq.h_next();输入到程序中,并删除for循环的for循环add。

package Beam; 
    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 TestBeam2 { 
     public static void main(String[] args) { 
      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_LIST, CV_CHAIN_APPROX_SIMPLE); 

      while (cvSeq != null && !cvSeq.isNull()) { 
       CvRect rect=cvBoundingRect(cvSeq, 0); 
       int x=rect.x(),y=rect.y(),h=rect.height(),w=rect.width(); 
       if (10 < w/h || w/h < 0.1){ 
        cvRectangle(src, cvPoint(x, y), cvPoint(x+w, y+h), CvScalar.RED, 1, CV_AA, 0); 
       } 
       cvSeq=cvSeq.h_next(); 
      } 
      CanvasFrame cnvs=new CanvasFrame("Beam"); 
      cnvs.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); 
      cnvs.showImage(src); 
      //cvShowImage("Final ", src); 
     } 
    } 
+2

这与我前几天提出的方法完全相同 - 这太糟糕了,您没有尝试过:'// ... CvSeq轮廓= new CvSeq(); CvSeq ptr = new CvSeq(); CvRect rect = null; // ... cvFindContours(...,contour,...); (ptr =等高线; ptr!= null; ptr = ptr.h_next()){ rect = cvBoundingRect(ptr,0); // ...如果符合条件,请画框 } – zenpoy 2012-07-17 13:26:14