在OpenCV

时间:2016-03-24 15:21:15

标签: c++ opencv area roi

我想在实际ROI中计算检测到的物体的面积(我使用的那个蓝色标记)。我的意思是这两个矩形中的一个是我在阈值图像中感兴趣的区域(黑色和白色)。

如何计算对象的面积(我认为 - &#34;已识别&#34;像素簇,白色)内部投资回报率 < / p>

enter image description here

我只想考虑混凝土投资回报率中的那些像素(在下面的例子中 - 左边的)。因此,超出ROI的所有像素都不会被计算在内。

enter image description here

投资回报率是这样创建的:

rectangle( imgOriginal, Point( 20, 100 ), Point( 170, 250), Scalar( 0, 0, 255 ), +5, 4 );
rectangle( imgThresholded, Point( 20, 100 ), Point( 170, 250), Scalar( 255, 255, 255 ), +5, 4 );
rectangle( imgOriginal, Point( 450, 100 ), Point( 600, 250), Scalar( 0, 0, 255 ), +5, 4 );
rectangle( imgThresholded, Point( 450, 100 ), Point( 600, 250), Scalar( 255, 255, 255 ), +5, 4 );

3 个答案:

答案 0 :(得分:3)

您可以使用cv::countNonZero函数计算imgThresholded图像中ROI内部的非零像素。这正是您所需要的。

cv::Rect leftROI(cv::Point(20, 100), cv::Point(170, 250));
int leftArea = cv::countNonZero(imgThresholded(leftROI));

答案 1 :(得分:0)

从技术上讲,你找到了最大的轮廓&#34;基于一些颜色过滤,在它周围画了一个矩形,现在尝试获得它的区域。为了得到它的区域,OpenCv为你提供了这个功能(它是如何发现它最大的?)所以这里是函数:double cv::contourArea(InputArray contour)

Here是主要页面,因为我认为您可以按原样发送轮廓,但我不确定,但请查看此基本示例并:

vector<Point> contour;
contour.push_back(Point2f(0, 0)); // push whatever points you want
contour.push_back(Point2f(10, 0));
contour.push_back(Point2f(10, 10));
contour.push_back(Point2f(5, 4));
double area0 = contourArea(contour); // retrieve the area

vector<Point> approx;
approxPolyDP(contour, approx, 5, true);
double area1 = contourArea(approx);
cout << "area0 =" << area0 << endl <<
        "area1 =" << area1 << endl <<
        "approx poly vertices" << approx.size() << endl;

答案 2 :(得分:0)

对于二进制图像上的特定ROI,扫描像素值并计算白色(255)。如果您有更多的投资回报率,并希望避免那些不包含白色像素的投资回报率,那么只需跳过那些不包含任何白色像素的投资回报率......