zebra
zebra

Reputation: 6531

How to read in a video file as grayscale

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?

Upvotes: 3

Views: 9151

Answers (1)

karlphillip
karlphillip

Reputation: 93410

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;

Upvotes: 6

Related Questions