1

I am trying to capture video into a Mat type from two or more MSFT LifeCam HD-3000s using the videoInput library, OpenCV 2.4.3, and VS2010 Express.

I followed the example at: Most efficient way to capture and send images from a webcam in a network and it worked great.

Now I want to replace the IplImage type with a c++ Mat type. I tried to follow the example at: opencv create mat from camera data

That gave me the following:

    VI = new videoInput;
    int CurrentCam = 0;
    VI->setupDevice(CurrentCam,WIDTH,HEIGHT);
    int width = VI->getWidth(CurrentCam);
    int height = VI->getHeight(CurrentCam);
    unsigned char* yourBuffer = new unsigned char[VI->getSize(CurrentCam)];
    cvNamedWindow("test",1);
    while(1)
    {
       VI->getPixels(CurrentCam, yourBuffer, false, true);
       cv::Mat image(width, height, CV_8UC3, yourBuffer, Mat::AUTO_STEP);
       imshow("test", image); 
       if(cvWaitKey(15)==27) break;
    }

The output is a lined image (i.e., it looks like the first line is correct but the second line seems off, third correct, fourth off, etc). That suggests that either the step part is wrong or there is some difference between the IplImage type and the Mat type that I am not getting. I have tried looking at/altering all the parameters, but I can't find anything.

Hopefully, an answer will help those facing what appears to be a fairly common issue with loading an image form the videoInput library to the Mat type.

Thanks in advance!

Community
  • 1
  • 1
user1805103
  • 127
  • 2
  • 19

1 Answers1

1

Try

cv::Mat image(height, width, CV_8UC3, yourBuffer, Mat::AUTO_STEP);
luhb
  • 656
  • 5
  • 4
  • Thanks so much! It works perfect now. The second example uses width-height order but uses a Size struct for the constructor. That threw me here. – user1805103 Nov 07 '12 at 18:03
  • The rows-cols manner do confuse those who's used to width-height. – luhb Nov 08 '12 at 03:26