大体实现思路是根据双目成像原理,构建悬浮物移动模型,然后根据悬浮物在三维空间中的移动以及成像原理,将抠出的悬浮物图像贴到无杂质的水下双目图像中。1.首先最重要的是根据相机标定系数,确定世界坐标系(4×1)相机坐标系(4×1)、以及图像坐标系(3×1)的转换关系
由于是在matlab中软件标定,得到系数文件stereo.mat。大部分系数如相机外参(4×4)、双目相机内参和(3×4)、左目相机焦距、右目相机焦距、基线距均可直接得到,然而需要注意矩阵的转置与补1等关系。2.其次是先生成N个图像坐标系位置,便于控制悬浮物在成像范围内。然后根据坐标转换关系得到悬浮物在三维空间中的世界坐标,悬浮物的运动由构建的运动模型控制,其在左目与右目图像中的成像由成像原理得到。因此可以得到悬浮物在右目图像中的坐标。之后根据坐标系之间的转换关系将图像坐标转换至世界坐标系,并根据构建的悬浮物移动模型对下一帧的悬浮物坐标进行更新。每次将世界坐标系转换到相机坐标系,并计算z值,z值越大,表示越远,则图像越小,z值越小,表示越近,则图像越大,z值与图像大小保持反比关系。通过这种方式模拟现实场景中的近大远小效果。
3.最后记录所有双目帧中所有悬浮物的生成位置,生成mask掩码。为了方便,在将悬浮物贴到无杂质双目图像的过程中,同时以同样的方式将悬浮物贴到白色背景的等大图像中,从而便于计算掩码图。
from PIL import Image
import os
from cv2 import SimpleBlobDetector
import natsort
import numpy as np
import random
import cv2
from numpy.lib.arraysetops import _in1d_dispatcher
def gen_dataxiugai(N,In,Ex,img):#修改,希望根据随机生成的图像坐标得到世界坐标系下的坐标
""" init position and speed of each people """
height,width,c=img.shape#得到图像大小,建立越界约束
worldcords=[]
imagecords=[]
for i in range(N):
generateddata=np.array([[random.uniform(0,1)*width],[random.uniform(0,1)*height],[1]])
generateddata=[generateddata[0]/1,generateddata[1]/1,1]
# print("前一帧坐标")
# print(generateddata)#图像坐标
wordcor=Imgtoworld(In,Ex,generateddata)#根据图像坐标转换到世界坐标系下
x=wordcor[0]/wordcor[3]
y=wordcor[1]/wordcor[3]
z=wordcor[2]/wordcor[3]
theta = np.random.random(1) * 360 / (2 * np.pi)
v0 = 0.1
vx,vy,vz= v0 * np.cos(theta), v0 * np.sin(theta), v0 * np.tan(theta)#调节移动速度
vx,vy,vz=vx[0],vy[0],vz[0]
worldcords.append(np.array([x, y, z, vx, vy,vz]).T)
imagecords.append((generateddata[0],generateddata[1],generateddata[2]))
return worldcords,imagecords
def update1(data,In,Ex,N):#输入上一帧世界坐标计算下一帧图像坐标
imgcors=[]
print(data[:][0:3])
for d in range(N):
x=data[0][1]
data[d][0:3] += data[d][3:6]
if 60 >abs(data[d][0]) > 40:
data[d][3] *= -0.5
elif abs(data[d][0]) >= 60:
data[d][3] *= -1
else:
data[d][3] *=1
if 60 >abs(data[d][1]) > 40:
data[d][4] *= -0.5
elif abs(data[d][1]) >= 60:
data[d][4] *= -1
else:
data[d][4] *=1
if 60 >abs(data[d][2]) > 40:
data[d][5] *= -0.5
elif abs(data[d][2]) >= 60:
data[d][5] *= -1
else:
data[d][5] *=1
# data[d][3] *= random.randint (-2,-1) if abs(data[d][0]) > 40 else 1
# data[d][4] *= random.randint(-1,0) if abs(data[d][1]) > 40 else 1
# data[d][5] *= random.randint(-1,0) if abs(data[d][2]) > 40 else 1
x=data[d][0]
y=data[d][1]
z=data[d][2]
world=[[x],[y],[z],[1]]
imgcor=WorldtoImg(In,Ex,world)
imx=imgcor[0][0]/imgcor[2][0]
imy=imgcor[1][0]/imgcor[2][0]
imgcors.append(np.array([imx,imy]))
return imgcors
def Picture_Synthesis(mother_img,
son_img,