1

在裁剪矩阵的实现中有一个断言,可以防止cropRect与源图像的边缘重叠。

// Asserts that cropRect fits inside the image's bounds.
cv::Mat croppedImage = image(cropRect); 

我想解除这个限制,并能够使用位于图像之外的黑色像素来做到这一点。这可能吗?

4

1 回答 1

0

答案是:从技术上讲这是可能的,但你真的不想这样做。您的图像周围没有“黑色像素”。您的“图像”为自己分配了足够的内存,仅此而已。因此,如果您尝试访问分配内存之外的像素,您将收到运行时错误。如果你想要一些黑色像素,你必须按照@ffriend 描述的方式自己做。image(cropRect) 没有分配任何东西,它只是创建指向已经存在的内存的新指针。

如果您仍然对如何进行这种裁剪感到好奇,OpenCV 正在执行以下操作:

// check that cropRect is inside the image
if ((cropRect & Rect(0,0,image.cols,image.rows)) != cropRect)
    return -1; // some kind of error notification
// use one of Mat constructors (x, y, width and height are taken from cropRect)
Mat croppedImage(Size(width,height), image.type(), image.ptr(y)+x, image.step);

您可以跳过测试并进行初始化,但正如我所说,这是灾难的好方法。

于 2013-10-20T07:18:35.650 回答