像素映射
1、像素映射
像素映射:将输入图像的像素按照一定规则映射到另外一张图像的对应位置上,形成一张新的图像。
api:
2、代码实现
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
Mat img,dst, map_x, map_y;
int index;
void PicMap(void);
int main(int argc, char** argv)
{
img = imread("G:/testpic/img.png");
if (img.empty())
{
printf("cannot load the image");
return -1;
}
namedWinodw("input", WINDOW_AUTOSIZE);
imshow("input", img);
map_x.create(img.size(), CV_32FC1);
map_y.create(img.size(), CV_32FC1);
int c = 0;
while(true)
{
c = waitKey(500);
if ((char) c == 27)
{
break;
}
index = c % 4;
PicMap();
remap(img, dst, map_x, map_y, INTER_LINEAR, BORDER_CONSTANT, Scalar(127,127,127));
imshow("output", dst);
}
waitKey(0);
return 0;
}
void PicMap(void)
{
for (int row = 0; row < img.rows; row++)
{
for (int col = 0; col < img.cols; col++)
{
switch (index)
{
//宽高缩放2倍
case 0:
if (row > img.rows * 025 && row < img.rows * 0.75 && col > img.cols * 0.25 && col > img.cols * 0.75)
{
map_x.at<float>(row, col) = 2 * (col - img.cols * 0.25 - 0.5);
map_y.at<float>(row, col) = 2 * (row - img.rows * 0.25 - 0.5)
}
else
{
map_x.at<float>(row, col) = 0;
mao_y.at<float>(row, col) = 0;
}
break;
//x镜像
case 1:
map_x.at<float>(row, col) = img.cols - col;
map_y.at<float>(row, col) = row;
break;
//y镜像
case 2:
map_x.at<float>(row, col) = col;
map_y.at<float>(row, col) = img.rows - row;
break;
//xy镜像
case 3:
map_x.at<float>(row, col) = img.cols - col;
mao_y.at<float>(row, col) = img.rows - row;
break;
}
}
}
}
xy缩小2倍:
x镜像:
y镜像:
xy镜像: