OpenCV 从正方形向量中提取图像的区域
我有一个包含正方形的图像,我需要提取该正方形中包含的区域。 应用 squares.c 脚本(在每个 OpenCV 发行版的示例中可用)后,我获得了一个正方形向量,然后我需要为每个正方形保存一个图像。
用户 karlphillip 建议这样做:
for (size_t x = 0; x < squares.size(); x++)
{
Rect roi(squares[x][0].x, squares[x][0].y,
squares[x][1].x - squares[x][0].x,
squares[x][3].y - squares[x][0].y);
Mat subimage(image, roi);
}
为了为原始图像中检测到的所有正方形生成一个名为子图像的新 Mat
正如 karl 记得的那样,图像中检测到的点可能不代表完美的正方形(如您可以在上图中看到)但我刚刚向您建议的代码假设它们确实如此。
事实上,我收到此错误:
OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width &&
roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height &&
roi.y + roi.height <= m.rows) in Mat, file /usr/include/opencv/cxmat.hpp,
line 187
terminate called after throwing an instance of 'cv::Exception'
what(): /usr/include/opencv/cxmat.hpp:187: error: (-215) 0 <= roi.x &&
0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y &&
0 <= roi.height && roi.y + roi.height <= m.rows in function Mat
Aborted
建议让脚本也接受非完美平方?
I have an image that contains a square, and I need to extract the area contained in that square.
After applying the squares.c script (available in the samples of every OpenCV distribution) I obtain a vector of squares, then I need to save an image for each of them.
The user karlphillip suggested this:
for (size_t x = 0; x < squares.size(); x++)
{
Rect roi(squares[x][0].x, squares[x][0].y,
squares[x][1].x - squares[x][0].x,
squares[x][3].y - squares[x][0].y);
Mat subimage(image, roi);
}
in order to generate a new Mat called subimage for all the squares detected in the original image
As karl remembered me, the points detected in the image may not represent a perfect square (as you can see in the image above) but the code I just suggested to you assumes they do.
In fact I get this error:
OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width &&
roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height &&
roi.y + roi.height <= m.rows) in Mat, file /usr/include/opencv/cxmat.hpp,
line 187
terminate called after throwing an instance of 'cv::Exception'
what(): /usr/include/opencv/cxmat.hpp:187: error: (-215) 0 <= roi.x &&
0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y &&
0 <= roi.height && roi.y + roi.height <= m.rows in function Mat
Aborted
Suggestion for make the script accept also non perfect squares?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我觉得我需要澄清有关该代码的一些事情。
首先,它假设检测到的区域是一个完美的正方形,因为它忽略了
squares[x]
内的一些点来创建一个新的Mat
。第二,它还假设构成该区域的点是按顺时针方向检测的,从图像左上角的
p0
开始:这可能不是对于所有检测到的区域都是如此。这意味着这段代码:
可能会生成具有无效尺寸的 ROI,例如负宽度和高度值,这就是 OpenCV 在
Mat subimage(image) 上向您抛出
。cv::Exception
的原因,投资回报率);您应该做的是编写一段代码来识别该区域的左上角点并将其命名为
p0
,然后是右侧最近的邻居p1
,然后找到该区域的右下点并将其称为p2
,然后剩下的就是p3
。之后,组装投资回报率就很容易了:编辑:
我在阅读OpenCV v2.3 的文档。它使我之前描述的过程自动化,使事情变得更加简单和干净。您可以使用此技巧将向量中的 4 个点排序为有意义的 Rect 结构:
I feel like I need to clarify a few things about that code.
First, it assumes that the region detected is a perfect square because it ignores some of the points inside
squares[x]
to create a newMat
.Second, it also assumes that the points that make the region were detected in the clockwise direction, starting with
p0
in the top-left corner of the image:which might not be true for all the regions detected. That means that this code:
probably will generate a ROI with invalid dimensions, such as negative width and height values, and that's why OpenCV throws a
cv::Exception
at you onMat subimage(image, roi);
.What you should do, is write a code that will identify the top-left point of the region and call it
p0
, then it's nearest neightbor on the right side,p1
, then find the bottom-right point of the region and call itp2
, and then what's left isp3
. After this, assembling the ROI is easy:EDIT:
I found an excellent solution while reading the documentation of the v2.3 of OpenCV. It automates the process I described earlier and it make things so much easier and clean. You can use this trick to order the 4 Points in the vector to a meaningful
Rect
structure: