realsense随意显示画框中心的深度,框的顶点的RGB和UV坐标

用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;
}
}

以下是一个简单的代码示例,可以帮助你开启摄像头并使用 OpenCV 识别黄色柱子并绘制边界并输出中心坐标。 ```cpp #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; int main() { VideoCapture cap(0); if (!cap.isOpened()) { cerr << "Error opening the camera" << endl; return -1; } while (true) { Mat frame; cap.read(frame); Mat hsv; cvtColor(frame, hsv, COLOR_BGR2HSV); // Define yellow color range Scalar lower_yellow(20, 100, 100); Scalar upper_yellow(30, 255, 255); // Threshold the image to get only yellow pixels Mat mask; inRange(hsv, lower_yellow, upper_yellow, mask); // Find contours of yellow objects vector<vector<Point>> contours; vector<Vec4i> hierarchy; findContours(mask, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); // Draw bounding boxes around yellow objects for (size_t i = 0; i < contours.size(); i++) { Rect bbox = boundingRect(contours[i]); rectangle(frame, bbox, Scalar(0, 255, 0), 2); Point center(bbox.x + bbox.width / 2, bbox.y + bbox.height / 2); circle(frame, center, 3, Scalar(0, 0, 255), -1); cout << "Center coordinate: (" << center.x << ", " << center.y << ")" << endl; } imshow("frame", frame); if (waitKey(10) == 27) break; } return 0; } ``` 该示例使用 `VideoCapture` 打开摄像头。将每个帧转换为 HSV 颜色空间,并使用 `inRange` 函数将图像中的黄色像素转换为二进制图像。接下来,使用 `findContours` 函数找到黄色对象的轮廓,并使用 `boundingRect` 函数获取对象的边界。最后,在对象的中心处绘制一个小圆圈,并输出中心坐标
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值