c++ - 使用指针将像素强度分配给未初始化的 Mat 矩阵
问题描述
我尝试在 OpenCV 的矩阵中提取感兴趣区域 (ROI)。它可以很容易做到cv:Rect
,例如,im_roi = im(Rect(x,y, width, height))
。但我更喜欢使用指针直接从内存中获取数据,这可能更有效。下面是我的代码:
Mat im_roi; //the desired matrix holding ROI of im, uninitialized
uchar* im_roi_data = im_roi.data;
uchar* im_data = im.data;
int xstart = x;
int xend = xstart + width;
int ystart = y;
int yend = ystart + height;
for(ii=ystart; ii<yend; ii++)
{
for(jj=xstart; jj<xend; jj++) //the typo 'jj<xstart' was corrected
{
*im_roi_data++ = *im_data++;
*im_roi_data++ = *im_data++;
*im_roi_data++ = *im_data++;
}
im_data +=3*(im.cols-width);
}
然而,上面的 for 循环代码不会继续。我觉得问题可能是由于未初始化的im_roi
.
解决方案
我认为你的第二个for
循环需要是:
for(jj=xstart; jj<xend; jj++)
推荐阅读
- cmake - CPACK:如何在使用 CPACK 打包人文件中使用 CPACK_RPM_ADDITIONAL_MAN_DIRS 变量?
- angular - 如何用角度格式化密钥?
- vba - 错误:-2147188160 Slides.Item:整数超出范围。2 不在索引的有效范围 1 到 1 VBA 电源点错误
- python - 删除数据帧的列,其中所有行的行值都是恒定的
- javascript - NUXT如何将数据从页面传递到布局
- python - 启动 Exe 时出现 FileNotFoundError
- flutter - Dart 数据类型命名约定.. 为什么使用字符串而不是字符串
- servlets - 没有使用 osgi.service.http.whiteboard 模式调用 Servlet
- java - Zip 依赖助焊剂
- robotframework - 如何使用运行关键字评估变量 if?