具体数学原理就很简单了,无非就是坐标变换,从x,y或者r,theta变换一下,cx+r*sin(theta)和cy-r*cos(theta)
当然由于采样原因,导致恢复得结果里有采样不到的点。
import cv2
import numpy as np
from PIL import Image
import math
def get_huan_by_circle(img,circle_center,radius,radius_width):
black_img = np.zeros((radius_width,int(2*radius*math.pi),3),dtype='uint8')
for row in range(0,black_img.shape[0]):
for col in range(0,black_img.shape[1]):
theta = math.pi*2/black_img.shape[1]*(col+1)#+origin_theta
rho = radius-row-1
p_x = int(circle_center[0] + rho*math.sin(theta)+0.5)-1
p_y = int(circle_center[1] - rho*math.cos(theta)+0.5)-1
black_img[row,col,:] = img[p_y,p_x,:]
return black_img
img = cv2.imread('timg.jpg')
get_huan_by_circle(img,(305,305),305,131)
def get_circle_by_huan(img):
cv2.imshow('bk',img)
cv2.waitKey()
h,w,_ = img.shape
radius = w/(math.pi*2)
black_img = np.zeros((int(2*radius)+1,int(2*radius)+1,3),dtype='uint8')
circle_center = ((int(2*radius)+1)//2,(int(2*radius)+1)//2)
for row in range(0,img.shape[0]):
for col in range(0,img.shape[1]):
rho = radius-row-1
theta = (col+1)*(math.pi*2)/img.shape[1]#+origin_theta
p_x = int(circle_center[0] + rho*math.sin(theta)+0.5)
p_y = int(circle_center[1] - rho*math.cos(theta)-0.5)
black_img[p_y,p_x,:] = img[row,col,:]
# print(col)
black_img = cv2.blur(black_img,(3,3))
return black_img
img = cv2.imread('bk.jpg')
get_circle_by_huan(img)