数字图像处理-几何变换

本程序实现图像处理图像几何变换,基本原理参考冈萨雷斯《数字图像处理》(第二版)第五章中第十一小节。程序需要先调用cal_coef函数计算出来系数,然后调用Image_TransAffine函数得到几何变换后的图像。
//
//函数名称:cal_coef
//传入参数:
//      int *p_Coef   保存计算拉伸转换系数
//      CPoint TopLeft  原始图像的左上点
//      CPoint TopRight  原始图像的右上点
//      CPoint DownLeft  原始图像的左下点
//      CPoint DownRight 原始图像的左上点
//      int i_Width   新图像的宽度
//      int i_Height  新图像的高度
//传出参数:
//      1     表示运算成功
//      0     表示运算失败
//函数作用:计算图像拉伸算法的拉伸系数
//计算公式 r(x,y) = c1 * x + c2 * y + c3 * x * y + c4  (r(x,y)为失真图像的x轴坐标)
//     s(x,y) = c5 * x + c6 * y + c7 * x * y + c8  (s(x,y)为失真图像的y轴坐标)
//
int cal_coef(float *p_Coef, CPoint TopLeft, CPoint TopRight, CPoint DownLeft, CPoint DownRight, int i_Width, int i_Height)
{
 // 判断传入参数是否有效
 if (!p_Coef || i_Width <= 0 || i_Height <= 0)
 {
  return 0;
 }
 p_Coef[3] = (float) TopLeft.x;
 p_Coef[7] = (float) TopLeft.y;
 p_Coef[1] = (float) ((TopRight.x - TopLeft.x) / i_Width);
 p_Coef[5] = (float) ((TopRight.y - TopLeft.y) / i_Width);
 p_Coef[0] = (float) ((DownLeft.x - TopLeft.x) / i_Height);
 p_Coef[4] = (float) ((DownLeft.y - TopLeft.y) / i_Height);
 p_Coef[2] = (float) ((DownRight.x - p_Coef[0] * i_Height - p_Coef[1] * i_Width - p_Coef[3]) / (i_Height * i_Width));
 p_Coef[6] = (float) ((DownRight.y - p_Coef[4] * i_Height - p_Coef[5] * i_Width - p_Coef[7]) / (i_Height * i_Width));
 return 1;
}
//
//函数名称:Image_TransAffine
//传入参数:
//      char *p_disImage   目的图像内存指针
//      char *p_srcImage   源图像内存指针
//      float *p_Coel    拉伸方程的系数
//      int i_Width     目的图像的宽度
//      int i_Height    目的图像的高度
//      int dis_step    目的图像的行步长
//      int src_step    源图像的行步长
//传出参数:
//      1     表示运算成功
//      0     表示运算失败
//函数作用:利用上面求取的拉伸方程的系数,对原始图像进行拉伸到目的图像内存里面
//
int Image_TransAffine(uchar *p_disImage, uchar *p_srcImage, float *p_Coel, int i_Width, int i_Height,int dis_step, int src_step)
{
 if (!p_disImage  || ! p_srcImage || !p_Coel || i_Width <= 0 || i_Height <= 0 || dis_step <= 0 || src_step <= 0)
 {
  return 0;
 }
 int i = 0, j = 0;
 int x = 0, y = 0;
 float a = 0.0,b = 0.0;
 for (i = 0; i < i_Height; i++)
 {
  for (j = 0; j < i_Width; j++)
  {
   /*
   x = p_Coel[0] * i + p_Coel[1] * j + p_Coel[2] * i * j + p_Coel[3] + 0.5;
   y = p_Coel[4] * i + p_Coel[5] * j + p_Coel[6] * i * j + p_Coel[7] + 0.5;
   p_disImage[i * dis_step + j] = p_srcImage[x * src_step + y];
   */
   a = p_Coel[0] * i + p_Coel[1] * j + p_Coel[2] * i * j + p_Coel[3];
   b = p_Coel[4] * i + p_Coel[5] * j + p_Coel[6] * i * j + p_Coel[7];
   x =(int)(a + 1);
   y = (int)(b + 1);
   a = a - (int)a;
   b = b - (int)b;
   //直接计算
//   p_disImage[i * dis_step + j] = (uchar)((1 - a) * ((1 - b) * p_srcImage[(x - 1) * src_step + y - 1] + b * p_srcImage[(x - 1) * src_step + y]) + a * ((1 - b) * p_srcImage[x * src_step + y - 1] + b * p_srcImage[x * src_step + y]) + 0.5);
   //调用函数计算
   p_disImage[i * dis_step + j] = Gray_Interpolation(p_srcImage[(x - 1) * src_step + y - 1],p_srcImage[(x - 1) * src_step + y],p_srcImage[x * src_step + y - 1],p_srcImage[x * src_step + y],a,b);
  }
 }
 return 1;
}
//
//函数名称:Gray_Interpolation
//传入参数:
//      char TL_Data   左上角像素灰度值
//      char TR_Data   右上角像素灰度值
//      char DL_Data   左下角像素灰度值
//      char DR_Data   右下角像素灰度值
//      float x     计算出来x轴坐标
//      float y     计算出来y轴坐标
//传出参数:
//      Mid_Data    拉伸后对应点的灰度值
//函数作用:按照计算拉伸前坐标点四周贡献不同计算该点的灰度值
//
char Gray_Interpolation(uchar TL_Data, uchar TR_Data, uchar DL_Data, uchar DR_Data, float x, float y)
{
 x = x - (int)x;  //取坐标的小数部分
 y = y - (int)y;  //取坐标的小数部分
 
 uchar Mid_Data;
 Mid_Data =(uchar)( x * (y * (int)DR_Data + (1 - y) * (int)DL_Data) + (1 - x) * (y * (int)TR_Data + (1 - y) * (int)TL_Data) + 0.5);

 return Mid_Data;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值