0
点赞
收藏
分享

微信扫一扫

障碍物检测

现在对于我来说最困惑的地方就是realsense SDK2.0怎么使用怎么将代码和摄像头获取图像结合起来.

障碍物检测思路

  1. 读入图像
  2. 对图像进行二值处理 (使用开运算或者闭运算),只检测一米内的距离的范围 (用realsense摄像头)
  3. 通过阈值大小(先设置好阈值大小),Canny边缘检测 检测出障碍物,绘制出轮廓且计算其凸包 (我认为通过颜色来作为判断元素之一不太靠谱,万一这个物体五颜六色)
  4. 生成最终图像

参考代码

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "RSWrapper.h"
#include "opencv2/imgproc/imgproc.hpp"
#include <librealsense/rs.hpp>
using namespace rs2;
using namespace std;
using namespace cv;
void mask_depth(Mat &image,Mat& th,int throld=1000)
{
    int nr = image.rows; // 横轴
    int nc = image.cols; // 纵轴 
    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);
    mask_depth(depth, dep, 1000);
    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);
    while (true)
    {
        // 获取RGB图像
        Mat color, depth;
        bool ret = depthCam.capture(color, depth);
        if (!ret) 
        {
            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 条评论