参考原项目地址:https://github.com/spmallick/learnopencv/tree/master/FunnyMirrors
原理
3D点和图像中的像素点具有的等式映射关系: ![在这里插入图片描述](https://img-blog.csdnimg.cn/65295467cf0346dab3329e440fa16f4c.png) ![在这里插入图片描述](https://img-blog.csdnimg.cn/4c4656556c12461abd6cff9b2cf13b3f.png)
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)
可视化效果
![在这里插入图片描述](https://img-blog.csdnimg.cn/062abb4e687c45169a856e5d8e190ac7.png) ![在这里插入图片描述](https://img-blog.csdnimg.cn/501a1f36a8c34a01ad0b07861ce2a11a.png) ![在这里插入图片描述](https://img-blog.csdnimg.cn/a2b2473f04c848de884aef1b1f644ee1.png)
|