我尝试在 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/