2015-03-08 39 views
0

我需要从两个不同的相机中检测蓝色物体和红色物体,现在需要的任务是在3D空间中定位每个物体位置,这意味着对于每个物体我们必须有它的x,y,z坐标,我已经看到this videohere女巫确实是我想要做的,但没有示例代码的情况下,第一个视频,我的代码看起来像这样,现在它得到我x,y的红色/蓝色的对象,但没有深度:在OpenCV中获取Z“深度”的简单方法

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

using namespace cv; 
using namespace std; 

int main(int argc, char** argv) 
//int func(int argc, char** argv) 
{ 
    VideoCapture cap(0); //capture the video from webcam 
    VideoCapture cap1(1); //capture the video from extrenal camers 
    if (!cap.isOpened()) // if not success, exit program 
    { 
     cout << "Cannot open the web cam" << endl; 
     return -1; 
    } 

    if (!cap1.isOpened()) // if not success, exit program 
    { 
     cout << "Cannot open the External camera" << endl; 
     return -1; 
    } 

    namedWindow("Control", CV_WINDOW_AUTOSIZE); //create a window called "Control" 

    int iLowH = 170; 
    int iHighH = 179; 
    int iLowS = 150; 
    int iHighS = 255; 
    int iLowV = 60; 
    int iHighV = 255; 

    //Create trackbars in "Control" window to control the range of red detection 
    createTrackbar("LowH", "Control", &iLowH, 179); //Hue (0 - 179) 
    createTrackbar("HighH", "Control", &iHighH, 179); 
    createTrackbar("LowS", "Control", &iLowS, 255); //Saturation (0 - 255) 
    createTrackbar("HighS", "Control", &iHighS, 255); 
    createTrackbar("LowV", "Control", &iLowV, 255);//Value (0 - 255) 
    createTrackbar("HighV", "Control", &iHighV, 255); 

    int iLastX = -1; //last known co-ordinates of red object 
    int iLastY = -1; 
    int iLastX1 = -1; 
    int iLastY1 = -1; 

    //Capture a temporary image from both cameras to obtain size 
    Mat imgTmp; 
    cap.read(imgTmp); 
    cap1.read(imgTmp); 

    //Create a black image with the size as the camera output 
    Mat imgLines = Mat::zeros(imgTmp.size(), CV_8UC3);; 
    Mat imgLines1 = Mat::zeros(imgTmp.size(), CV_8UC3);; 

    //loop of continuously capturing frames from video 
    while (true) 
    { 
     Mat imgOriginal; 

     Mat imgOriginal1; 
     bool bSuccess = cap.read(imgOriginal); // read a new frame from video webcam 
     bool bSuccess1 = cap1.read(imgOriginal1); // read a new frame from video external cam 

     if (!bSuccess || !bSuccess1) //if not success, break loop 
     { 
      cout << "Cannot read a frame from video stream" << endl; 
      break; 
     } 
     //WebCam code for image and tracking/detecting 
     Mat imgHSV; 
     cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV to control range of color to obtain and be able to detect it 
     Mat imgThresholded; 
     inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); //Threshold the image at the colors within specified range 
     //morphological opening (removes noise and similar colored objects appearing in thresholded image) 
     erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5))); 
     dilate(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5))); 

     //morphological closing (removes noise appearing inside our object in the thresholded image) 
     dilate(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5))); 
     erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5))); 
     //Calculate the moments of the thresholded image to calculate the object position 
     Moments oMoments = moments(imgThresholded); 
     double dM01 = oMoments.m01; 
     double dM10 = oMoments.m10; 
     double dArea = oMoments.m00; 

     // if the area <= 10000, I consider that the there are no object in the image and it's because of the noise, the area is not zero 
     if (dArea > 10000) 
     { 
      //calculate the position of the ball 
      int posX = dM10/dArea; 
      int posY = dM01/dArea; 

      if (iLastX >= 0 && iLastY >= 0 && posX >= 0 && posY >= 0) 
      { 
       //Draw a red line from the previous point to the current point 
       line(imgLines, Point(posX, posY), Point(iLastX, iLastY), Scalar(0, 0, 255), 2); 
      } 

      iLastX = posX; //current point becomes last known point and loop continues 
      iLastY = posY; 
     } 
     imshow("Thresholded Image", imgThresholded); //show the thresholded image 
     imgOriginal = imgOriginal + imgLines; 
     imshow("Original", imgOriginal); //show the original image with the tracking lines if exist 
     //External Cam code track/detect 
     Mat imgHSV1; 
     cvtColor(imgOriginal1, imgHSV1, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV 
     Mat imgThresholded1; 
     inRange(imgHSV1, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded1); //Threshold the image 
     //morphological opening (removes noise and similar colored objects appearing in thresholded image) 
     erode(imgThresholded1, imgThresholded1, getStructuringElement(MORPH_ELLIPSE, Size(5, 5))); 
     dilate(imgThresholded1, imgThresholded1, getStructuringElement(MORPH_ELLIPSE, Size(5, 5))); 
     //morphological closing (removes noise appearing inside our object in the thresholded image) 
     dilate(imgThresholded1, imgThresholded1, getStructuringElement(MORPH_ELLIPSE, Size(5, 5))); 
     erode(imgThresholded1, imgThresholded1, getStructuringElement(MORPH_ELLIPSE, Size(5, 5))); 

     //Calculate the moments of the thresholded image to calculate the object position 
     Moments oMoments1 = moments(imgThresholded1); 
     double dM011 = oMoments1.m01; 
     double dM101 = oMoments1.m10; 
     double dArea1 = oMoments1.m00; 

     // if the area <= 10000, I consider that the there are no object in the image and it's because of the noise, the area is not zero 
     if (dArea1 > 10000) 
     { 
      //calculate the position of the ball 
      int posX1 = dM101/dArea1; 
      int posY1 = dM011/dArea1; 

      if (iLastX1 >= 0 && iLastY1 >= 0 && posX1 >= 0 && posY1 >= 0) 
      { 
       //Draw a red line from the previous point to the current point 
       line(imgLines1, Point(posX1, posY1), Point(iLastX1, iLastY1), Scalar(0, 0, 255), 2); 
      } 

      iLastX1 = posX1; 
      iLastY1 = posY1; 
     } 

     imshow("Thresholded Image 2", imgThresholded1); //show the thresholded image 
     imgOriginal1 = imgOriginal1 + imgLines1; 
     imshow("Original 2", imgOriginal1); //show the original image 

     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; 
     } 
    } 
    return 0; 
} 
+1

第二个链接有一个到源代码的github页面的链接。我不明白,你不能发现,但你说它做你想做的事... – GPPK 2015-03-08 19:49:32

+0

我知道这一点,但它需要安装额外的软件“机器人操作系统”,但我也认为它可以在Cpp和opencv中完成。 – 2015-03-08 20:42:23

回答

1

您的问题的答案将是立体视觉。您需要对两台摄像机进行立体校准,以获得转换矩阵,以便从2个视图中生成场景的深度图。 OpenCV提供some functions来做到这一点。

Here是一个教程开始。