5

It seems that the standard method for setting the resolution on a webcam in Java opencv doesn't work. I do the following:

VideoCapture v = new VideoCapture();

boolean wset = v.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, 1280);
boolean hset = v.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, 800);

System.out.println(wset);
System.out.println(hset);

v.open(1);

Which prints:

> false
> false

...and doesn't change the camera resolution. It appears to be stuck at 640x480. I know that the camera is not at fault because I can successfully set the resolution to 1280x800 using the C++ bindings.

Also - v.getSupportedPreviewSizes() doesn't work. It returns an error:

HIGHGUI ERROR: V4L2: getting property #1025 is not supported

Any thoughts?

mayhewsw
  • 704
  • 9
  • 20

5 Answers5

5

You need to open the camera first then set it. I tried it before camera.open(0) and it just returns to standard settings, but when try setting after camera.open(0) it works.

so do this in your code

v.open(1)
boolean wset = v.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, 1280);
boolean hset = v.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, 800);
Dave P
  • 156
  • 1
  • 8
2

If you want your camera to run, You first need to open your capture device:

    VideoCapture v = new VideoCapture(0); 

//add 0 as paramater in the constructor.This is the index of your capture device.

    boolean wset = v.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, 1280);
    boolean hset = v.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, 800);

    System.out.println(wset);
    System.out.println(hset);

And v.getSupportedPreviewSizes() doesnt work use this instead:

v.get(Highgui.CV_CAP_PROP_FRAME_WIDTH); //to get the actual width of the camera
v.get(Highgui.CV_CAP_PROP_FRAME_HEIGHT);//to get the actual height of the camera
neoprez
  • 349
  • 2
  • 11
  • Oh, I have been able to see the video - I just didn't mark that step. I'll edit it. The problem is still there. Also, every time I do `v.get(...)` it returns 640 and 480, regardless of what I have set it to. – mayhewsw Jan 11 '14 at 22:45
2

Just pass camera number with the constructor and set width and height...

webCamera = new VideoCapture(0);
webCamera.set(Highgui.CV_CAP_PROP_FRAME_WIDTH,1280);
webCamera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, 720);

There you go....

Gopal00005
  • 2,061
  • 4
  • 35
  • 54
1

Instead use Highgui to set resolution, use property ID, where 3 is to WIDTH and 4 is to HEIGHT, so your code will be like:

VideoCapture v = new VideoCapture();
boolean wset = v.set(3, RESOLUTION_WIDTH);
boolean hset = v.set(4, RESOLUTION_HEIGHT);

System.out.println(wset);
System.out.println(hset);

v.open(cameraID);

Other property ids you can find at Setting Camera Parameters in OpenCV/Python

Community
  • 1
  • 1
1

On the new OpenCV 4.5 you should use something like:

import org.opencv.videoio.Videoio;
...
this.capture.set(Videoio.CAP_PROP_FRAME_WIDTH, 640);
this.capture.set(Videoio.CAP_PROP_FRAME_HEIGHT, 480);
ariel
  • 253
  • 5
  • 9