OpenCV计算距离(立体视觉) [英] OpenCV calculate distance (stereo vision)

查看:474
本文介绍了OpenCV计算距离(立体视觉)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的项目,我正在使用下一部分代码的一部分:

For my project I am using parts of the next code: link.

要跟踪特定颜色的对象,我实现了此方法:

To track objects of a specific color I implemented this method:

我的问题是:如何计算到跟踪的彩色物体的距离?

My question is: How can I calculate the distance to the tracked colored objects?

提前谢谢!

*应用程序调用左右框架的方法.这效率不高... **我需要计算detectedObject.Zcor

*The application calls the method for the left and right frame. This is not efficient... **I need to calculate detectedObject.Zcor

DetectedObject Detect(IplImage *frame)
{
 //Track object (left frame and right frame)
 //Calculate average position
 //Show X,Y,Z coordinate and detected color

color_image = frame;

imgThreshold = cvCreateImage(cvSize(color_image->width,color_image->height), IPL_DEPTH_8U, 1);
cvInitFont(&font, CV_FONT_HERSHEY_PLAIN, 1, 1, 0, 1.4f, CV_AA);

imgdraw = cvCreateImage(cvGetSize(color_image),8,3);
cvSetZero(imgdraw);

cvFlip(color_image, color_image, 1);

cvSmooth(color_image, color_image, CV_GAUSSIAN, 3, 0);

threshold = getThreshold(color_image);
cvErode(threshold, threshold, NULL, 3);
cvDilate(threshold, threshold, NULL, 10);
imgThreshold = cvCloneImage(threshold);

storage = cvCreateMemStorage(0);
contours = cvCreateSeq(0, sizeof(CvSeq), sizeof(CvPoint), storage);
cvFindContours(threshold, storage, &contours, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE, cvPoint(0,0));
final = cvCreateImage(cvGetSize(color_image),8,3);

for(; contours!=0; contours = contours->h_next)
{
    CvRect rect = cvBoundingRect(contours, 0);  

    cvRectangle(color_image, 
        cvPoint(rect.x, rect.y),
        cvPoint(rect.x+rect.width, rect.y+rect.height),
        cvScalar(0,0,255,0),
        2,8,0);

    string s = to_string(rect.x) + "," +  to_string(rect.y);
    char const* pchar = s.c_str();

    cvPutText(frame, pchar, cvPoint(rect.x, rect.y), &font, cvScalar(0,0,255,0));
    detectedObject.Xcor = rect.x;
    detectedObject.Ycor = rect.y;
}

cvShowImage("Threshold", imgThreshold);

cvAdd(final,imgdraw,final);
detectedObject.Zcor = 0;
return detectedObject;

}

推荐答案

要进行深度估计,您将需要校准的立体声对(左右相机的已知相机矩阵).然后,使用相机矩阵和立体声对中的相应点/轮廓,可以计算深度.

For depth estimation you will need a calibrated stereo pair (known camera matrices for both the left and the right cameras). Then, using the camera matrices and corresponding points/contours in the stereo pair, you can compute depth.

这篇关于OpenCV计算距离(立体视觉)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆