如何找到离相机最近的物体?

How to find the closest object to the camera?

我正在研究一种算法,可以找到物体最靠近相机的点。我通过使用 meanshift 在网上找到了一些帮助。我已经粘贴了代码来做到这一点。代码的工作原理是:它使用 track_window 来跟踪最近的对象。但问题是我不知道如何找到最近物体的位置。 我试过的: 我可以让 track_window 遍历整个图像,从而缩小感兴趣的区域,但它太长了,我仍然不知道确切的位置。 有人可以帮我找到相机附近的物体吗? 这是我的代码:

    /*
 * back_project.cpp
 *
 *  Created on: Mar 8, 2015
 *      Author: sushrut
 */

#include "backProject.h"

using namespace cv;
using namespace std;

/// Global Variables
Mat src; Mat hsv; Mat hue;
int bins = 25;
Rect track_window = cv::Rect(250, 250, 50, 50);
int x=1, y=1, wr=1, hr=1;

/// Function Headers
void Hist_and_Backproj(int, void* );

/** @function main */
int back_proj_main()
{
    VideoCapture cap(0);

    if(!cap.isOpened())
    {
        cout << "Cannot open the first web cam" << endl;
        return -1;
    }
    while(true)
    {
        Mat src;
        bool bSuccess = cap.read(src);
        if(!bSuccess)
        {
            cout << "Cannot read a frame from video stream 1" << endl;
            break;
        }

        /// Transform it to HSV
        cvtColor( src, hsv, CV_BGR2HSV );

        /// Use only the Hue value
        hue.create( hsv.size(), hsv.depth() );
        int ch[] = { 0, 0 };
        mixChannels( &hsv, 1, &hue, 1, ch, 1 );

        /// Create Trackbar to enter the number of bins
        char* window_image = "Source image";
        namedWindow( window_image, CV_WINDOW_AUTOSIZE );
        createTrackbar("* Hue  bins: ", window_image, &bins, 180, Hist_and_Backproj );
        Hist_and_Backproj(0, 0);

        // create a rectangle on the image
        rectangle(src, Point(x,y), Point(x+wr, y+hr), Scalar(0, 250, 0));
        /// Show the image
        imshow( window_image, src );

        if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
        {
            cout << "esc key is pressed by user" << endl;
            break;
        }
    }
    // TODO release the capture
    return 0;
}


/**
 * @function Hist_and_Backproj
 * @brief Callback to Trackbar
 */
void Hist_and_Backproj(int, void* )
{
    MatND hist;
    int histSize = MAX( bins, 2 );
    float hue_range[] = { 0, 180 };
    const float* ranges = { hue_range };

    /// Get the Histogram and normalize it
    calcHist( &hue, 1, 0, Mat(), hist, 1, &histSize, &ranges, true, false );
    normalize( hist, hist, 0, 255, NORM_MINMAX, -1, Mat() );

    /// Get Backprojection
    MatND backproj;
    calcBackProject( &hue, 1, 0, hist, backproj, &ranges, 1, true );

    /// Draw the backproj
    imshow( "BackProj", backproj );

    // code to create a meanshift
    meanShift(backproj,track_window, TermCriteria(TermCriteria::COUNT|TermCriteria::EPS, 10, 1));

    // update the value of recctange
    x = track_window.x;
    y = track_window.y;
    wr = track_window.width;
    hr = track_window.height;
    cout << "values: " << x << ", " << y << ", " << wr << ", " << hr << endl;

    /// Draw the histogram
    int w = 400; int h = 400;
    int bin_w = cvRound( (double) w / histSize );
    Mat histImg = Mat::zeros( w, h, CV_8UC3 );

    for( int i = 0; i < bins; i ++ )
    { rectangle( histImg, Point( i*bin_w, h ), Point( (i+1)*bin_w, h - cvRound( hist.at<float>(i)*h/255.0 ) ), Scalar( 0, 0, 255 ), -1 ); }

    imshow( "Histogram", histImg );
}

参考文献: http://www.opencv.org.cn/opencvdoc/2.3.1/html/doc/tutorials/imgproc/histograms/back_projection/back_projection.html

一般情况下,如果你只有一帧和一个摄像头-物体相对位置,是不可能检测到哪个物体最接近摄像头的。 您应该处理视频序列并跟踪特征点以确定对象在相机坐标系中的位置。

你应该研究一些关于多视图几何的材料。例如,YouTube 上 Daniel Cremers 教授的讲座。哈特利和齐瑟曼的书也很好

看完评论更新

不清楚你的意思。它们是图像中对象的像素坐标还是 3D 欧几里德中的点 space?

如果是第一个,那么您可以进行以下操作。 Backproject returns 你的图像,其中每个像素包含属于对象的概率。您可以设置此图像的阈值以排除非常低的概率,从噪声中过滤结果,使用形态学运算符(侵蚀、闭合)进行细化,从而获得一团像素,很可能属于您的对象。然后你可以找到它的边界矩形。如果你不想使用 backproject,因为它并不总能找到对象,那么你可以使用 OpenCV 中的 2D feature detectors and matchers。他们可以估计属于该对象的一些 "extreme" 个点的位置。

在第二种情况下,任务看起来像是 SLAM(同步定位和映射)的任务。这是一个更复杂的主题。我再次向您推荐多视图几何和 SLAM 课程。此任务还大量使用 2D 特征点。