import numpy as np
#输入参数src: 源图像矩阵(方图,笛卡尔坐标)
#输入参数d: 目标图像直径
#返回:dst:目标图像矩阵(圆图,极坐标)
def polar_transform(src, d):
#初始换目标图像矩阵
dst = np.zeros((d, d), dtype = float)
rows, cols = src.shape
#径向步长
delta_r = sample_num/((d - 1)/2.0)
#切向(旋转角度)步长
delta_t = 2.0*np.pi/line_num
ox = (d - 1)/2.0
oy = (d - 1)/2.0
for i in range(0, d, 1):
for j in range(0, d, 1):
rx = j - ox
ry = i - oy
r = np.sqrt(np.power(rx, 2) + np.power(ry, 2))
if (r > rmin) & (r <= (d - 1)/2.0):
t = np.arctan2(ry, rx)
if t < 0:
t = t + 2.0*np.pi
ri = r*delta_r
rf = np.int_(np.floor(ri))
rc = np.int_(np.ceil(ri))
if rf < 0:
rf = 0
if rc > rows - 1:
rc = rows - 1
ti = t/delta_t
tf = np.int_(np.floor(ti))
tc = np.int_(np.ceil(ti))
if tf < 0:
tf = 0
if tc > cols - 1:
tc = cols - 1
#选择相应的插值算法
#dst[i][j] = interpolate_adjacent(src, ri, rf, rc, ti, tf, tc)
dst[i][j] = interpolate_bilinear(src, ri, rf, rc, ti, tf, tc)
#dst[i][j] = interpolate_bitriple(src, ri, rf, rc, ti, tf, tc, rows, cols)
return dst
####最邻近插值算法
def interpolate_adjacent(src, ri, rf, rc, ti, tf, tc):
r = 0
t = 0
if (ri - rf) > (rc - ri):
r = rc
else:
r = rf
if (ti - tf) > (tc - ti):
t = tc
else:
t = tf
out = src[r][t]
return out
####双线性插值
def interpolate_bilinear(src, ri, rf, rc, ti, tf, tc):
if rf == rc & tc == tf:
out = src[rc][tc]
elif rf == rc:
out = (ti - tf)*src[rf][tc] + (tc - ti)*src[rf][tf]
elif tf == tc:
out = (ri - rf)*src[rc][tf] + (rc - ri)*src[rf][tf]
else:
inter_r1 = (ti - tf)*src[rf][tc] + (tc - ti)*src[rf][tf]
inter_r2 = (ti - tf)*src[rc][tc] + (tc - ti)*src[rc][tf]
out = (ri - rf)*inter_r2 + (rc - ri)*inter_r1
return out
####三次卷积算法
def interpolate_bitriple(src, ri, rf, rc, ti, tf, tc, rows, cols):
rf_1 = rf - 1
rc_1 = rc + 1
tf_1 = tf - 1
tc_1 = tc + 1
if rf_1 < 0:
rf_1 = 0
if rc_1 > rows - 1:
rc_1 = rows - 1
if tf_1 < 0:
tf_1 = 0
if tc_1 > cols - 1:
tc_1 = cols - 1
if rf == rc & tc == tf:
out = src[rc][tc]
elif tf == tc:
A = np.array([(4 - 8*(ri - rf + 1) + 5*np.power(ri - rf + 1, 2) - np.power(ri - rf + 1, 3)),
(1 - 2*np.power((ri - rf), 2) + np.power((ri - rf), 3)),
(1 - 2*np.power((rc - ri), 2) + np.power((rc - ri), 3)),
(4 - 8*(rc - ri + 1) + 5*np.power(rc - ri + 1, 2) - np.power(rc - ri + 1, 3))])
B = np.array([[src[rf_1][tf]],
[src[rf][tf]],
[src[rc][tc]],
[src[rc_1][tc]]])
out = np.dot(A, B)
elif rf == rc:
B = np.array(src[rf][tf_1], src[rf][tf], src[rf][tc], src[rf][tc_1])
C = np.array([[(4 - 8*(ti - tf + 1) + 5*np.power(ti - tf + 1, 2) - np.power(ti - tf + 1, 3))],
[(1 - 2*np.power((ti - tf), 2) + np.power((ti - tf), 3))],
[(1 - 2*np.power((tc - ti), 2) + np.power((tc - ti), 3))],
[(4 - 8*(tc - ti + 1) + 5*np.power(tc - ti + 1, 2) - np.power(tc - ti + 1, 3))]])
out = np.dot(B, C)
else:
A = np.array([(4 - 8*(ri - rf + 1) + 5*np.power(ri - rf + 1, 2) - np.power(ri - rf + 1, 3)),
(1 - 2*np.power((ri - rf), 2) + np.power((ri - rf), 3)),
(1 - 2*np.power((rc - ri), 2) + np.power((rc - ri), 3)),
(4 - 8*(rc - ri + 1) + 5*np.power(rc - ri + 1, 2) - np.power(rc - ri + 1, 3))])
B = np.array([[src[rf_1][tf_1], src[rf_1][tf], src[rf_1][tc], src[rf_1][tc_1]],
[src[rf][tf_1], src[rf][tf], src[rf][tc], src[rf][tc_1]],
[src[rc][tf_1], src[rc][tf], src[rc][tc], src[rc][tc_1]],
[src[rc_1][tf_1], src[rc_1][tf], src[rc_1][tc], src[rc_1][tc_1]]])
C = np.array([[(4 - 8*(ti - tf + 1) + 5*np.power(ti - tf + 1, 2) - np.power(ti - tf + 1, 3))],
[(1 - 2*np.power((ti - tf), 2) + np.power((ti - tf), 3))],
[(1 - 2*np.power((tc - ti), 2) + np.power((tc - ti), 3))],
[(4 - 8*(tc - ti + 1) + 5*np.power(tc - ti + 1, 2) - np.power(tc - ti + 1, 3))]])
out = np.dot(np.dot(A, B), C)
return out