【问题标题】:how to count the pixels in roi opencv如何计算 roi opencv 中的像素
【发布时间】:2015-05-29 03:17:57
【问题描述】:

我有一张经过裁剪的硬币图像。我已经应用了面具,所以我可以专注于硬币本身。接下来是我要计算这枚硬币的像素数。我已经阅读过类似的帖子,但它们似乎对我不起作用。

这是原图: http://s30.postimg.org/eeh3lp99d/IMG_20150414_121300.jpg

和裁剪的硬币: http://s3.postimg.org/4k2pdst73/cropped.png

到目前为止,这是我的代码:

//get the number of pixels of the coin 
//STEP 1: CROP THE COIN 
//get the Rect containing he circl 
Rect rectCircle(center.x - radius, center.y - radius, radius * 2, radius * 2); //obtain the image ROI: 
Mat roi(src_gray, rectCircle); 
//make a black mask, same size: 
Mat mask(roi.size(), roi.type(), Scalar::all(0)); 
//with a white,filled circle in it: 
circle(mask, Point(radius, radius), radius, Scalar::all(255), -1); 
//combine roi and mask: 
cv::Mat coin_cropped = roi & mask;

如何计算裁剪硬币的像素数?

【问题讨论】:

  • 记得贴出你目前的代码
  • 这是我目前所拥有的......//get the number of pixels of the coin //STEP 1: CROP THE COIN //get the Rect containing he circl Rect rectCircle(center.x - radius, center.y - radius, radius * 2, radius * 2); //obtain the image ROI: Mat roi(src_gray, rectCircle); //make a black mask, same size: Mat mask(roi.size(), roi.type(), Scalar::all(0)); //with a white,filled circle in it: circle(mask, Point(radius, radius), radius, Scalar::all(255), -1); //combine roi and mask: cv::Mat coin_cropped = roi & mask;
  • 在你的面具上使用 cv::countNonZero 函数
  • @Micka,我在哪里插入 cv::countNonZero?太感谢了。我真是个菜鸟。

标签: opencv pixels image-size roi


【解决方案1】:

你需要使用countnonzero

countNonZero

计算非零数组元素。

C++: int countNonZero(InputArray src)

在 ROI 矩阵上使用它,它将返回一个像素数的整数,在您的代码中它看起来像这样:

numberOfPixelsInMask = cv::countNonZero(mask);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多