重映射就是把输入图像中各个像素按照制定的规则顺序映射到另外一张图像的对应位置上去,形成一张新的图像。
cv::remap(
InputArray src, //输入图像
OutputArray dst, //输出图像
InputArray map_x, // x 映射表 CV_32FC1/CV_32FC2
InputArray map_y, // y 映射表 CV_32FC1/CV_32FC2
int interpolation, //插值方法,线性差值等
int borderMode, //边缘填充方法,默认BORDER_CONSTANT
const Scalar borderValue //color
)
参数说明:
interpolation 插值方式
INITER_NEAREST——最近邻插值
INTER_LINEAR——双线性插值法(默认值)
INTER_CUBIC——双三次样条插值(过4×4像素领域内)
INTER_LANCZOS4——lanczos插值(过8×8像素领域内)
borderMode:图像边界处理方式,边界的类型有以下几种
(1)BORDER_REPLICATE:重复: aaaaaa|abcdefgh|hhhhhhh
(2)BORDER_REFLECT:反射: fedcba|abcdefgh|hgfedcb
(3)BORDER_REFLECT_101:反射101: gfedcb|abcdefgh|gfedcba
(4)BORDER_WRAP:外包装: cdefgh|abcdefgh|abcdefg
(5)BORDER_CONSTANT:常量复制: iiiiii|abcdefgh|iiiiiii(i的值由后一个参数Scalar()确定,如Scalar::all(0) )
参数borderValue情况:
若上一参数为BORDER_CONSTANT,则由此参数确定补充上去的像素值,可选用默认值。
=========================================================================
代码实现
#include"stdafx.h"
#include
#include
#include
using namespace cv;
using namespace std;
Mat src, dst;
Mat map_x, map_y;
int index = 0;
void update_map(void);
int main(int argc, char** argv)
{
src = imread("F:/photo/qx.jpg");
if (src.empty()) {
cout << "cannot load the image..." << endl;
return -1;
}
char input_title[] = "input";
char output_title[] = "remap_img";
namedWindow(input_title, WINDOW_AUTOSIZE);
imshow(input_title, src);
map_x.create(src.size(), CV_32FC1);
map_y.create(src.size(), CV_32FC1);
int c = 0;
while (true) {
c = waitKey(500);
if ((char)c == 27) {
break;
}
index = c % 4;
update_map();
remap(src, dst, map_x, map_y, INTER_LINEAR, 0, Scalar(0, 255, 255));
imshow(output_title, dst);
}
return 0;
}
void update_map(void) {
for (int row = 0; row < src.rows; row++) {
for (int col = 0; col < src.cols; col++) {
switch (index) {
case 0://缩小
if (col >(src.cols*0.25) && (col < src.rows*0.75) && (row > src.rows*0.25) && (row < src.rows*0.75)) {
map_x.at(row, col) = 2 * (col - (src.cols*0.25));
map_y.at(row, col) = 2 * (row - (src.rows*0.25));
}
else {
map_x.at(row, col) = 0;
map_y.at(row, col) = 0;
}
break;
case 1://X方向对调
map_x.at(row, col) = float(src.cols - col - 1);
map_y.at(row, col) = row;
break;
case 2://Y方向对调
map_x.at(row, col) = col;
map_y.at(row, col) = float(src.rows - row - 1);
break;
case 3://XY对调
map_x.at(row, col) = float(src.cols - col - 1);
map_y.at(row, col) = float(src.rows - row - 1);
break;
}
}
}
}
---------------------------------------------------------------------------------------------------------------------------------
图像处理效果
case =1情况
case =2情况
case=3情况