python opencv 圆环转矩形例程(环境anaconda,jupyter notebook)

想跑的例程:圆环转矩形(python opencv)
https://blog.csdn.net/wi162yyxq/article/details/106980593

1. 搭环境

conda新虚拟环境(opencv):numpy,opencv,matplotlib包(conda install 前两个包名,pip install matplotlib),版本降级conda install pywin32=302,conda list查看已安装的包
conda新建虚拟环境

用jupyter notebook打开新环境(opencv)
https://blog.csdn.net/weixin_44109827/article/details/127195459

2. 代码

# cell 1
import cv2
%matplotlib inline
import numpy as np
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-row-1
            p_x = int(circle_center[0] + rho*math.cos(theta)+0.5)
            p_y = int(circle_center[1] - rho*math.sin(theta)+0.5)
            
            black_img[row,col,:] = img[p_y,p_x,:]
            
    #IM.fromarray(black_img).show()
    
    return black_img
# cell 2
img = cv2.imread('test.jpg') # 图片放在和.ipynb同一目录下
img2 = get_huan_by_circle(img,(200,300),200,130)
cv2.imshow("img2",img2)
cv2.waitKey(0)
cv2.destroyAllWindows()

3. 参考

百度搜索 jupyter imshow
https://blog.csdn.net/kuweicai/article/details/103359299