Java 如何计算轮廓opencv中的非零像素

Java 如何计算轮廓opencv中的非零像素,java,android,opencv,Java,Android,Opencv,我正在使用opencv库开发OMRscanner android应用程序。 我已经检测到我的圆在表内作为轮廓,现在我想从所有的轮廓中得到填充的圆轮廓 由于java对opencv的支持非常少,我想不出什么, 请建议一些同样的方法 //paramview is my image Utils.bitmapToMat(paramView, localMat1); Mat localMat2 = new Mat(); double[] lo; Imgpro

我正在使用
opencv
库开发
OMR
scanner android应用程序。 我已经检测到我的圆在表内作为轮廓,现在我想从所有的轮廓中得到填充的圆轮廓 由于java对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);
//paramview是我的图像
bitmapToMat(paramView,localMat1);
Mat localMat2=新Mat();
双[]低;
Imgproc.GaussianBlur(localMat1,localMat2,新尺寸(5.0D,5.0D),7.0D,6.5D);
Object localObject=new Mat();
cvtColor(localMat2,(Mat)localObject,COLOR_RGB2GRAY);
Mat cloneMat=((Mat)localObject).clone();
localMat2=localMat1.clone();
按位_not(cloneMat,cloneMat);
Imgproc.阈值(cloneMat,LocalMat2127255,Imgproc.THRESH_OTSU);
Mat thresh=localMat2.clone();
列表等高线=新的ArrayList();
列出问题=新建ArrayList();
列表排序=新建ArrayList();
//检测到所有轮廓
Mat层次结构=新Mat();
Imgproc.findContours(localMat2、等高线、层次结构、,
Imgproc.RETR\u外部,Imgproc.CHAIN\u近似值\u简单值);

我重新编写了自己的代码,找到了这个解决方案。希望能有所帮助

 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++;
        }

    }
for(int-contourIdx=0;contourIdx如果(pixel>=100&&pixel我提出了一个替代公认答案的方法:不计算边界矩形内的像素数,而是将轮廓画成一个遮罩,然后遮罩原始图像并计算其中的像素数。我计算的是白色背景上的黑色像素数,其中轮廓在边缘保留了几个像素,因此您的里程数可能会有所不同。H以下是我用Python编写的代码:

mask=np.zero(bw_image.shape,np.uint8)
等高线图(遮罩,[等高线],0,255,-1)
反转=cv.按位\u非(bw\u图像)
掩码=cv.按位\u非(cv.按位\u和(反转,反转,掩码=掩码))
#抓取遮罩图像内轮廓
x、 y,w,h=等高线边界矩形(等高线)
像素=遮罩[y:y+h,x:x+w]
#检查黑色是否只是一条线,在这种情况下,白度为1
内核=np.ones((3,3),np.uint8)
放大=cv.放大(像素、内核、迭代次数=1)
白度=净和(放大)/(255*w*h)

您可以添加代码以查找圆的轮廓。另外,添加当前图像输出和所需结果。最后,请查看。