本文主要介绍了如何使用python与V-rep联合仿真,并用OpenCV可视化V-rep中视觉传感器所能看到的 RGB图和深度图,效果图如下。
本文使用的V-rep版本是3.5:
其中,sim.py , simConst.py在vrep的安装目录中,具体在“programming/remoteApiBindings/python”下。 **remoteApi.dll(Windows)**在“programming/remoteApiBindings/lib/lib/Windows”下。
① 调用import sim加载库;
② 利用sim.simxStart()建立客户端;
③ 调用以"simx"为前缀的vrep远程API函数;
④ 停止仿真:sim.simxFinish().
import sim
import time
import sys
import cv2
import numpy as np
#关闭之前的连接
sim.simxFinish(-1)
# 获得客户端ID
clientID = sim.simxStart('127.0.0.1',19997,True,True,5000,5)
print("Connection success!!!")
if clientID != -1:
print('Connected to remote API server')
else:
print('Connection not successful')
sys.exit('Could not connect')
# 启动仿真
sim.simxStartSimulation(clientID,sim.simx_opmode_blocking)
print("Simulation start")
# 使能同步模式
sim.simxSynchronous(clientID,True)
# 获得对象的句柄
ret, targetObj = sim.simxGetObjectHandle(clientID,'target',sim.simx_opmode_blocking)
errorCode,visionSensorHandle = sim.simxGetObjectHandle(clientID,'Vision_sensor',sim.simx_opmode_oneshot_wait)
errprCode,resolution,rawimage = sim.simxGetVisionSensorImage(clientID,visionSensorHandle,0,sim.simx_opmode_streaming)
def readVisionSensor():
global resolution
errprCode, resolution, rawimage = sim.simxGetVisionSensorImage(clientID, visionSensorHandle, 0, sim.simx_opmode_buffer)
sensorImage = []
sensorImage = np.array(rawimage, dtype=np.uint8) #transform the raw image to uint8
sensorImage.resize([resolution[1], resolution[0], 3]) # Process the image to the format (256,128,3)
cv2.flip(sensorImage, 0, sensorImage) # image upside down
image = sensorImage
# print("image.shape: ", image.shape)
return image
def readDepthSensor():
global resolution
# 获取 Depth Info
sim_ret, resolution, depth_buffer = sim.simxGetVisionSensorDepthBuffer(clientID, visionSensorHandle, sim.simx_opmode_blocking)
depth_img = np.asarray(depth_buffer)
depth_img.shape = (resolution[1], resolution[0])
zNear = 0.01
zFar = 2
depth_img = depth_img * (zFar - zNear) + zNear
depth_img = cv2.flip(depth_img, 0)
return depth_img
while True:
# 获得对象的位置,并输出
ret, arr = sim.simxGetObjectPosition(clientID,targetObj,-1,sim.simx_opmode_blocking)
image = readVisionSensor()
depth = readDepthSensor()
print(depth)
cv2.imshow("image", image)
cv2.imshow("depth", depth)
cv2.waitKey(1)
saveFile = ".\image.jpg" # 保存文件的路径
cv2.imwrite(saveFile, depth) # 保存图像文件
if ret == sim.simx_return_ok:
print(arr)
# time.sleep(2)
# 退出
sim.simxFinish(clientID)
print('Program end')