图像处理之重映射、仿射和透射变换

微笑重映射Remapping


函数介绍


其中map_x和map_y矩阵记录目标图像对应位置的像素从源图像哪些位置获取。

#include "cv.h"
#include "highgui.h"

using namespace cv;
using namespace std;

Mat src,dst,map_x,map_y;
int change=0;
void getMap(void);

int main(int argc,char *argv[])
{	
	char c;
	src=imread("fish.jpg");
	imshow("src",src);
	map_x.create(src.size(),CV_32FC1);
	map_y.create(src.size(),CV_32FC1);
	while(true)
	{
		c=(char)waitKey(0);
		if(c==27) break;
        getMap();
		remap(src,dst,map_x,map_y,CV_INTER_LINEAR,BORDER_CONSTANT,Scalar(0,0,0));
		imshow("dst",dst);
	}
	destroyAllWindows();
	return 0;
}

void getMap(void)
{
	int i,j;
	change%=4;
	for (i=0;i<src.rows;i++)
	{
		for (j=0;j<src.cols;j++)
		{
			switch(change)
			{
			case 0:
				if (i>src.rows*0.25&&i<src.rows*0.75&&j>src.cols*0.25&&j<src.cols*0.75)
				{
					map_x.at<float>(i,j)=2*(j-src.cols*0.25)-0.5;//后面所加尾数是在图像出现边界时进行调整
					map_y.at<float>(i,j)=2*(i-src.rows*0.25);
				}
				else
				{
					map_x.at<float>(i,j)=0;
					map_y.at<float>(i,j)=0;
				}
				break;
			case 1:
				map_x.at<float>(i,j)=j;
				map_y.at<float>(i,j)=src.rows-i-1;
				break;
			case 2:
				map_x.at<float>(i,j)=src.cols-j-1;
				map_y.at<float>(i,j)=i;
				break;
			case 3:
				map_x.at<float>(i,j)=src.cols-j-1;
				map_y.at<float>(i,j)=src.rows-i-1;
			}
		}
	}
	change++;
}
实验效果:

微笑仿射变换


Opencv函数介绍:


#include "cv.h"
#include "highgui.h"

using namespace cv;
using namespace std;

int main(int argc,char *argv[])
{	
	Mat src,dst,warp(2,3,CV_32FC1);
	Point2f srcPoint[3],dstPoint[3];
	
	src=imread("fish.jpg");
	srcPoint[0]=Point2f(0,0);
	srcPoint[1]=Point2f(src.cols-1,0);
	srcPoint[2]=Point2f(0,src.rows-1);
	dstPoint[0]=Point2f(0,src.rows*0.2);
	dstPoint[1]=Point2f(src.cols*0.8,src.rows*0.1);
	dstPoint[2]=Point2f(src.cols*0.2,src.rows*0.7);
	warp=getAffineTransform(srcPoint,dstPoint);
	warpAffine(src,dst,warp,src.size());
	imshow("dst",dst);
	waitKey(0);
	Point center(src.cols/2,src.rows/2);
	double angle=-50;
	double scale=0.6;
	warp=getRotationMatrix2D(center,angle,scale);
	warpAffine(src,dst,warp,src.size());
	imshow("dst",dst);
	waitKey(0);
	destroyAllWindows();
	return 0;
}
实验效果:(其他如边界复制为定值或源图像边界根据情况自己定)






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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值