OpenCV尝试将两个差异图像合二为一

时间:2014-05-08 01:53:56

标签: c++ opencv

使用OpenCV库的C ++(VS2013)(2.4.9)。在Kyle Hounslow OpenCV教程的教程的帮助下:没有颜色的实时对象跟踪。 我试图用2个摄像头添加功能太多的轨道。

    //read first frame
    stream1.read(frame1);
    //read first frame
    stream2.read(frame11);

    //convert frame1 to gray scale for frame differencing
    cv::cvtColor(frame1, grayImage1, COLOR_BGR2GRAY);
    //convert frame1 to gray scale for frame differencing
    cv::cvtColor(frame11, grayImage11, COLOR_BGR2GRAY);


    //copy second frame
    stream1.read(frame2);
    //copy second frame
    stream2.read(frame22);

    //convert frame2 to gray scale for frame differencing
    cv::cvtColor(frame2, grayImage2, COLOR_BGR2GRAY);
    //convert frame2 to gray scale for frame differencing
    cv::cvtColor(frame22, grayImage22, COLOR_BGR2GRAY);

    //perform frame differencing with the sequential images. This will output an "intensity image"
    //do not confuse this with a threshold image, we will need to perform thresholding afterwards.
    cv::absdiff(grayImage1, grayImage2, differenceImage1);
    cv::absdiff(grayImage11, grayImage22, differenceImage2);


    // Match the 2 Images in one
    Size sz1 = differenceImage1.size();                                         //get the size from cam 1
    Size sz2 = differenceImage2.size();                                         //get the size from cam 2

    Mat differenceImage3(sz1.height, sz1.width + sz2.width, CV_8UC3);           //create image 1 and 2

    Mat leftone(differenceImage3, Rect(0, 0, sz1.width, sz1.height));           //parameters for the left side one
    differenceImage1.copyTo(leftone);                                           //copy image 1 in leftone

    Mat rightone(differenceImage3, Rect(sz1.width, 0, sz2.width, sz2.height));  //parameters for the right side one
    differenceImage2.copyTo(rightone);                                          //copy image 2 in rightone


    //threshold intensity image at a given sensitivity value
    cv::threshold(differenceImage3, thresholdImage, SENSITIVITY_VALUE, 255, THRESH_BINARY);
    if (debugMode == true){
        //show the difference image and threshold image
        cv::imshow("Difference Image3", differenceImage3);
        cv::imshow("Difference Image2", differenceImage2);
        cv::imshow("Difference Image1", differenceImage1);
    }

我的问题是differenceImage1和differenceImage2完美显示。 但差异图像3是灰色的。

我可能做错了什么?

1 个答案:

答案 0 :(得分:2)

differenceImage1differenceImage2是灰度图像,因此有1个通道,您正在为differenceImage3分配3个通道(使用标记CV_8UC3)。这导致两个copyTo调用在leftonerightone中分配新缓冲区,而不是使用differenceImage3中预先分配的缓冲区,因此永远不会填充。

如果您替换:

,这应该按预期工作
Mat differenceImage3(sz1.height, sz1.width + sz2.width, CV_8UC3);

Mat differenceImage3(sz1.height, sz1.width + sz2.width, CV_8UC1);