#coding=utf-8
import numpy as np
import cv2
def img_polar_transform(input_img,center,r_range,theta_rangge=(0,360),r_step=0.5,theta_step=360.0/(180*8)):
minr,maxr=r_range
mintheta,maxtheta=theta_rangge
H=int((maxr-minr)/r_step+1)
W=int((maxtheta-mintheta)/theta_step+1)
output_img=125*np.ones((H,W,3),input_img.dtype)
print(input_img.shape)
print(output_img.shape)
x_center,y_center=center
#极坐标变换
r=np.linspace(minr,maxr,H)
r=np.tile(r,(W,1))
r=np.transpose(r)#矩阵的转置
theta=np.linspace(mintheta,maxtheta,W)
theta=np.tile(theta,(H,1))#在垂直方向重复H次,水平重复1次
x,y=cv2.polarToCart(r,theta,angleInDegrees=True)
#最邻近插值
C=3
for c in range(C):
for i in range(H):
for j in range(W):
px=int(round(x[i,j])+x_center)
py = int(round(y[i, j]) + y_center)
if ((px>=0 and px<=W-1) and (py>=0 and py<=H-1)):
output_img[i,j,c]=input_img[px,py,c]
return output_img
input_img = cv2.imread('C:\\Users\\dwx\\Desktop\\bigdata\\train\\origin\\animal_final\\25_cir.png')
h,w=input_img.shape[:2]
center=(int(h/2),int(w/2))
r_range=(0,159)#修改为原图尺寸一半-1
output_img=img_polar_transform(input_img,center,r_range)
cv2.imshow('img',input_img)
cv2.imshow('polar',output_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
r_range=(0,159)#修改为原图尺寸一半-1
灰度图可在读取图片时,在最后添加一句代码
cv2.IMREAD_GRAYSCALE
input_img = cv2.imread('C:\\Users\\dwx\\Desktop\\bigdata\\train\\origin\\animal_final\\25_cir.png',cv2.IMREAD_GRAYSCALE)