c++ - 使用指针将像素强度分配给未初始化的 Mat 矩阵

标签 c++ opencv

我尝试在 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-loop 代码不会继续。我觉得问题可能是由于未初始化的im_roi

最佳答案

我认为您的第二个 for 循环需要:

for(jj=xstart; jj<xend; jj++)

关于c++ - 使用指针将像素强度分配给未初始化的 Mat 矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54384594/

相关文章:

c++ - 在 C++14 中将 constexpr intializer_list 作为参数传递

c++ - 用互联网浏览器的客户区填充窗口的客户区

未调用 C++11 move 构造函数,首选默认构造函数

opencv - OpenCV2.4.6 + CUDA5.0 + VS2010,在 Debug模式下,不显示图像

c++ - 如何使用BackgroundSubtractorMOG2处理图像

c++ - 反对使用 size_t 的理由是什么?

c++ - C++中运行时和编译时多态性之间的区别

algorithm - 追踪图片中的像素

python - 将 OpenCV 图像传入 KNearest 的 find_nearest

python - 级联分类器HAAR LBP建议