opencv+python学习记录(六)极坐标变换cv.ploarToCart

# 实现图像从圆形到矩形的转变,需要用到opencv中的cv.ploarToCart

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

    #返回x, y(笛卡尔坐标的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)

# 极坐标变换

    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]

# 极坐标中心

    cx, cy= w/2, h/2

    # 标出图像中心点

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

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

# 旋转

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

你可能感兴趣的:(opencv+python学习记录(六)极坐标变换cv.ploarToCart)