Python+OpenCV人体变形哈哈镜
如需远程调试,可加QQ905733049由专业技术人员远程协助!
运行代码如下:
import cv2
import numpy as np
img = cv2.imread('face1.jpg')
#img = cv2.imread('me.jpg')
new_img = np.zeros([row, col, dim], dtype=np.uint8)
#cv2.imshow('img', img)
#cv2.waitKey(0)
#
center_i = row / 2
center_j = col / 2
radius = min(center_i, center_j) # 防止前面赋值越界
# i, j为new_img的坐标,下面要找到原图像的对应坐标
for i in range(row):
for j in range(col):
y = np.sqrt((i - center_i)**2 + (j - center_j)**2) # 新坐标i,j与圆心距离
new_img[i, j, :] = img[i, j, :]
if (y <= radius): # 圆心内的像素要分配新的位置
original_i = np.int(floor(y*(i-center_i) / radius + center_i)) # 根据现有坐标找回原图坐标,这里不懂,记录下来
original_j = np.int(np.floor(y*(j-center_j) / radius + center_j))
new_img[i, j, :] = img[original, original_j, :]
cv2.imshow('new_img', new_img)
cv2.waitKey(0)
运行结果如下: