Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

D405 Setting Exposure in OpenCV #12185

Closed
DaveBGld opened this issue Sep 12, 2023 · 11 comments
Closed

D405 Setting Exposure in OpenCV #12185

DaveBGld opened this issue Sep 12, 2023 · 11 comments
Labels

Comments

@DaveBGld
Copy link

Using cap.set(cv2.CAP_PROP_EXPOSURE, exposureValue) no matter what exposure value (i.e. -4, 100000) does nothing.

I have had to resort to this code:

pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.color, 1280, 720, rs.format.bgr8, 30)
pipeline.start(config)
depth_sensor = pipeline.get_active_profile().get_device().first_depth_sensor()
exposureValue = 100000
depth_sensor.set_option(rs.option.exposure, exposureValue)
pipeline.stop()

camera_index = 4
cap = cv2.VideoCapture(camera_index)

But this code slows the fps to 6 instead of 30....

Specific question: What is the preferred way to set the exposure value in OpenCV Python?

Thanks!

DBG

@MartyG-RealSense
Copy link
Collaborator

MartyG-RealSense commented Sep 13, 2023

Hi @DaveBGld Your FPS is lagging because of the '100000' exposure value that you have set. When setting a manual exposure value, certain value ranges can cause FPS to slow, as described by a RealSense team member at #1957 (comment)

The default exposure value for the D405 is '33000'.

The D405 does not have a separate RGB sensor and so shares its exposure setting with depth. So whatever the depth exposure is manually set to will affect RGB color exposure too. I confirmed in my own tests that FPS is 30 at exposure 33000 and drops to 6 at 100000 exposure.

OpenCV has an exposure command called CAP_PROP_EXPOSURE for altering image brightness, though I note that you have already tried it. The links below have Python examples for implementing it.

https://www.kurokesu.com/main/2020/05/22/uvc-camera-exposure-timing-in-opencv/

https://www.principiaprogramatica.com/2017/06/11/setting-manual-exposure-in-opencv/

opencv/opencv#9738 has advice from others whom CAP_PROP_EXPOSURE did not work for. According to their comments, CAP_PROP_AUTO_EXPOSURE should also be set to '0.25' in order for CAP_PROP_EXPOSURE to work.

@DaveBGld
Copy link
Author

@MartyG-RealSense Thanks for the reply

I tried this:

cap = cv2.VideoCapture(camera_index)
cap.set(cv2.CAP_PROP_AUTO_EXPOSURE, 0.25) # manual mode
cap.set(cv2.CAP_PROP_EXPOSURE, -10) # This should return a black frame

And also

cap.set(cv2.CAP_PROP_AUTO_EXPOSURE, 0.75) # auto mode

before setting manual mode

And also using values 3 and 1 for auto and manual exposure.

Nothing works.

The OpenCV forums point out that these values are back-end (Intel libreaalsense) dependent. People report different values for different cameras...

@MartyG-RealSense
Copy link
Collaborator

MartyG-RealSense commented Sep 13, 2023

The 'native' backend of librealsense is V4L2 by default (RSUSB = false). With the native backend, RealSense camera streams are accessible to standard non-RealSense Linux tools.

@DaveBGld
Copy link
Author

@MartyG-RealSense thanks for that.

Still the code you suggested

cap = cv2.VideoCapture(camera_index)
cap.set(cv2.CAP_PROP_AUTO_EXPOSURE, 0.25) # manual mode
cap.set(cv2.CAP_PROP_EXPOSURE, -10) # This should return a black frame

doesn't appear to have any effect on the camera feed.

Any other ideas?

Thanks,

DBG

@MartyG-RealSense
Copy link
Collaborator

What if you set VideoCapture as '0' instead of camera_index?

cap = cv2.VideoCapture(0)

@DaveBGld
Copy link
Author

@MartyG-RealSense thanks, but I have two cameras connected

@MartyG-RealSense
Copy link
Collaborator

I do not have any other suggestions to offer, unfortunately. I do apologize.

@DaveBGld
Copy link
Author

So the D405 camera is not compatible with OpenCV...

@MartyG-RealSense
Copy link
Collaborator

RealSense cameras are fully compatible with OpenCV.

https://github.com/IntelRealSense/librealsense/tree/master/wrappers/opencv

https://github.com/IntelRealSense/librealsense/blob/master/doc/stepbystep/getting_started_with_openCV.md

Typically, auto-exposure is used with RealSense OpenCV applications rather than manual exposure though.

@DaveBGld
Copy link
Author

If it was fully compatible, my project would not be struggling and stuck on this. :(

I guess the next step is getting some of the OpenCV OAK supported cameras...

Thanks @MartyG-RealSense for the support above

@MartyG-RealSense
Copy link
Collaborator

You are very welcome. I'm sincerely sorry that you did not get the outcome that you were aiming for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants