python-opencv ring to rectangle to rectangle to ring (counterclockwise)

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)
            rho = radius-black_img.shape[0]+row-1
            p_x = int(circle_center[0] + rho*math.sin(math.pi*2-theta)+0.5)-1
            p_y = int(circle_center[1] - rho*math.cos(math.pi*2-theta)+0.5)-1
            black_img[row,col,:] = img[p_y,p_x,:]
    cv2.imshow('bk',black_img)
    cv2.waitKey()
    cv2.imwrite('bk1.jpg',black_img)
    return black_img

img = cv2.imread('yinzhang.png')
#get_huan_by_circle(img,(305,305),305,131,-1*math.pi/2)
get_huan_by_circle(img,(72,72),72,35,-1*math.pi/2)

def get_circle_by_huan(img):
    cv2.imshow('bk',img)
    cv2.waitKey()
    h,w,_ = img.shape
    radius = w/(math.pi*2)
    black_img = np.full((int(2*radius)+1,int(2*radius)+1,3),dtype='uint8',fill_value=255)
    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-black_img.shape[0]+row+1
            theta = (col+1)*(math.pi*2)/img.shape[1]
            #theta = (col+1)/black_img.shape[1]*(math.pi*7/11)+origin_theta
            p_x = int(circle_center[0] + rho*math.sin(-1*theta)-0.5)-1
            p_y = int(circle_center[1] - rho*math.cos(-1*theta)-0.5)-1
            black_img[p_y,p_x,:] = img[img.shape[0]-row-1,col,:]
    cv2.imshow('bk1',black_img)
    cv2.waitKey()
    return black_img

img = cv2.imread('bk1.jpg')
get_circle_by_huan(img)

 

Guess you like

Origin blog.csdn.net/wi162yyxq/article/details/106994310