我想将图像中的每个像素向右移动1px,下面是我用来进行重映射转换的地图。
这种方法比进行这种简单的转换所需的时间要多得多。我可以使用cv功能吗?或者我只是将图像分成2个图像,一个是src.cols-1像素宽,另一个是1像素宽,然后将它们复制到新图像?
void update_map()
{
for( int j = 0; j < src.cols; j++ ){
for( int i = 0; i < src.rows; i++ ){
if (j == src.cols-1)
mat_x_Rotate.at<float>(i,j) = 0;
else
mat_x_Rotate.at<float>(i,j) = j + 1;
mat_y_Rotate.at<float>(i,j) = i;
}
}
}
答案 0 :(得分:8)
您可以采取哪些措施来改善绩效:
Mat
以行主顺序存储,因此首先迭代列非常缓存不友好)Mat::ptr()
直接访问同一行中的像素,作为C风格的数组。 (这是使用at<>()
的一个巨大的性能胜利,这可能会像每次访问的检查索引一样)if
语句从内循环中取出,并分别处理列0
。作为替代方案:是的,将图像分割成部分并复制到新图像可能与直接复制一样有效,如上所述。
答案 1 :(得分:3)
Mat Shift_Image_to_Right( Mat src_in, int num_pixels)
{
Size sz_src_in = src_in.size();
Mat img_out(sz_src_in.height, sz_src_in.width, CV_8UC3);
Rect roi;
roi.x = 0;
roi.y = 0;
roi.width = sz_src_in.width-num_pixels;
roi.height = sz_src_in.height;
Mat crop;
crop = src_in(roi);
// Move the left boundary to the right
img_out = Scalar::all(0);
img_out.adjustROI(0, 0, -num_pixels, 0);
crop.copyTo(img_out);
img_out.adjustROI(0, 0, num_pixels, 0);
return img_out;
}