2017-07-12 138 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++; 
     } 

    }