opencv/javacv: How to iterate over contours for shape identification?

前端 未结 3 782

I\'m developing a shape identification project using JavaCV and I have found some OpenCV code to identify U shapes in a particular image. I have tried to convert it into JavaCV

3条回答
  •  深忆病人
    2021-02-08 17:48

    EDIT: Here is the most interesting finding - I think you are not iterating correctly through the contours - you should do something like:

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

    Or:

    // ... 
    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
    }
    

    First, I think pst is right regarding the calculation of the ratio - you have to cast the width to float.

    Secondly, I see that when you are making the gray image in python you use COLOR_BGR2GRAY and in java you are using CV_RGB2GRAY that could lead to a totally different gray picture. I would add some debug steps on both programs to save the temp gray images and compare them as also print outs for the values of x,y,w and h when (10 < (w/h) || (w/h) < 0.1) is true.

    Another thing is that in the java solution you use CV_RETR_CCOMP to get the contours and in the python solution you use CV_RETR_LIST according to the documentation:

    CV_RETR_LIST retrieves all of the contours without establishing any hierarchical relationships CV_RETR_CCOMP retrieves all of the contours and organizes them into a two-level hierarchy: on the top level are the external boundaries of the components, on the second level are the boundaries of the holes. If inside a hole of a connected component there is another contour, it will still be put on the top level

    So first I would double check that all cv's parameters in both programs are the same, then I would add debug steps to see that the intermediate variables contains the same data.

提交回复
热议问题