算法+OpenCV】图像极坐标变换及基于OpenCV的实现

在医学图像处理,尤其是在处理血管断层扫描类(如OCT、IVUS等)图像的过程中,不可避免的会使用到极坐标变换,也即是我们通常所说的“方转圆”。同样,我们可以使用极坐标变换的反变换实现“圆转方”

极坐标变换及其反变换的关键在于,根据极坐标变换前的图像(我们称为“方图”)确定极坐标变换后的图像(我们称为“圆图”)上每个像素点的像素值。也即是找到“圆图”和“方图”间几何坐标的对应关系。


1、极坐标变换(方转圆)

原理:如下图所示,实现极坐标变换的关键即在于找到圆图上任一点P(i,j),在方图上对应的点p(m,n),然后通过插值算法实现圆图上所有像素点的赋值。

方图上,其行列数分别为M、N,方图上的每一列对应为圆图上的每条半径,半径方向存在着一个长度缩放因子delta_r = M/R,圆周方向被分为N等分,即角度因子为delta_t = 2π/N;

圆图上,图像坐标(i,j)和世界坐标(x,y)有着如下变换关系:x = j - R, y = R - i;

那么,图中P点半径长度为r = sqrt(x*x + y*y),角度theta = arctan(y/x);

圆图上点P在方图上对应行数为r/delta_r;

圆图上点P在方图上对应的列数n = thata/delta_t。

以上就是极坐标变换的基本原理,结合相应的插值算法,即可实现图像的极坐标变换。



实现代码如下:

[cpp]  view plain  copy
  1. bool cartesian_to_polar(cv::Mat& mat_c, cv::Mat& mat_p, int img_d)  
  2. {  
  3.     mat_p = cv::Mat::zeros(img_d, img_d, CV_8UC1);  
  4.   
  5.     int line_len = mat_c.rows;  
  6.     int line_num = mat_c.cols;  
  7.   
  8.     double delta_r = (2.0*line_len) / (img_d - 1); //半径因子  
  9.     double delta_t = 2.0 * PI / line_num; //角度因子  
  10.   
  11.     double center_x = (img_d - 1) / 2.0;  
  12.     double center_y = (img_d - 1) / 2.0;  
  13.   
  14.     for (int i = 0; i < img_d; i++)  
  15.     {  
  16.         for (int j = 0; j < img_d; j++)  
  17.         {  
  18.             double rx = j - center_x; //图像坐标转世界坐标  
  19.             double ry = center_y - i; //图像坐标转世界坐标  
  20.   
  21.             double r = std::sqrt(rx*rx + ry*ry);  
  22.   
  23.             if (r <= (img_d - 1) / 2.0)  
  24.             {  
  25.                 double ri = r * delta_r;  
  26.                 int rf = (int)std::floor(ri);  
  27.                 int rc = (int)std::ceil(ri);  
  28.   
  29.                 if (rf < 0)  
  30.                 {  
  31.                     rf = 0;  
  32.                 }  
  33.                 if (rc > (line_len - 1))  
  34.                 {  
  35.                     rc = line_len - 1;  
  36.                 }  
  37.   
  38.                 double t = std::atan2(ry, rx);  
  39.   
  40.                 if (t < 0)  
  41.                 {  
  42.                     t = t + 2.0 * PI;  
  43.                 }  
  44.   
  45.                 double ti = t / delta_t;  
  46.                 int tf = (int)std::floor(ti);  
  47.                 int tc = (int)std::ceil(ti);  
  48.   
  49.                 if (tf < 0)  
  50.                 {  
  51.                     tf = 0;  
  52.                 }  
  53.                 if (tc > (line_num - 1))  
  54.                 {  
  55.                     tc = line_num - 1;  
  56.                 }  
  57.   
  58.                 mat_p.ptr<uchar>(i)[j] = interpolate_bilinear(mat_c, ri, rf, rc, ti, tf, tc);  
  59.             }  
  60.         }  
  61.     }  
  62.   
  63.     return true;  
  64. }  

顺便给一段双线性插值的代码:

[cpp]  view plain  copy
  1. uchar interpolate_bilinear(cv::Mat& mat_src, double ri, int rf, int rc, double ti, int tf, int tc)  
  2. {  
  3.     double inter_value = 0.0;  
  4.   
  5.     if (rf == rc && tc == tf)  
  6.     {  
  7.         inter_value = mat_src.ptr<uchar>(rc)[tc];  
  8.     }  
  9.     else if (rf == rc)  
  10.     {  
  11.         inter_value = (ti - tf) * mat_src.ptr<uchar>(rf)[tc] + (tc - ti) * mat_src.ptr<uchar>(rf)[tf];  
  12.     }  
  13.     else if (tf == tc)  
  14.     {  
  15.         inter_value = (ri - rf) * mat_src.ptr<uchar>(rc)[tf] + (rc - ri) * mat_src.ptr<uchar>(rf)[tf];  
  16.     }  
  17.     else  
  18.     {  
  19.         double inter_r1 = (ti - tf) * mat_src.ptr<uchar>(rf)[tc] + (tc - ti) * mat_src.ptr<uchar>(rf)[tf];  
  20.         double inter_r2 = (ti - tf) * mat_src.ptr<uchar>(rc)[tc] + (tc - ti) * mat_src.ptr<uchar>(rc)[tf];  
  21.   
  22.         inter_value = (ri - rf) * inter_r2 + (rc - ri) * inter_r1;  
  23.     }  
  24.   
  25.     return (uchar)inter_value;  
  26. }  


2、极坐标变换的反变换(圆转方)

原理:顾名思义,极坐标变换的反变换即极坐标变换的逆变换,原理和极坐标变换类似,只是更为直接和方便,且不需要进行插值,这里就不再赘述了。


直接看代码吧:

[cpp]  view plain  copy
  1. bool polar_to_cartesian(cv::Mat& mat_p, cv::Mat& mat_c, int rows_c, int cols_c)  
  2. {  
  3.     mat_c = cv::Mat::zeros(rows_c, cols_c, CV_8UC1);  
  4.   
  5.     int polar_d = mat_p.cols;  
  6.     double polar_r = polar_d / 2.0; // 圆图半径  
  7.   
  8.     double delta_r = polar_r / rows_c; //半径因子  
  9.     double delta_t = 2.0*PI / cols_c;  //角度因子  
  10.   
  11.     double center_polar_x = (polar_d - 1) / 2.0;  
  12.     double center_polar_y = (polar_d - 1) / 2.0;  
  13.   
  14.     for (int i = 0; i < cols_c; i++)  
  15.     {  
  16.         double theta_p = i * delta_t; //方图第i列在圆图对应线的角度  
  17.         double sin_theta = std::sin(theta_p);  
  18.         double cos_theta = std::cos(theta_p);  
  19.   
  20.         for (int j = 0; j < rows_c; j++)  
  21.         {  
  22.             double temp_r = j * delta_r; //方图第j行在圆图上对应的半径长度  
  23.   
  24.             int polar_x = (int)(center_polar_x + temp_r * cos_theta);  
  25.             int polar_y = (int)(center_polar_y - temp_r * sin_theta);  
  26.   
  27.             mat_c.ptr<uchar>(j)[i] = mat_p.ptr<uchar>(polar_y)[polar_x];  
  28.         }  
  29.     }  
  30.     return true;  
  31. }  



3、结果

为验证算法,下载了一张IVUS(血管超声)图像,先用极坐标反变换得到方图,再用极坐标变换将方图变换回圆图,通过比较,变换前后的图像基本一致,哦了~~~


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值