我有一个包含正方形的图像,我需要提取该正方形中包含的区域。应用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);
}
为了为原始图像中检测到的所有正方形生成一个名为 subimage 的新 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
使脚本也接受非完美正方形的建议?