重映射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;
}
实验效果:(其他如边界复制为定值或源图像边界根据情况自己定)