现在的位置: 首页 > 综合 > 正文

如何利用opencv实现投射变换

2012年12月05日 ⁄ 综合 ⁄ 共 1407字 ⁄ 字号 评论关闭

在应用开发中,如果得到的视频图像是投射变换后的,需要进行校正,可以利用cvWarpPerspective函数实现透视变换,通过透视变换,可实现图像翻转,线性的任意变形。

同样也可以利用投射变换 cvGetQuadrangleSubPix实现图像翻转,但是它的自由度要少三个,所以线性的任意变形不能实现。

 

//透视变换
int HumanMotion::WarpPerspective(IplImage *grey)
{
 IplImage* Img_old = cvCloneImage( grey );
 
 int fix = 4;
 int w = Img_old->width + fix;
 int h = Img_old->height + fix;
 
 CvPoint2D32f src_point[4];
 CvPoint2D32f dst_point[4];

 //设定源观察面,即指定四边形的四个顶点
 src_point[0].x=17;            
 src_point[0].y=475;
 
 src_point[1].x=952;                   
 src_point[1].y=449;
 
 src_point[2].x=100;
 src_point[2].y=128;
 
 src_point[3].x=840;
 src_point[3].y=110;

 
   //设定目标观察面,即指定四边形的四个顶点

 dst_point[0].x=-fix;              
 dst_point[0].y=h;
 
 dst_point[1].x=w;             
 dst_point[1].y=h;
   
 dst_point[2].x=-fix;
 dst_point[2].y=-fix;
 
 dst_point[3].x=w;
 dst_point[3].y=-fix;
   
 
 float newm[9];           
 CvMat newM = cvMat( 3, 3, CV_32F, newm );
 //获得透视转换矩阵
 cvWarpPerspectiveQMatrix(src_point,dst_point,&newM);
 //透视转换
 cvWarpPerspective(Img_old,grey,&newM,CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS, cvScalarAll(0) );
 cvReleaseImage(&Img_old);

 return 0;
 
}

 

//测试投射变换

int HumanMotion::Test_cvMUL2(IplImage *grey)
{

 IplImage* src = cvCloneImage( grey );
 
 float m[6];
 int angle = frame_count%60 - 30;
 
 CvMat M = cvMat( 2, 3, CV_32F, m );
 int w = src->width;
 int h = src->height;
 
 m[0] = (float)(cos(angle*CV_PI/180.));
 m[1] = (float)(sin(angle*CV_PI/180.));
 m[2] = w*0.5f;
 m[3] = -m[1];
 m[4] = m[0];
 m[5] = h*0.5f;
 
 cvGetQuadrangleSubPix( src, grey, &M);
 
 return 0;
 
}

抱歉!评论已关闭.