如何计算轮廓opencv内的非零像素 [英] how to count non-zero pixels inside a contour opencv

查看:505
本文介绍了如何计算轮廓opencv内的非零像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用opencv库开发OMR扫描仪android应用程序. 我已将工作表内的圆检测为轮廓,现在我要从所有获得的轮廓中获取填充的圆轮廓 由于Java对opencv的支持非常少,因此我一无所知, 请提出一些相同的方法.

I am developing OMR scanner android application using opencv library. I have detected my circles inside the sheet as contours and now I want to get filled circle contours from all the obtains contours Since java support for opencv is very less I couldnt figure out anything, please suggest some method for the same.

    //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);

在此检测到的圆圈的图像

推荐答案

我重新编写了自己的代码,并找到了此解决方案.希望对您有所帮助.

I reworked my own code and found this solution. Hope it might help.

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

    }

这篇关于如何计算轮廓opencv内的非零像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆