Ask Your Question
0

OpenCV 4.1.0 not changing brightness

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

fishy gravatar image

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

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 = cam.read()
r2 = cam.set(11,60)
ret1,img1 = cam.read()
r3 = cam.set(10,70)
print(r1,r2,r3)
print(ret0,ret1)

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

Comments

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 -0600 )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 -0600 )edit

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

StevenPuttemans gravatar imageStevenPuttemans ( 2019-08-21 04:46:58 -0600 )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 -0600 )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 -0600 )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 -0600 )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 -0600 )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 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

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

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.

Before:

cam = cv2.VideoCapture(0)

Now:

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

Comments

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 -0600 )edit
1

Sure thing

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

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 -0600 )edit
1

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 -0600 )edit

Question Tools

1 follower

Stats

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

Seen: 1,729 times

Last updated: Aug 22 '19