opencv学习--透视变化

时间:2023-03-09 06:37:15
opencv学习--透视变化

透视变换和仿射变换具有很大的相同特性,前面提到了放射变化,这里再次把它拿出和透视变换进行比较

 #include"cv.h"
#include"highgui.h"
using namespace cv;
void WarpPerspective(IplImage *img);
void WarpFangshe(IplImage *img);
int main()
{
IplImage *getimg = cvLoadImage("e:/picture/Wife2.jpg");
IplImage *img = cvCreateImage(cvSize(,),getimg->depth,getimg->nChannels);
cvResize(getimg,img);
WarpPerspective(img);
//WarpFangshe(img);
cvWaitKey();
cvDestroyAllWindows();
return ;
}
//透视变换
//任意四边形的变换
//透视变换需要的设置四个点
void WarpPerspective(IplImage *img)
{
IplImage *dst = cvCreateImage(cvGetSize(img),img->depth,img->nChannels);
CvMat *mat = cvCreateMat(,,CV_32FC1);
CvPoint2D32f ori_point[], dst_point[];
ori_point[].x = ; ori_point[].y = ;
ori_point[].x = img->width - ; ori_point[].y = ;
ori_point[].x = ; ori_point[].y = img->height-;
ori_point[].x = img->width - ; ori_point[].y = img->height - ;
dst_point[].x = img->width / ; dst_point[].y = img->height*0.05;
dst_point[].x = img->width*0.3;; dst_point[].y = img->height / ;
dst_point[].x = img->width*0.7; dst_point[].y = img->height / ;
dst_point[].x = img->width / ; dst_point[].y = img->height*0.9;
//获取映射矩阵
cvGetPerspectiveTransform(ori_point,dst_point,mat);
cvWarpPerspective(img,dst,mat);
//cvFlip(dst,dst,1);
cvNamedWindow("origin");
cvNamedWindow("Warp");
cvShowImage("origin",img);
cvShowImage("Warp",dst);
cvReleaseImage(&img);
cvReleaseImage(&dst);
cvReleaseMat(&mat);
}
void WarpFangshe(IplImage *img)//仿射变换
{
//定义两个CvPoint2D32F的数组
//第一个数组用来标定将要变换的原始图像中的区域
//第二个数组用来标定变换后的图像在窗口中的位置
CvPoint2D32f SrcTri[], DstTri[];
//定义仿射映射矩阵,然后计算(2*3的矩阵)
CvMat *warp_mat = cvCreateMat(, , CV_32FC1);
CvMat *rot_mat = cvCreateMat(, , CV_32FC1);
IplImage *src, *dst;
src = img;
dst = cvCloneImage(src);
SrcTri[].x = ; SrcTri[].y = ;
SrcTri[].x = src->width - ; SrcTri[].y = ;
SrcTri[].x = ; SrcTri[].y = src->height - ;
DstTri[].x = ; DstTri[].y = src->height*0.33;
DstTri[].x = src->width*0.85; DstTri[].y = src->height*0.25;
DstTri[].x = src->width*0.15; DstTri[].y = src->height*0.7;
//获取映射矩阵
cvGetAffineTransform(SrcTri, DstTri, warp_mat);
//映射变换
cvWarpAffine(src, dst, warp_mat);
cvCopy(dst, img);
cvNamedWindow("Warp");
//cvShowImage("Warp",dst);
//下面是对变换后的图像进一步旋转
CvPoint2D32f center = cvPoint2D32f(src->width / , src->height / );
double angle = 50.0;
double scale = 0.8;
//旋转图像
//上面center是旋转中心
//下面函数的第二个和第三个参数给出了旋转的角度和缩放的尺度
//最后一个参数是输出的映射矩阵
/*cv2DRotationMatrix(
CvPoint2D32F center
double angle
double scale
CvMat *map_matrix
)*/
cv2DRotationMatrix(center, angle, scale, rot_mat);
cvWarpAffine(src, dst, rot_mat);
cvShowImage("Warp", dst);
cvReleaseImage(&img);
cvReleaseImage(&dst);
}