参考原项目地址:https://github.com/spmallick/learnopencv/tree/master/FunnyMirrors
原理
3D点和图像中的像素点具有的等式映射关系:
1.创建虚拟相机,本质上式中P 2.定义3D面 原始图像为2D,将原始相机表示为虚拟相机中的3D面,我们用以下三种表示:
- plane.Z += 20np.exp(-0.5((plane.X1.0/plane.W)/0.1)**2)/(0.1np.sqrt(2*np.pi))
- plane.Z += 20np.sin(2np.pi*((plane.X-plane.W/4.0)/plane.W)) + 20np.sin(2np.pi*((plane.Y-plane.H/4.0)/plane.H))
- plane.Z -= 100np.sqrt((plane.X1.0/plane.W)**2+(plane.Y*1.0/plane.H)**2)
3.虚拟相机捕获面,得到2D平面,根据原图与2D坐标间的对应关系进行重映射。
代码
mport cv2
import numpy as np
import math
from vcam import vcam,meshGen
img = cv2.imread("4.png")
H,W = img.shape[:2]
c1 = vcam(H=H,W=W)
plane = meshGen(H,W)
plane.Z -= 100*np.sqrt((plane.X*1.0/plane.W)**2+(plane.Y*1.0/plane.H)**2)
pts3d = plane.getPlane()
pts2d = c1.project(pts3d)
map_x,map_y = c1.getMaps(pts2d)
output = cv2.remap(img,map_x,map_y,interpolation=cv2.INTER_LINEAR)
cv2.imshow("Funny Mirror",output)
cv2.imshow("Input and output",np.hstack((img,output)))
cv2.waitKey(0)
可视化效果
|