像素映射

像素映射
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镜像:
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值