小項目——圖轉

import numpy as np
import math
import cv2
img_name = '/home/thugliu/2.jpeg'
img = cv2.imread(img_name)
img = wrapped_img = cv2.resize(img,None,fx=2,fy=2,interpolation=cv2.INTER_CUBIC)
# 準備工作,計算原圖像尺寸和變換後的圖片大小
x0 = img.shape[0]
y0 = img.shape[1]
print(x0,y0)
# 最大半徑計算
radius = int(y0/(2*math.pi))
w=2*radius
h=2*radius
wrapped_img = 255*np.ones((w, h, 3), dtype="u1")

except_count = 0
for j in range(y0):
    # 1. 求極座標系中對應的角度theta
    theta = 2 * math.pi * (j /y0)
    # print(theta)
    for i in range(x0):
       # 2. 計算半徑縮放係數
        wrapped_radius = (i-x0)*radius/x0
        # 3. 利用對應關係進行換算
        y = wrapped_radius * math.cos(theta) + radius
        x = wrapped_radius * math.sin(theta) + radius
        x, y = int(x), int(y)
        try:
            wrapped_img[x, y, :] = img[i, j, :]
            # 注意點,在數學座標系中的座標與數字圖像中的座標表示存在差異需要注意
        except Exception as e:
            except_count = except_count + 1

print(except_count)
# 提取ROI區域進行平滑處理,效果一般
roi = wrapped_img[0:radius,radius-5:radius+5,:]
roi_blur = cv2.blur(roi,(3,3))
wrapped_img[0:radius,radius-5:radius+5,:]=roi_blur
#wrapped_img = cv2.resize(wrapped_img,None,fx=1,fy=1,interpolation=cv2.INTER_CUBIC)
name = 'p_'+img_name
cv2.imwrite(name, wrapped_img)
cv2.imshow("Unwrapped", wrapped_img)
cv2.waitKey(0)

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章