用Opencv显示,代码如下:
#include<opencv2/opencv.hpp>
#include “librealsense2/rs.hpp”
#include “cv-helpers.hpp”
#include
using namespace cv;
using namespace std;
using namespace rs2;
void on_mouse(int EVENT, int x, int y, int flags, void* userdata);
Point startpoint, endpoint;
bool flag = false;
int main()
{
rs2::align align_to(RS2_STREAM_COLOR);
//【1】从摄像头读入视频
//VideoCapture capture(2);//若测试摄像头有没有打开,0是默认camera,realsense相机的彩色camera是2(看设备管理器,从上往下,第一个为0,第三个为2)
//if(!capture.isOpened()) {cout<< “cannot open the camera.”;cin.get();return -1;}
Mat frame; //定义一个Mat变量,用于存储每一帧的图像
// Create a Pipeline, which serves as a top-level API for streaming and processing frames
pipeline p;
// Configure and start the pipeline
p.start();
startpoint.x = 0;
startpoint.y = 0;
endpoint.x = 0;
endpoint.y = 0;
while (1)
{
// Block program until frames arrive
//frameset frames = p.wait_for_frames();
// Try to get a frame of a depth image
//depth_frame depth = frames.get_depth_frame();
frameset data = p.wait_for_frames();
// Make sure the frameset is spatialy aligned
// (each pixel in depth image corresponds to the same pixel in the color image)
frameset aligned_set = align_to.process(data);
depth_frame depth = aligned_set.get_depth_frame();
auto color_mat = frame_to_mat(aligned_set.get_color_frame());
frame = color_mat;
//capture >> frame; //读取当前帧
if (frame.empty())
{
printf("–(!) No captured frame – Break!");
//break;
}
else
{
setMouseCallback(“Rect读取”, on_mouse, &frame);
if(flag)
rectangle(frame, startpoint, endpoint, Scalar::all(0), 2, 8, 0);
imshow(“Rect读取”, frame); //显示当前帧
}
// The frameset might not contain a depth frame, if so continue until it does
if (!depth) continue;
// Get the depth frame’s dimensions
//float width = depth.get_width();
//float height = depth.get_height();
float width = startpoint.x + endpoint.x;
float height = startpoint.y + endpoint.y;
// Query the distance from the camera to the object in the center of the image
float dist_to_center = depth.get_distance(width / 2, height / 2);
// Print the distance
std::cout << “The camera is facing an object " << dist_to_center << " meters away \r”;
//char c = cvWaitKey(33);//获取键盘输入,也是控制视频的播放速度
char c = waitKey(30); //延时30ms,上边那句测试过也可以
if (c == 27) //ESC退出
break;
}
cvDestroyWindow(“Rect读取”);
return 0;
}
void on_mouse(int EVENT, int x, int y, int flags, void* userdata)
{
Mat hh;
hh = (Mat)userdata;
Point p(x, y);
switch (EVENT)
{
case EVENT_LBUTTONDOWN:
{
printf(“b=%d\t”, hh.at§[0]);
printf(“g=%d\t”, hh.at§[1]);
printf(“r=%d\n”, hh.at§[2]);
printf(“DOWN x=%d\t”, x);
printf(“DOWN y=%d\n”, y);
startpoint.x = x;
startpoint.y = y;
flag = false;
}
case EVENT_LBUTTONUP:
{
printf(“b=%d\t”, hh.at§[0]);
printf(“g=%d\t”, hh.at§[1]);
printf(“r=%d\n”, hh.at§[2]);
printf(“UP x=%d\t”, x);
printf(“UP y=%d\n”, y);
endpoint.x = x;
endpoint.y=y;
flag = true;
}
break;
}
}