Opencv Revert back to a higher resolution than what I set.

asked 2020-07-01 14:46:11 -0600

JeffTheMess gravatar image

I am having an issue with saving images. I want to start with I do not want to resize the image after I receive it, I want the webcam to start with a native lower resolution which my camera supports. I am on Raspberry Pi OS. The supported resolutions supported.

pi@raspberrypi:~/Desktop $ v4l2-ctl --list-formats-ext
ioctl: VIDIOC_ENUM_FMT
Type: Video Capture

[0]: 'YUYV' (YUYV 4:2:2)
    Size: Discrete 640x480
        Interval: Discrete 0.033s (30.000 fps)
    Size: Discrete 352x288
        Interval: Discrete 0.033s (30.000 fps)
    Size: Discrete 320x240
        Interval: Discrete 0.033s (30.000 fps)
    Size: Discrete 176x144
        Interval: Discrete 0.033s (30.000 fps)
    Size: Discrete 160x120
        Interval: Discrete 0.033s (30.000 fps)

I tried to changed the resolution via terminal and via my c++ code with the following.

in C++

stream.set(cv::CAP_PROP_FRAME_WIDTH, 320);
stream.set(cv::CAP_PROP_FRAME_HEIGHT, 240);

in terminal I changed and checked the resolution

pi@raspberrypi:~/Desktop $ v4l2-ctl -d0 --set-fmt-video=width=320,height=240
pi@raspberrypi:~/Desktop $ v4l2-ctl -d0 --get-fmt-video | egrep 'Pixel Format|Width/Height'
Width/Height      : 320/240
Pixel Format      : 'YUYV' (YUYV 4:2:2)
pi@raspberrypi:~/Desktop $ v4l2-ctl -d2 --set-fmt-video=width=320,height=240
pi@raspberrypi:~/Desktop $ v4l2-ctl -d2 --get-fmt-video | egrep 'Pixel Format|Width/Height'
Width/Height      : 320/240
Pixel Format      : 'YUYV' (YUYV 4:2:2)
pi@raspberrypi:~/Desktop $ v4l2-ctl -d4 --set-fmt-video=width=320,height=240
pi@raspberrypi:~/Desktop $ v4l2-ctl -d4 --get-fmt-video | egrep 'Pixel Format|Width/Height'
Width/Height      : 320/240
Pixel Format      : 'YUYV' (YUYV 4:2:2)

When I go to run my code, it always saves at a higher resolution of 640x480. I am running 3 pthreads one for each thread. There isn't anything inherently wrong with the code, it saves but at the higher resolution. But when I run something like streamer -f jpeg -o image.jpeg It saves correctly

Simple example of c++ code which doesn't save at resolution of 320/240 but it does save at 640x480. Running the egrep code above also shows the width/height as revert back to 640/480. So I am sure OpenCV isn't playing nice when I change these values.

#include <stdio.h>
#include <stdlib.h>
#include <thread>
#include <iostream>
#include <fstream>

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <string>
#include <boost/date_time/posix_time/posix_time.hpp>


int main(){
int i = 0;
    cv::VideoCapture stream(0);
    stream.set(cv::CAP_PROP_FRAME_WIDTH, 320);
    stream.set(cv::CAP_PROP_FRAME_HEIGHT, 240);
    cv::Mat3b Frame;
    if(!stream.isOpened())
{
    std::cout << "Cannot Open Camera: " + 0 + '\n';
}
else
{
    std::cout << "Camera Open: " << 0 + '\n';
}

    while (true)
    {
        stream.open(0);
        const boost::posix_time::ptime now = 
            boost::posix_time::microsec_clock::local_time();
        const boost::posix_time::time_duration td = now.time_of_day();
        const long year         = now.date().year();
        const long month        = now.date().month();
        const long day          = now.date().day();
        const long hours        = td.hours();
        const long minutes      = td.minutes();
        const long seconds      = td.seconds();
        const long milliseconds = td.total_milliseconds() -
                                  ((hours * 3600 + minutes * 60 + seconds) * 1000);

        char buf[80];
        sprintf ...
(more)
edit retag flag offensive close merge delete

Comments

Used cv:: resizer()

supra56 gravatar imagesupra56 ( 2020-07-01 21:38:38 -0600 )edit

Thanks but I already said I do not want to resize it, the webcam can default to a lower resolution. I am running multiple threads so I need a lower resolution to start with.

JeffTheMess gravatar imageJeffTheMess ( 2020-07-02 04:34:24 -0600 )edit

snippet:

$ uvcdynctrl -f
Listing available frame formats for device video0:
Pixel format: YUYV (YUYV 4:2:2; MIME type: video/x-raw-yuv)
  Frame size: 640x480
    Frame rates: 30, 20, 10
  Frame size: 352x288
    Frame rates: 30, 20, 10
  Frame size: 320x240
    Frame rates: 30, 20, 10
  Frame size: 176x144
    Frame rates: 30, 20, 10
  Frame size: 160x120
    Frame rates: 30, 20, 10
supra56 gravatar imagesupra56 ( 2020-07-02 08:37:58 -0600 )edit

My Cam

pi@raspberrypi:~/Desktop $ uvcdynctrl -f
Listing available frame formats for device video0:
Pixel format: YUYV (YUYV 4:2:2; MIME type: video/x-raw-yuv)
Frame size: 640x480
Frame rates: 30
Frame size: 352x288
Frame rates: 30
Frame size: 320x240
Frame rates: 30
Frame size: 176x144
Frame rates: 30
Frame size: 160x120
Frame rates: 30
JeffTheMess gravatar imageJeffTheMess ( 2020-07-02 21:21:40 -0600 )edit