简体   繁体   English

如何计算OpenCV中每个圆圈的像素数?

[英]How to count the number of pixels in each circle in OpenCV?

I currently have a source image of: Source image 我目前有以下来源图片: 源图片

And I have made a bounding rects for each as an ROI: Rects 我已经将每个区域的边界矩形作为ROI: Rects

My goal is to find the number of red pixels inside each rect. 我的目标是找到每个矩形内红色像素的数量。 However, I do not know how to proceed. 但是,我不知道如何进行。 I have used the area (30*30) and countNonZero for finding the number of pixels for each circle by individually manually cropping and saving as a separate image. 我已经使用区域(30 * 30)和countNonZero通过分别手动裁剪并另存为单独的图像来查找每个圆圈的像素数。 However I want to implement it in the whole image where I could just iterate through the bounding rects. 但是,我想在整个图像中实现它,我可以在边界矩形中进行迭代。

Edit: If it will help, this is the code that I use to get the bounding rects. 编辑:如果有帮助,这是我用来获取边界矩形的代码。

for (int i = 0; i < contours.size(); i++)
    {
        approxPolyDP(Mat(contours[i]), contours_poly[i], 0.1, true);
        //Get the width and heights of the bounding rectangles
        int w = boundingRect(Mat(contours[i])).width;
        int h = boundingRect(Mat(contours[i])).height;
        //Apply aspect ratio for filtering rects (optional)
        double ar = (double)w / h;
        //Apply a bounding Rects/Circles


        //Rect/contour filter optional
        if (hierarchy[i][3] == -1) //No parent
            if ((w >= 28 && w <= 32) && (h >= 28 && h <= 32) && ar < 1.1 && ar > 0.9) {
                //Apply a bounding Rects/Circles
                boundRect[i] = boundingRect(Mat(contours_poly[i]));
                minEnclosingCircle((Mat)contours_poly[i], center[i], radius[i]);
                //Add to a new 
                filtered_contours.push_back(contours_poly[i]);

                std::cout << i << " w: " << w << " h: " << h << std::endl;
            }

    }

Maybe, this helps you. 也许这对您有帮助。

// Load image.
cv::Mat circles = cv::imread("circles.jpg", cv::IMREAD_GRAYSCALE);

// Use simple threshold to get rid of compression artifacts.
cv::Mat circlesThr;
cv::threshold(circles, circlesThr, 128, 255, cv::THRESH_BINARY_INV);

// Find contours in binary image (cv::RETR_EXTERNAL -> only most outer contours).
std::vector<std::vector<cv::Point>> contours;
std::vector<cv::Vec4i> hierarchy;
cv::findContours(circlesThr, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE);

// Iterate all contours...
// Iterate all contours...
for (std::vector<cv::Point>& contour : contours)
{
    // Determine bounding rectangle of contour.
    cv::Rect rect = cv::boundingRect(contour);

    // Count non-zero pixels within bounding rect.
    std::string count = std::to_string(cv::countNonZero(circlesThr(rect)));

    // Output text to image.
    cv::putText(circlesThr, count, cv::Point(rect.x - 5, rect.y - 5), cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(255));
}

// Save output image.
cv::imwrite("output.jpg", circlesThr);

Results in: 结果是:

输出图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM