核心函数:
cvGetPerspectiveTransform,cvWarpPerspective
步骤:
先用cvGetPerspectiveTransform(形变,拉伸,收缩,源目标图像四个点对应确定)求出变换矩阵,然后用cvWarpPerspective进行密集透视变换
程序:
代码:
#include "cv.h" #include "cxcore.h" #include "highgui.h" #include <iostream> int WarpPerspective(int argc,char** argv) //warp:弯曲的 perspective:透射的 { IplImage* src=cvLoadImage("e:\\picture\\4.jpg"); IplImage* dst=cvCreateImage(cvGetSize(src),src->depth,src->nChannels); //定义原图像目标图像对应的四个脚的坐标 CvPoint2D32f SrcPt[4]; CvPoint2D32f DstPt[4]; SrcPt[0].x=0; //src top left SrcPt[0].y=0; SrcPt[1].x=src->width-1; //src top right SrcPt[1].y=0; SrcPt[2].x=0; //src buttom left SrcPt[2].y=src->height-1; SrcPt[3].x=src->width-1; //src buttom right SrcPt[3].y=src->height-1; DstPt[0].x=(dst->width-1)*0.1; //dst top left DstPt[0].y=(dst->height-1)*0.1; DstPt[1].x=(dst->width-1)*0.85; //dst top right DstPt[1].y=(dst->height-1)*0.15; DstPt[2].x=(dst->width-1)*0.2; //dst buttom left DstPt[2].y=(dst->height-1)*0.8; DstPt[3].x=(dst->width-1)*0.7; //dst buttom right DstPt[3].y=(dst->height-1)*0.7; CvMat* WarpMat=cvCreateMat(3,3,CV_32FC1); cvGetPerspectiveTransform(SrcPt,DstPt,WarpMat); //获取变换矩阵 cvWarpPerspective(src,dst,WarpMat,9,cvScalar(0,255,0)); //密集透视变换 cvNamedWindow("src"); cvNamedWindow("dst"); cvShowImage("src",src); cvShowImage("dst",dst); cvWaitKey(0); cvDestroyWindow("src"); cvDestroyWindow("dst"); cvReleaseImage(&src); cvReleaseImage(&dst); return 0; }
密集透视变换 cvWarpPerspective
时间: 2024-10-25 02:38:51