cv::RotatedRect 中非零像素的数量
正如标题所说,我试图找到 cv::Mat 的某个区域(即 RotatedRect 内)的非零像素数。
对于常规矩形,可以简单地在 ROI 上使用 countNonZeroPixels。然而 ROI 只能是规则的(非旋转的)矩形。
另一个想法是绘制旋转的矩形并将其用作遮罩。然而 openCV 既不支持旋转矩形的绘制,也不接受 countNonZeroPixels 接受掩码。
有谁有解决方案来优雅地解决这个问题?
谢谢 !
as the title says i'm trying to find the number of non-zero pixels in a certain area of a cv::Mat, namely within a RotatedRect.
For a regular Rect one could simply use countNonZeroPixels on a ROI. However ROIs can only be regular (non rotated) rectangles.
Another idea was to draw the rotated rectangle and use that as a mask. However openCV neither supports the drawing of rotated rectangles nor does countNonZeroPixels accept a mask.
Does anyone have a solution for how to elegantly solve this ?
Thank you !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,这是我的第一次尝试。
这个想法是将图像旋转到与矩形旋转相反的方向,然后在拉直的矩形上应用 roi。
您可以通过在旋转之前应用另一个 roi 来加快速度,以避免旋转整个图像...
Ok, so here's my first take at it.
The idea is to rotate the image reverse to the rectangle's rotation and than apply a roi on the straightened rectangle.
You can probably speed this up by applying another roi before rotation to avoid having to rotate the whole image...
一种完全不同的方法可能是旋转图像(沿相反方向),并仍然将矩形 ROI 与 countNonZeroPixels 结合使用。唯一的问题是您必须围绕 ROI 中心的轴旋转图像...
为了使其更清楚,请参阅随附的示例:
A totally different approach might be to rotate your image (in opposite direction), and still use the rectangular ROI in combination with countNonZeroPixels. The only problem will be that you have to rotate your image around a pivot of the center of the ROI...
To make it clearer, see attached example:
为了避免类似任务中的旋转,我使用这样的函数迭代 RotatedRect 中的每个像素:
它看起来像通常对矩形的迭代,但在每一步中,我们将单位 1px 向量添加到目标方向的当前点。
这个循环不会迭代所有点并跳过一些像素,但这对我的任务来说是可以的。
UPD: 使用 LineIterator 进行迭代要好得多:
To avoid rotation in similar task I iterate over each pixel in RotatedRect with such function:
It's looks like usual iteration over rectangle, but on each step we add unit 1px vector to current point in direction to destination.
This loop NOT iterate over all points and skip a few pixels, but it was okay for my task.
UPD: It much better to use LineIterator to iterate: