Question

C++ (VS2013) using the OpenCV library (2.4.9). with help from the tutorial by Kyle Hounslow OpenCV Tutorial: Real-Time Object Tracking Without Colour. I've tried to add the functional too track with 2 cameras.

    //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);
    }

my problem is that the differenceImage1 and differenceImage2 are shown perfectly. But the differenceImage3 is grey.

What could I be doing wrong?

Was it helpful?

Solution

differenceImage1 and differenceImage2 are grayscale images, hence with 1 channel, and you are allocating differenceImage3 with 3 channels (using flag CV_8UC3). This causes the two copyTo calls to allocate new buffers in leftone and rightone instead of using the pre-allocated buffer from differenceImage3, which therefore is never filled.

This should work as expected if you replace:

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

with

Mat differenceImage3(sz1.height, sz1.width + sz2.width, CV_8UC1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top