opencv3实现多种重映射

时间:2021-11-25 17:19:02
#include<opencv2/opencv.hpp>
#include<iostream>
#include<vector>

using namespace cv;
using namespace std;

int main()
{
Mat srcImage = imread("tiger.jpg", 1);
imshow("【原图】", srcImage);

Mat grayImage;
cvtColor(srcImage, grayImage, CV_BGR2GRAY);

Mat XImage, YImage;
Mat dstImage;
dstImage.create(srcImage.size(), srcImage.type());
XImage.create(srcImage.size(), CV_32FC1);
YImage.create(srcImage.size(), CV_32FC1);

char key;
int _flag = 0;
while (1)
{
for (int i = 0; i < srcImage.rows; i++)
{
for (int j = 0; j < srcImage.cols; j++)
{
//映射关系 (x, y) <-> (cols - x, y);
if (_flag == 1)
{
//这里是将原图中 ((srcImage.cols - j), i)的坐标值给目标图像的 (i, j)处
//经过remap 函数后便将原图中 ((srcImage.cols - j), i)的灰度值给目标图像的 (i, j)处的灰度值
XImage.at<float>(i, j) = static_cast<float>(srcImage.cols - j);
YImage.at<float>(i, j) = static_cast<float>(i);
}
//映射关系 (x, y) <-> (x, rows - y);
else if (_flag == 2)
{
//这里是将原图中 (j, (srcImage.rows - i))的坐标值给目标图像的 (i, j)处
//经过remap 函数后便将原图中 (j, (srcImage.rows - i))的灰度值给目标图像的 (i, j)处的灰度值
XImage.at<float>(i, j) = static_cast<float>(j);
YImage.at<float>(i, j) = static_cast<float>(srcImage.rows - i);
}
//映射关系 (x, y) <-> (cols - x, rows - y);
else if (_flag == 3)
{
//这里是将原图中 ((srcImage.cols - j), (srcImage.rows - i))的坐标值给目标图像的 (i, j)处
//经过remap 函数后便将原图中 ((srcImage.cols - j), (srcImage.rows - i))的灰度值给目标图像的 (i, j)处的灰度值
XImage.at<float>(i, j) = static_cast<float>(srcImage.cols - j);
YImage.at<float>(i, j) = static_cast<float>(srcImage.rows - i);
}
//映射关系 将原图缩放
else if (_flag == 4)
{
/*这里是将原图中 ((2 * (j - srcImage.cols*0.25) + 0.5), (2 * (i - srcImage.rows*0.25) + 0.5))
的坐标值给目标图像的 (i, j)处*/
/*经过remap 函数后便将原图中 ((2 * (j - srcImage.cols*0.25) + 0.5), (2 * (i - srcImage.rows*0.25) + 0.5))
的灰度值给目标图像的 (i, j)处的灰度值*/
if (j > srcImage.cols*0.25 && j < srcImage.cols*0.75 && i > srcImage.rows*0.25 && i < srcImage.rows*0.75)
{
XImage.at<float>(i, j) = static_cast<float>(2 * (j - srcImage.cols*0.25) + 0.5);
YImage.at<float>(i, j) = static_cast<float>(2 * (i - srcImage.rows*0.25) + 0.5);
}
else
{
XImage.at<float>(i, j) = 0;
YImage.at<float>(i, j) = 0;
}
}
else
{
XImage.at<float>(i, j) = static_cast<float>(j);
YImage.at<float>(i, j) = static_cast<float>(i);
}
}
}

//进行重映射
remap(srcImage, dstImage, XImage, YImage, CV_INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0));

imshow("【重映射后】", dstImage);

key = waitKey(1);

if (key == 27)
break;
if (key == '1')
_flag = 1;
if (key == '2')
_flag = 2;
if (key == '3')
_flag = 3;
if (key == '4')
_flag = 4;
}

return 0;
}

opencv3实现多种重映射