v-rep 入门-和c++client 图像通讯

介绍

在v-rep中设置图像传感器,c++客户端读取图像传感器图像

 

v-rep

v-rep中添加vision_sensor(注意不是camera,camera好像不能读,用同样的代码)

调整分辨率

v-rep 入门-和c++client 图像通讯_第1张图片

c++client


#include "stdafx.h"
#include 
#include 
#include 
#include   
#include 
#include 
using namespace std;
#include 
#include 
#include 
#include 

extern "C" {
#include "extApi.h"
}

int main()
{
	int clientID = simxStart("127.0.0.1", 19997, true, true, 5000, 5);


	if (clientID != -1)
	{
		printf("success");
	}
	else
	{
		printf("error");
	}
	simxStartSimulation(clientID, simx_opmode_oneshot);

	//simxInt leftmotor;
	//simxInt rightmotor;
	//simxGetObjectHandle(clientID, "Leftmotor", &leftmotor,simx_opmode_blocking);
	//simxGetObjectHandle(clientID, "Rightmotor", &rightmotor, simx_opmode_blocking);
	simxInt camera;
	simxGetObjectHandle(clientID, "Vision_sensor", &camera, simx_opmode_blocking);
	simxInt resolution[2];
	simxUChar * image=0;
	cv::namedWindow("opencv test", CV_WINDOW_AUTOSIZE);

	for (int time = 0; time < 10000; time++){
		//Sleep(1000);
		//simxSetJointTargetVelocity(clientID, leftmotor, -time*0.02, simx_opmode_oneshot);
		//simxSetJointTargetVelocity(clientID, rightmotor, -time*0.03, simx_opmode_oneshot);

		int retval = simxGetVisionSensorImage(clientID, camera, resolution, &image, 0, simx_opmode_oneshot);
		if (retval != simx_return_ok) {
			continue;
		}
		cv::Mat channel(resolution[0], resolution[1], CV_8UC3, image);
		//读回来的图像数据是垂直翻转的,问题应该是在cvMat 和 v-rep 垂直坐标轴的方向相反,flip一下就正常了
		cv::flip(channel, channel, 0);
                //读回来的图像数据时rgb通道分布的,而cvMat 默认bgr
		cv::cvtColor(channel, channel, cv::COLOR_RGB2BGR);
		cv::imshow( "opencv test",channel);
		cv::waitKey(10);
	}
	simxStopSimulation(clientID, simx_opmode_oneshot);
	simxFinish(clientID);
	return 0;

}

结果

v-rep 入门-和c++client 图像通讯_第2张图片

你可能感兴趣的:(vrep)