Change OpenCV video file resolution
Asked Answered
A

1

9

I'm creating a program in OpenCV (2.4.8) which should read video files and do some calculations on them. For these calculations I don't need the high-res frames, I'm perfectly fine with 640*360 as resolution.

In early tests I had my webcam attached and I used:

VideoCapture cap(0);
cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 360);
Mat image;
cap.read(image);
namedWindow("firstframe", 1);
imshow("firstframe", image);
waitKey(0);

Which resized the image perfectly. Now I'm getting to the next step where I want to use my program for stored video instead of a live feed (which I used for testing). When I change the '0' with the source file path (string), the file is loaded, but the resolution remains 1920*1080.

Did I do anything wrong? Is there a way to load the video at a lower resolution 'on the fly'?

I've read the OpenCV documentation. Some of the settings are labeled 'cameras only' but this setting isn't: http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-videocapture

I'm using OpenCV on a mac, and installed it with MacPorts.

Let me know if any more details are needed.

Thanks ahead for your help

edit: I've realised that the cap.set(...) functions return a boolean, so I've tried printing them out and they both return 0. This of course confirms that the frame isn't resized. Still no clue as to why...

edit 2 : So now, for a temporary solution I use the following line after read(image):

resize(image, image, Size(640, 360), 0, 0, INTER_CUBIC);

And this works. But I'm guessing this isn't really the most optimal solution.

Acidimetry answered 3/4, 2014 at 14:30 Comment(2)
when you have a webcam, you can set the resolution of the acquired images, but when you load a your video has the resolution encoded there. The only way I see is to resize the image, as you did.Package
Thanks, I was thinking about the same. The change in resolution is probably done in hardware on the webcams directly. My guess was that ffmpeg would resize on the fly with this option enabled. But then again, that should be the same computation wise as my resize statement.Acidimetry
A
10

using

resize(image, image, Size(640, 360), 0, 0, INTER_CUBIC);

after read(image) seems to be the best solution to solve this problem. So the total (test) code becomes:

VideoCapture cap("path/to/file");
cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 360);
Mat image;
cap.read(image);
resize(image, image, Size(640, 360), 0, 0, INTER_CUBIC);
namedWindow("firstframe", 1);
imshow("firstframe", image);
waitKey(0);

If anyone knows of a better way, please let me know.

Acidimetry answered 4/4, 2014 at 13:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.