3

When you read in an image, there is a flag you can set to 0 to force it as grayscale.

cv::Mat img = cv::imread(file, 0); // keeps it grayscale

Is there an equivalent for videos?

zebra
  • 6,373
  • 20
  • 58
  • 67

1 Answers1

6

There's not.

You need to query the frames and convert them to grayscale yourself.

Using the C interface: https://stackoverflow.com/a/3444370/176769

With the C++ interface:

VideoCapture cap(0);
if (!cap.isOpened())
{
    // print error msg
    return -1;
}

namedWindow("gray",1);

Mat frame;
Mat gray;
for(;;)
{
    cap >> frame;

    cvtColor(frame, gray, CV_BGR2GRAY);

    imshow("gray", gray);
    if(waitKey(30) >= 0) 
        break;
}

return 0;
Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • Sure declaring `gray` outside of the `for` scope, together with `frame`, will prevent the expensive memory allocation and deallocation of this large image every frame. Fixing that would be worth an up-vote. – Christian Rau Dec 07 '11 at 15:33