# 基于深度摄像头的障碍物检测（realsense+opencv）

2462人阅读 评论(4)

//find_obstacle函数是获取深度图障碍物的函数,返回值是每个障碍物凸包的坐标，参数一depth是realsense返回的深度图（ushort型），
//参数二thresh和参数三max_thresh，是二值化的参数，参数四是凸包的最小有效面积，小于这个面积的障碍物可以视为噪点。
//函数首先筛选掉距离大于安全距离的点，然后进行阀值化和开运算减少一下噪点，用findContours得到轮廓图，最后用convexHull得到每个障碍物的凸包，最后返回坐标

//即为安全，不用考虑。

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "RSWrapper.h"
#include "opencv2/imgproc/imgproc.hpp"

using namespace std;
using namespace cv;
{
int nr = image.rows; // number of rows
int nc = image.cols; // number of columns
for (int i = 0; i<nr; i++)
{

for (int j = 0; j<nc; j++) {
if (image.at<ushort>(i, j)>throld)
th.at<ushort>(i, j) = 0;
}
}

}
vector<vector<Point> > find_obstacle(Mat &depth, int thresh = 20, int max_thresh = 255, int area = 500)
{
Mat dep;
depth.copyTo(dep);
dep.convertTo(dep, CV_8UC1, 1.0 / 16);
//imshow("color", color);
imshow("depth", dep);
Mat element = getStructuringElement(MORPH_RECT, Size(15, 15));//核的大小可适当调整
Mat out;
//进行开操作
morphologyEx(dep, out, MORPH_OPEN, element);
//dilate(dhc, out, element);

//显示效果图
imshow("opencv", out);
Mat src_copy = dep.clone();
Mat threshold_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
RNG rng(12345);
/// 对图像进行二值化
threshold(dep, threshold_output, thresh, 255, CV_THRESH_BINARY);
/// 寻找轮廓
findContours(threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

/// 对每个轮廓计算其凸包
vector<vector<Point> >hull(contours.size());
vector<vector<Point> > result;
for (int i = 0; i < contours.size(); i++)
{
convexHull(Mat(contours[i]), hull[i], false);

}

/// 绘出轮廓及其凸包
Mat drawing = Mat::zeros(threshold_output.size(), CV_8UC3);
for (int i = 0; i< contours.size(); i++)
{
if (contourArea(contours[i]) < area)//面积小于area的凸包，可忽略
continue;
result.push_back(hull[i]);
Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
drawContours(drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point());
drawContours(drawing, hull, i, color, 1, 8, vector<Vec4i>(), 0, Point());
}
imshow("contours", drawing);
return result;
}
int main(int argc, char* argv[])
{
Mat dhc;
Mat dep;
int idxImageRes = 1, idxFrameRate = 30;
RSWrapper depthCam(idxImageRes, idxImageRes, idxFrameRate, idxFrameRate);
if (!depthCam.init())
{
std::cerr << "Init. RealSense Failure!" << std::endl;
return -1;
}

while (true)
{
//Get RGB-D Images
cv::Mat color, depth;
bool ret = depthCam.capture(color, depth);
if (!ret) {
std::cerr << "Get realsense camera data failure!" << std::endl;
break;
}
vector<vector<Point> > result;
result = find_obstacle(depth, 20, 255, 500);

if (cvWaitKey(1) == 27)
break;
}

depthCam.release();
}

0
0

个人资料
• 访问：1886319次
• 积分：16248
• 等级：
• 排名：第770名
• 原创：155篇
• 转载：221篇
• 译文：0篇
• 评论：440条
最新评论