如何裁剪具有与源图像边界重叠的矩形的打开的 CV 矩阵
How do I crop an open CV matrix with an rectangle that overlaps the boundary of the source image
在
裁剪矩阵的实现中有一个断言,可以防止cropRect与源图像的边缘重叠。
// Asserts that cropRect fits inside the image's bounds.
cv::Mat croppedImage = image(cropRect);
我想解除此限制,并能够使用位于图像外部的黑色像素来执行此操作。 这可能吗?
答案是:从技术上讲这是可能的,但你真的不想这样做。图像周围没有"黑色像素"。你的"形象"只为自己分配了足够的内存,仅此而已。因此,如果您尝试访问分配内存之外的像素,则会出现运行时错误。如果你想有一些黑色像素,你必须按照@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);
您可以跳过测试并进入初始化,但正如我所说,这是灾难的好方法。
相关文章:
- Ardunio UNO解决了多个重叠的定时器循环
- 在c++中检查长方体是否尽可能快地重叠(无迭代)
- std::当在256字节边界上写入整数时,流的奇怪行为
- 算术序列与区间的最大重叠
- 使用不变量来确定二分搜索中的边界条件
- 跨 DLL 边界访问虚拟方法是否安全/可能?
- 在 64 位边界上对齐C++结构数组?
- 按顺序声明的字符数组重叠
- 拆分重叠段
- std::数组边界检查如何工作?
- CGAL:如何创建填充边界曲线的曲面网格?
- 在 c++ 中是否允许创建具有运行时边界的数组?
- 纹理单位重叠?渲染了错误的纹理
- 是否可以跨 dll 边界返回常量引用/指向 std::vectors?
- 什么可以防止类中相邻成员重叠
- 对多个(可能)重叠范围进行分区的最简单算法
- 英特尔将指令存储在重叠的内存区域
- C++11:16 字节原子<>变量是否在 16 字节边界上自动对齐,从而允许CMPXCHG16B指令?
- 如何在使用网格布局时重叠qwidgets,并将重叠的widget定位在距离窗口边界特定距离的位置
- 如何裁剪具有与源图像边界重叠的矩形的打开的 CV 矩阵