逆透视变换详解 及 代码实现(二)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yeyang911/article/details/51915348


根据 逆透视变换详解 及 代码实现(一)的原理

下面我用车上拍摄的车道图像,采用逆透视变换得到的图像,给出代码前我们先看下处理结果。


首先是原始图像:


下图为逆透视变换图像:



下面说具体的实现吧!!

一、参数设置:

1、需要知道相机的内部参数(这个具体步骤可以找相关文档,这里就不具体展开说)。

我们这里假设已经获取内部参数:

相机焦距,相机光学中心, 相机高度, 相机的俯仰角, 相机的偏航角, 相机拍摄出的图像尺寸。

参数说明:  其中偏航角 俯仰角 就是在(一)中所说的世界坐标经过旋转矩阵得到相应的相机坐标。  而偏航角和俯仰角将决定这个旋转矩阵。  而相机焦距  和相机光学中心 是可以从相机标定后得出 ,相机高度需要自己测量。

图像尺寸,是拍出图像的尺寸。

2、 设定逆透视变换的参数:

逆透视图像的尺寸,需要进行逆透视变换的区域,逆透视变换的差值算法。

逆透视变换的区域:原始图像中需要变换的区域(当然这个区域不能超过消失点区域,后面会说到)

逆透视图像的尺寸: 就是需要将逆透视变换区域映射到这个逆透视图像上。

差值算法:因为需要映射,所以某些数值需要估计出,这里用双线性差值。


二、

根据相机的内部参数计算消失点:

因为图像是二维的,所以消失点是是一个二维坐标。

code:


 
 
  1. function [ vp ] = GetVanishingPoint( cameraInfo )
  2. %GetVanishingPoint Summary of this function goes here
  3. % Detailed explanation goes here
  4. vpp = [ sin(cameraInfo.yaw*pi/ 180)/ cos(cameraInfo.pitch*pi/ 180); cos(cameraInfo.yaw*pi/ 180)/ cos(cameraInfo.pitch*pi/ 180); 0];
  5. tyawp = [
  6. cos(cameraInfo.yaw*pi/ 180), - sin(cameraInfo.yaw*pi/ 180), 0;
  7. sin(cameraInfo.yaw*pi/ 180), cos(cameraInfo.yaw*pi/ 180), 0;
  8. 0, 0, 1];
  9. tpitchp = [ 1, 0, 0;
  10. 0, - sin(cameraInfo.pitch*pi/ 180), - cos(cameraInfo.pitch*pi/ 180);
  11. 0, cos(cameraInfo.pitch*pi/ 180), - sin(cameraInfo.pitch*pi/ 180)];
  12. transform = tyawp*tpitchp;
  13. t1p = [
  14. cameraInfo.focalLengthX, 0, cameraInfo.opticalCenterX;
  15. 0, cameraInfo.focalLengthY, cameraInfo.opticalCenterY;
  16. 0, 0, 1];
  17. transform = t1p*transform;
  18. vp = transform*vpp;
  19. end

三、利用消失点可以得到uv平面中的图像范围和对应的xy平面的范围

code:

uvLimitsp = [ vp.x,         ipmInfo.ipmRight, ipmInfo.ipmLeft,  vp.x;
              ipmInfo.ipmTop, ipmInfo.ipmTop, ipmInfo.ipmTop,   ipmInfo.ipmBottom];

xyLimits = TransformImage2Ground(uvLimitsp,cameraInfo);


 
 
  1. function [ xyLimits ] = TransformImage2Ground( uvLimits,cameraInfo )
  2. % TransformImage2Ground Summary of this function goes here
  3. % Detailed explanation goes here
  4. [ row , col ] = size (uvLimits) ;
  5. inPoints4 = zeros(row+ 2,col);
  6. inPoints4( 1: 2? = uvLimits;
  7. inPoints4( 3? = 1;
  8. inPoints3 = inPoints4( 1: 3?;
  9. c1 = cos(cameraInfo.pitch pi/180);
  10. s1 = sin(cameraInfo.pitchpi/ 180);
  11. c2 = cos(cameraInfo.yaw pi/180);
  12. s2 = sin(cameraInfo.yawpi/ 180);
  13. matp= [
  14. -cameraInfo.cameraHeight c2/cameraInfo.focalLengthX,cameraInfo.cameraHeights1 s2/cameraInfo.focalLengthY,(cameraInfo.cameraHeightc2 cameraInfo.opticalCenterX/cameraInfo.focalLengthX)- (cameraInfo.cameraHeight s1s2 cameraInfo.opticalCenterY/ cameraInfo.focalLengthY) - cameraInfo.cameraHeight c1s2;
  15. cameraInfo.cameraHeight s2/cameraInfo.focalLengthX, …
  16. cameraInfo.cameraHeight s1c2/cameraInfo.focalLengthY, …
  17. (-cameraInfo.cameraHeight s2 cameraInfo.opticalCenterX …
  18. /cameraInfo.focalLengthX)-(cameraInfo.cameraHeight s1c2 …
  19. cameraInfo.opticalCenterY /cameraInfo.focalLengthY) - …
  20. cameraInfo.cameraHeight c1c2;
  21. 0, cameraInfo.cameraHeight c1/cameraInfo.focalLengthY, (-cameraInfo.cameraHeight c1 cameraInfo.opticalCenterY/cameraInfo.focalLengthY)+cameraInfo.cameraHeights1;
  22. 0, -c1 /cameraInfo.focalLengthY,(c1* cameraInfo.opticalCenterY /cameraInfo.focalLengthY) - s1];
  23. inPoints4 = matp*inPoints3;
  24. inPointsr4 = inPoints4( 4?;
  25. div = inPointsr4;
  26. inPoints4( 1? = inPoints4( 1?./ div;
  27. inPoints4( 2? = inPoints4( 2?./ div;
  28. inPoints2 = inPoints4( 1: 2?;
  29. xyLimits = inPoints2;
  30. end


四、根据得到的范围计算xy平面的一一对应的映射


 
 
  1. row1 = xyLimits( 1,:);
  2. row2 = xyLimits( 2,:);
  3. xfMin = min(row1); xfMax = max(row1);
  4. yfMin = min(row2); yfMax = max(row2);
  5. [outRow outCol] = size(outImage);
  6. stepRow = (yfMax - yfMin)/outRow;
  7. stepCol = (xfMax - xfMin)/outCol;
  8. xyGrid = zeros( 2,outRow*outCol);
  9. y = yfMax- 0.5*stepRow;
  10. for i = 1:outRow
  11. x = xfMin+ 0.5*stepCol;
  12. for j = 1:outCol
  13. xyGrid( 1,(i- 1)*outCol+j) = x;
  14. xyGrid( 2,(i- 1)*outCol+j) = y;
  15. x = x + stepCol;
  16. end
  17. y = y - stepRow;
  18. end

五、将xy平面的映射转换到uv平面,并画出这个映射


 
 
  1. %TransformGround2Image
  2. uvGrid = TransformGround2Image(xyGrid,cameraInfo);
  3. % Image mean
  4. means = mean(R(:))/ 255;
  5. RR = double(R)/ 255;
  6. for i= 1:outRow
  7. for j = 1:outCol;
  8. ui = uvGrid( 1,(i -1)*outCol+j);
  9. vi = uvGrid( 2,(i -1)*outCol+j);
  10. if (ui<ipmInfo.ipmLeft || ui>ipmInfo.ipmRight || vi<ipmInfo.ipmTop || vi>ipmInfo.ipmBottom)
  11. outImage(i,j) = means;
  12. else
  13. x1 = int32(ui); x2 = int32(ui+ 1);
  14. y1 = int32(vi); y2 = int32(vi+ 1);
  15. x = ui- double(x1) ; y = vi- double(y1);
  16. val = double(RR(y1,x1))*( 1-x)*( 1-y)+ double(RR(y1,x2))*x*( 1-y)+ double(RR(y2,x1))*( 1-x)*y+ double(RR(y2,x2))*x*y;
  17. outImage(i,j) = val;
  18. end
  19. end
  20. end

最终可以显示这个图像:如上面的逆透视变化图像!

具体的code,可以在这里下载。如果问题可以留言交流!!



另外,如果需要标定相机的可以参考这篇博文:

http://blog.csdn.net/yeyang911/article/details/52382722


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值