当前位置:网站首页>Obstacle detection
Obstacle detection
2022-07-06 00:54:00 【DDsoup】
Now for me, the most confused place is realsense SDK2.0 How do you use it? , How to combine the code with the image obtained by the camera .
Obstacle detection ideas
- Read image
- Binary processing of the image ( Use open or close operations ), Only detect the range of distance within one meter ( use realsense camera )
- Pass threshold size ( First set the threshold size ),Canny edge detection Obstacles detected , Draw the outline and calculate its convex hull ( I don't think it's reliable to use color as one of the judging elements , In case this object is colorful )
- Generate the final image
Reference code
#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; // The horizontal axis
int nc = image.cols; // The vertical axis
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;
}
}
}
// Find obstacles
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));// The size of the core can be adjusted appropriately
Mat out;
// Carry out the opening operation
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);
// Binarization of the image
threshold(dep, threshold_output, thresh, 255, CV_THRESH_BINARY);
// Contour discovery
findContours(threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
// Calculate the convex hull of each contour
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);
}
// Draw the outline and its convex hull
Mat drawing = Mat::zeros(threshold_output.size(), CV_8UC3);
for (int i = 0; i< contours.size(); i++)
{
if (contourArea(contours[i]) < area)// The area is less than area Convex hull of , Negligible
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)
{
// obtain RGB Images
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();
}
边栏推荐
- Starting from 1.5, build a micro Service Framework - call chain tracking traceid
- Cloud guide DNS, knowledge popularization and classroom notes
- The detailed page returns to the list and retains the original position of the scroll bar
- Recoverable fuse characteristic test
- Getting started with devkit
- 云导DNS和知识科普以及课堂笔记
- What is the most suitable book for programmers to engage in open source?
- The growth path of test / development programmers, the problem of thinking about the overall situation
- Four dimensional matrix, flip (including mirror image), rotation, world coordinates and local coordinates
- Mobilenet series (5): use pytorch to build mobilenetv3 and learn and train based on migration
猜你喜欢

关于#数据库#的问题:(5)查询库存表中每本书的条码、位置和借阅的读者编号

ADS-NPU芯片架构设计的五大挑战

如何制作自己的機器人

Finding the nearest common ancestor of binary search tree by recursion

Convert binary search tree into cumulative tree (reverse middle order traversal)

Folding and sinking sand -- weekly record of ETF

Arduino hexapod robot
![[groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)](/img/09/9076de099147b2d0696fe979a68ada.jpg)
[groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)

ubantu 查看cudnn和cuda的版本

Uniapp development, packaged as H5 and deployed to the server
随机推荐
Mobilenet series (5): use pytorch to build mobilenetv3 and learn and train based on migration
NLP basic task word segmentation third party Library: ICTCLAS [the third party library with the highest accuracy of Chinese word segmentation] [Chinese Academy of Sciences] [charge]
curlpost-php
Convert binary search tree into cumulative tree (reverse middle order traversal)
Cf:c. the third problem
【文件IO的简单实现】
For a deadline, the IT fellow graduated from Tsinghua suddenly died on the toilet
cf:D. Insert a Progression【关于数组中的插入 + 绝对值的性质 + 贪心一头一尾最值】
Opencv classic 100 questions
Spark SQL UDF function
Idea远程提交spark任务到yarn集群
[groovy] compile time metaprogramming (compile time method interception | find the method to be intercepted in the myasttransformation visit method)
Spark SQL null value, Nan judgment and processing
logstash清除sincedb_path上传记录,重传日志数据
Comment faire votre propre robot
测试/开发程序员的成长路线,全局思考问题的问题......
Introduction to robotics I. spatial transformation (1) posture, transformation
Data analysis thinking analysis methods and business knowledge - analysis methods (III)
Uniapp development, packaged as H5 and deployed to the server
Arduino六足机器人