2017-07-12 53 views
0

我正在开发使用opencv库的OMR scanner android应用程序。 我已经在表格中检测到我的圆圈作为轮廓,现在我想从所有获得的轮廓中获得实心圆轮廓 由于java对opencv的支持非常少,我无法弄清楚任何东西,因此请为其提供一些方法。如何计算轮廓内的非零像素opencv

//paramview is my image  
    Utils.bitmapToMat(paramView, localMat1); 
    Mat localMat2 = new Mat(); 
    double[] lo; 
    Imgproc.GaussianBlur(localMat1, localMat2, new Size(5.0D, 5.0D), 7.0D, 6.5D); 
    Object localObject = new Mat(); 
    Imgproc.cvtColor(localMat2, (Mat)localObject, COLOR_RGB2GRAY); 
    Mat cloneMat= ((Mat) localObject).clone(); 
    localMat2 = localMat1.clone(); 
    bitwise_not(cloneMat,cloneMat); 
    Imgproc.threshold(cloneMat,localMat2,127,255,Imgproc.THRESH_OTSU); 
    Mat thresh=localMat2.clone(); 

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>(); 
    List<MatOfPoint> questions = new ArrayList<MatOfPoint>(); 
    List<MatOfPoint> sorted = new ArrayList<MatOfPoint>(); 

    //All contours detected 
    Mat hierarchy = new Mat(); 
    Imgproc.findContours(localMat2, contours, hierarchy, 
    Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); 

Image of Detected circles here

+0

你可以添加你的代码来找到圈子的轮廓。此外,添加您当前的图像输出和所需的结果。最后,请查看[帮助中心](https://stackoverflow.com/help/mcve)。 –

回答

0

我返工我自己的代码,发现了这个解决方案。希望它可以帮助。

for (int contourIdx = 0; contourIdx < questionSortedR.size(); contourIdx++) { 
     //creating rectangle around identified contour 
     Rect rectCrop = boundingRect(questionSortedR.get(contourIdx)); 
     //creating crop of that contour from actual image 
     Mat imageROI= thresh.submat(rectCrop); 
     //apply countnonzero method to that crop 
     int total = countNonZero(imageROI); 
     double pixel =total/contourArea(questionSortedR.get(contourIdx))*100; 
     //pixel is in percentage of area that is filled 
     if(pixel>=100 && pixel<=130){ 
      //counting filled circles 
      count++; 
     } 

    }