opencv3.0线性极坐标变换,python
对数极坐标变换:
import math import numpy as np import cv2 def print_hi(str): print('hi,'+str) def print_3(str): print('hi3,'+str) def polar2(I, center, r, theta=(0, 360), rstep=0.52, thetastep=360.0/(180*8)): #def polar2(I, center, r, theta=(0, 360), rstep=0.5, thetastep=1.0/4): """ 最近邻插值方法 """ cx, cy = center minr, maxr = r mintheta, maxtheta = theta h = int((maxr-minr)/rstep)+1 w = int((maxtheta-mintheta)/thetastep)+1 O33 = 125*np.ones((h, w), I.dtype) #cv2.imshow('i',O33) # 极坐标变换 r = np.linspace(minr, maxr, h) # 生成等差数列,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)) x1, y1 = cv2.polarToCart(r, theta, angleInDegrees=True) # 默认是角度表示,也可以幅度表示 # 最近邻插值 for i in range(h): for j in range(w): px = int(round(x1[i][j])+cx) #近邻 py = int(round(y1[i][j])+cy) #近邻 if ((px >= 0 and px <= w-1) and (py >= 0 and py <= h-1)): O33[i][j] = I[py][px] return O33 if __name__ == "__main__": print_hi('极坐标') print('坐标11,13以0,0为中心极坐标转换') I = cv2.imread(r'clock2.png', cv2.IMREAD_GRAYSCALE) #dst = cv2.linearPolar(I,(390,370),390,cv2.INTER_LINEAR) #opencv3.0线性极坐标变换, M = 100 #系数,越大越好 dst = cv2.logPolar(I,(390,370),M,cv2.WARP_FILL_OUTLIERS) #opencv3.0对数极坐标变换 dst = cv2.rotate(dst,2) # 0:0度,1:90度 cv2.imwrite('duishu.jpg',dst) cv2.imshow('O33', dst) cv2.waitKey(0) cv2.destroyAllWindows()
欢迎讨论,相互学习。
cdtxw@foxmail.com