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.
Using 'set()' class, we can set the height and width of the frames. The following lines are setting the height and width of the video in our program. set(CAP_PROP_FRAME_WIDTH, 320); set(CAP_PROP_FRAME_HEIGHT, 240);
The first step is to create an object of the DNN superresolution class. This is followed by the reading and setting of the model, and finally, the image is upscaled. We have provided the Python and C++ codes below. You can replace the value of the model_path variable with the path of the model that you want to use.
FourCC is a 4-byte code used to specify the video codec. List of codes can be obtained at Video Codecs by FourCC. The codecs for Windows is DIVX and for OSX is avc1, h263. FourCC code is passed as cv2. VideoWriter_fourcc(*'MJPG') for MJPG and cv2.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With