opencv+python學(xué)習(xí)記錄(六)極坐標(biāo)變換cv.ploarToCart

# 實(shí)現(xiàn)圖像從圓形到矩形的轉(zhuǎn)變,需要用到opencv中的cv.ploarToCart

#cv.polarToCart(magnitude, angle, x=None, y=None, angleInDegrees=None)

? ? #返回x, y(笛卡爾坐標(biāo)的x, y)

#-*-coding:utf-8-*-

import cv2 as cv

import numpy as np

def polar(I,center,r,theta=(90,450),rstep=1.0,thetastep = 360.0/(180*8)):

? ? # 圖像灰度處理

? ? gray= cv.cvtColor(I, cv.COLOR_BGR2GRAY)

# 得到距離的最小、最大范圍

? ? minr, maxr= r

? ? # 角度的最小范圍

? ? mintheta, maxtheta= theta

? ? # 輸出圖像的高、寬

? ? H= int((maxr-minr)/rstep)+1

? ? W= int((maxtheta-mintheta)/thetastep)+1

? ? O= 125 * np.ones((H, W), gray.dtype)

# 極坐標(biāo)變換

? ? 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))

????x, y= cv.polarToCart(r,theta,angleInDegrees=True)

????# 最近鄰插值

? ? for iin range(H):

? ? ? ? for jin range(W):

? ? ? ? ? ? px= int(round(x[i][j])+ cx)

????????????py= int(round(y[i][j])+ cy)

????????????if((px>= 0 and px<=w-1)and (py>=0 and py<=h-1)):

? ? ? ? ? ? ? ? O[i][j]= gray[py][px]

return O

if __name__== "__main__":

? ? print("---------------Hello python ------------")

????filename= ("polar.jpg")

????src= cv.imread(filename)

????# cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)

????# cv.imshow("input image", src)

????# 圖像的寬、高

? ? h, w= src.shape[:2]

# 極坐標(biāo)中心

? ? cx, cy= w/2, h/2

? ? # 標(biāo)出圖像中心點(diǎn)

? ? cv.circle(src, (int(cx),int(cy)),10, (255,0.0,0),3)

? ?O= polar(src, (cx, cy), (50,400))

# 旋轉(zhuǎn)

? ? O= cv.flip(O,0)

????O1= cv.cvtColor(O, cv.COLOR_GRAY2BGR)

????cv.imshow("src", src)

????cv.imshow("O", O)

????cv.imshow("O1", O1)

????cv.waitKey(0)

????cv.destroyAllWindows()

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容