c++ (VS2013)使用OpenCV库(2.4.9)。在Kyle Hounslow教程的帮助下,OpenCV教程:无颜色的实时对象跟踪。我试过用2个摄像头添加too track功能。
//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);
}
我的问题是差异image1和差异image2被完美地显示。但是image3是灰色的
我做错了什么?
differenceImage1
和differenceImage2
是灰度图像,因此具有1个通道,并且您将differenceImage3
分配为3个通道(使用标志CV_8UC3
)。这导致两个copyTo
调用在leftone
和rightone
中分配新的缓冲区,而不是使用从differenceImage3
中预先分配的缓冲区,因此永远不会填充。
如果您替换:
Mat differenceImage3(sz1.height, sz1.width + sz2.width, CV_8UC3);
Mat differenceImage3(sz1.height, sz1.width + sz2.width, CV_8UC1);