一、鱼眼效果核心代码
import cv2 as cv
import math
import numpy as np
def qingxin_fun(instance):
src_img = instance.m_image
row = src_img.shape[0]
col = src_img.shape[1]
channel = src_img.shape[2]
new_img = np.zeros([row, col, channel], dtype=np.uint8)
center_x = row / 2
center_y = col / 2
radius = min(center_x, center_y)
for i in range(row):
for j in range(col):
distance = ((i - center_x) * (i - center_x) + (j - center_y) * (j - center_y))
new_dist = math.sqrt(distance)
new_img[i, j, :] = src_img[i, j, :]
if distance <= radius ** 2:
new_i = np.int(np.floor(new_dist * (i - center_x) / radius + center_x))
new_j = np.int(np.floor(new_dist * (j - center_y) / radius + center_y))
new_img[i, j, :] = src_img[new_i, new_j, :]
instance.m_image = new_img
instance.updata_image()
二、UI界面
action_qingxin_show = QAction('&鱼眼效果', instance)
action_qingxin_show.triggered.connect(instance.qingxin_effect_emit)
tempMenu.addAction(action_qingxin_show)
def qingxin_effect_emit(self):
qingxin_effect.qingxin_fun(self)
三、效果