Ask Your Question

OpenCV 4.1.0 not changing brightness

asked 2019-08-19 16:33:38 -0500

fishy gravatar image

updated 2019-08-20 13:37:53 -0500

I made a program that can change the brightness/contrast of the camera by pressing the arrow keys, while showing a live feed of what the camera sees. Since I only used one camera, I initiated it using cv2.VideoCapture(-1), and it worked fine.

However, when I tried to add a second camera to the program, I ran into an issue. The two cameras can be found at /dev/video0 and /dev/video1. When I changed the first camera to use cv2.VideoCapture(0), the program stopped working. After further testing, whenever I try to change any setting on a camera initialized with a value other than -1, the property is set to zero. If I adjust a property twice (typically brightness or contrast) before taking a picture, I get an error (libv4l2: error setting pixformat: Device or resource busy). If I try to do anything with the camera after that, it fails entirely. Using cam.set() returns false no matter what property/value I provide.

These issues only show up using cv2.VideoCapture(0) and cv2.VideoCapture(1), but cv2.VideoCapture(-1) always works. I've tried keeping only one camera plugged in and using cv2.VideoCapture(0) and it still fails, and if I don't try to set any properties I can successfully take an image from both cameras, so I don't believe bandwidth is an issue.

Edit: I simplified the code to figure out what was causing the issue. This is what I am currently using:

import cv2

cam = cv2.VideoCapture(0)
r1 = cam.set(10,55)
ret0,img0 =
r2 = cam.set(11,60)
ret1,img1 =
r3 = cam.set(10,70)

The output was as follows:

libv4l2: error setting pixformat: Device or resource busy
(True, True, False)
(True, False)

If I only took one picture, or only changed one setting, there was not an error.

edit retag flag offensive close merge delete


Please add your code, and tell us something on how both camera's are connected to the pc. If they are both on the same physical USB bus, that can render issues.

StevenPuttemans gravatar imageStevenPuttemans ( 2019-08-20 07:09:19 -0500 )edit

At first, the USBs were in the same bus. Since I wasn't sure if that was causing the issue, I actually unplugged one. Even with just one camera plugged in, the program still failed to work unless I used -1 as the index. If I wanted to constantly take pictures and never change a setting, it worked fine though.

fishy gravatar imagefishy ( 2019-08-20 13:39:18 -0500 )edit

Can you please use the correct flags instead of integer values, as described here?

StevenPuttemans gravatar imageStevenPuttemans ( 2019-08-21 04:46:58 -0500 )edit

Do you mean using cv2.CAP_PROP_BRIGHTNESS instead of 10, and the same for contrast? It didn't make a difference

fishy gravatar imagefishy ( 2019-08-21 08:14:53 -0500 )edit

But it tells us what you are trying to change. You cannot expect us to dig into the code to figure it out. Are you sure your camera's allow setting these values through software?

StevenPuttemans gravatar imageStevenPuttemans ( 2019-08-22 09:29:30 -0500 )edit

Yes. Sorry, that was not my intention, and I thought I specified what parameters I was changing. If I used cv2.VideoCapture(-1), every setting was able to be adjusted correctly. When I changed to using cv2.VideoCapture(0), the program stopped working. At that time, only one camera was plugged in and could be found at /dev/video0.

fishy gravatar imagefishy ( 2019-08-22 09:34:16 -0500 )edit

can you add between each read() operation a wait? I know that when grabbing frames in a loop, in C++ I always add a waitKey(1) or a waitKey(10), which is in milliseconds. It can be that the second grab is happening at the same time the first is finishing and thus the driver isn't released yet. In that case, the program can crash. Grabbing frames in two seperate threads can also solve this issue I experienced, but thats a more heavy workaround.

StevenPuttemans gravatar imageStevenPuttemans ( 2019-08-22 09:51:08 -0500 )edit

I figured out how to get it to work (answer below). For what it's worth, though, adding a wait did not work. I tried adding waitKey(500) between each line of code with no effect in the output. An index of -1 still worked, and an index of 0 still failed. Thank you for your time!

fishy gravatar imagefishy ( 2019-08-22 12:10:35 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-08-22 12:08:48 -0500

fishy gravatar image

I changed how I initialized the camera and it works now. I'm not sure why this was able to fix the issue, but I thought I'd put it up in case anybody else has the same problem.


cam = cv2.VideoCapture(0)


cam = cv2.VideoCapture("/dev/video0")
edit flag offensive delete link more


My guess is the internal translation of the indexes is somehow broken ... could you maybe open up an issue at github with all your system details so people can have a look at this?

StevenPuttemans gravatar imageStevenPuttemans ( 2019-08-23 08:00:42 -0500 )edit

Sure thing

fishy gravatar imagefishy ( 2019-08-23 09:10:36 -0500 )edit

The issue apparently was that VideoCapture was using the wrong backend. If I used cv2.VideoCapture(0, cv2.CAP_V4L) the code worked correctly. Based on the documentation, I hadn't realized this was even possible.

fishy gravatar imagefishy ( 2019-08-27 16:07:09 -0500 )edit

The documentation is indeed not that clear. We might want to propose a pull request with a chance on that level.

StevenPuttemans gravatar imageStevenPuttemans ( 2019-08-28 01:54:09 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-08-19 16:33:38 -0500

Seen: 269 times

Last updated: Aug 22 '19