#define window "[结果图]"
//
Mat mapx;
Mat mapy;
Mat src;
void get_mapxy(int ch);
int main()
{
//1.原图像。
src = imread("马车2.jpg");
resize(src, src, Size(), 0.2, 0.2);
imshow("效果图",src);
//2.准备参数。
//3.wasd四个按键对应四种重映射。
namedWindow(window);
//3.根据按键对mapx和mapy进行赋值。
mapx.create(src.size(),CV_32FC1);
mapy.create(src.size(),CV_32FC1);//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy一定初始化否则会报错map参数为空。
while (1)
{
int ch = waitKey(0);
if (ch == 27)
break;
get_mapxy(ch);
Mat dst(src.size(),src.type());
remap(src, dst, mapx, mapy, CV_INTER_LINEAR);
//5.show
imshow(window,dst);
}
waitKey(0);
return 0;
}
void get_mapxy(int ch)
{
switch (ch)
{
case 'w':
for (int i = 0; i < src.rows; ++i)
{
for (int j = 0; j < src.cols; ++j)
{
mapy.at<float>(i, j) = static_cast<float>(src.rows - i);//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx浮点数。
mapx.at<float>(i, j) = static_cast<float>(j);//xxxxxxxxxxxxxxxxxxxxxxxxxxx注意x,y的的对应关系,点的坐标和图像的at(x,y)是相反的。
}
}
break;
case 's':
for (int i = 0; i < src.rows; ++i)
{
for (int j = 0; j < src.cols; ++j)
{
mapy.at<float>(i, j) = static_cast<float>(src.rows - i);//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx浮点数。
mapx.at<float>(i, j) = static_cast<float>(src.cols-j);//xxxxxxxxxxxxxxxxxxxxxxxxxxx注意x,y的的对应关系,点的坐标和图像的at(x,y)是相反的。
}
}
break;
case 'd':
for (int i = 0; i < src.rows; ++i)
{
for (int j = 0; j < src.cols; ++j)
{
mapy.at<float>(i, j) = static_cast<float>(i);//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx浮点数。
mapx.at<float>(i, j) = static_cast<float>(src.cols-j);//xxxxxxxxxxxxxxxxxxxxxxxxxxx注意x,y的的对应关系,点的坐标和图像的at(x,y)是相反的。
}
}
break;
case 'a'://其实就是缩小原图,左边为现在的坐标,右边为原图的坐标。结果图像值显示0.25到0.75范围,而右边的坐标却可以显示全部范围。(0.5-(src.rows-0.5))
//这样更容易理解remap函数中两个map参数的意义了。
for (int i = 0; i < src.rows; ++i)
{
for (int j = 0; j < src.cols; ++j)
{
if (i > src.rows*0.25 && i < src.rows * 0.75&&j>src.cols*0.25&&j<src.cols*0.75)
{
mapy.at<float>(i, j) = static_cast<float>(2*(i-src.rows*0.25)+0.5);//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx浮点数。
mapx.at<float>(i, j) = static_cast<float>(2*(j-src.cols*0.25)+0.5);//xxxxxxxxxxxxxxxxxxxxxxxxxxx注意x,y的的对应关系,点的坐标和图像的at(x,y)是相反的。
}
else
{
mapy.at<float>(i, j) = 0;
mapx.at<float>(i, j) = 0;
}
}
}
break;
}
}
w:
a:
s:
d: