我正在使用单眼相机实时工作,并且正在尝试恢复鸟瞰图以检测路线。我在python中使用opencv库,并且使用了warpPerspective函数来获取转换后的图像。
现在,我得到了鸟瞰图图像。但是,这些线不平行,并且图像具有梯形形状。我不知道哪个函数可以检索并行图像。
代码在下面。
注意:我正在ROS工作。
i=0
#declare rotation angles
phi = -80*math.pi/180
#tangage
theta = -0*math.pi/180
#roulis
psi = -(0+90)*math.pi/180
#lacet
mu = 0*math.pi/180
#tangage supplimentaire
zeta = 0*math.pi/180
#roulis suplimentaire
hc = 500
#declare rotation matrix
Rphi = [[1,0,0,0],
[0,math.cos(phi),-math.sin(phi),0],
[0,math.sin(phi),math.cos(phi),0],
[0,0,0,1]]
Rzeta = [[1,0,0,0],
[0,math.cos(zeta),-math.sin(zeta),0],
[0,math.sin(zeta),math.cos(zeta),0],
[0,0,0,1]]
Rtheta = [[math.cos(theta),0,math.sin(theta),0],
[0,1,0,0],
[-math.sin(theta),0,math.cos(theta),0],
[0,0,0,1]]
Rmu = [[math.cos(mu),0,math.sin(mu),0],
[0,1,0,0],
[-math.sin(mu),0,math.cos(mu),0],
[0,0,0,1]]
Rpsi = [[math.cos(psi),math.sin(psi),0,0],
[-math.sin(psi),math.cos(psi),0,0],
[0,0,1,0],
[0,0,0,1]]
T = [[0,0,0,0],
[0,0,0,0],
[0,0,0,hc],
[0,0,0,0]]
K = [[1617.442,0,664.520,0],
[0,1622.176,490.369,0],
[0,0,1,0]]
K2 = [[0,1,-664.520],
[1,0,-490.369],
[0,0,10],
[0,0,1]]
rot = np.dot(Rzeta,Rmu)
Rrot1 = np.dot(Rphi,np.dot(Rtheta,Rpsi))
Rrot2 = np.dot(Rrot1,rot)
Rrot3 = np.add(Rrot2,T)
#matrice de transformation
G1 = np.dot(K,np.dot(Rrot3,K2))
#G1 = np.dot(K,np.dot(T,np.dot(Rrot2,K2)))
G = inv(G1)
dist_vect = np.array([-0.4417, 0.9801, -0.000189, 0.0002, -3.1882])
mtx_vect = np.array([[1617.442,0,664.520],
[0,1622.176,490.369],
[0,0,1]])
img_size = [1280,964]
def image_callback(msg):
global i
print("Received an image!")
try:
# Convert your ROS Image message to OpenCV2
cv2_img = bridge.imgmsg_to_cv2(msg, "bgr8")
gray = cv2.cvtColor(cv2_img, cv2.COLOR_BGR2GRAY)
rospy.sleep(1)
except CvBridgeError, e:
print(e)
else:
exists = os.path.isfile('./camera_image'+str(i)+'.png')
if exists:
# Save your OpenCV2 image as a png
i+=1
dst = cv2.undistort(gray, mtx_vect, dist_vect, None, mtx_vect)
transformed = cv2.warpPerspective(dst, G, (1280, 964), cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP + cv2.WARP_FILL_OUTLIERS)
cv2.imwrite('camera_image'+str(i)+'.png', transformed)
print("image enregistrer num ", str(i))
else :
cv2.imwrite('camera_image0.png', transformed)
print("image enregistrer num ", str(i))