twVolc
Posts: 21
Joined: Wed Mar 16, 2016 11:41 am

Controlling analog gain on either v1.3 or v2 cameras

Thu Aug 11, 2016 11:15 am

From what I have read before, the only way to set/fix analog gain in the v1.3 camera is by turning exposure mode to 'off'. However, I would like to actually be able to tell the camera what gain to use, as I may initially be taking a dark image, and then taking bright images - with this process, the gain is large for the dark image, and so if I then fix, the bright image just saturates. I should state that I am working with the RAW images, so as far as I am aware, analog gain is the only thing that can be affecting the images if shutter speed is fixed.

I'm wondering now if the v2 cameras might have brought something new to the table in this area? Is there any way of setting the gain - it may not be in the python Picamera module, but perhaps if I delve deeper into the source code will there be anywhere that makes this possible, or is it just a limitation of the hardware?

A brief add-on question - if I set exposure mode to 'off' to fix the analog gain, can I still then adjust the shutter speed whilst the gain remains in its fixed state? This is probably pretty easy to test but I haven't got around to trying it yet.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5536
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Controlling analog gain on either v1.3 or v2 cameras

Thu Aug 11, 2016 11:32 am

http://picamera.readthedocs.io/en/relea ... Camera.iso
Minor error in the docs as only values of 100, 200, 400, and 800 are actually supported.

Daft as it'll sound, setting exposure mode to "off" will mean that your requested ISO mode gain is not requested. The ISO modes are just alternative automatic modes where the gain is (nearly) fixed at the appropriate point (There's a small amount of wiggle room still to allow for flicker avoidance to get reasonable results).
Yes, you should still be able to set the shutter speed explicitly even with ISO set, or with exposure mode off (that's the only way then).

I have had thoughts on occasion of trying to add explicit analogue and digital gain parameters to the camera component, but have never got there due to other projects/issues. I'll see if I can build some motivation to do some hacking to the camera component again. If exposure mode is off, then it's just a matter of plumbing.

All of this should be common between V1.3 and V2.1 cameras. If not, then it's a bug.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

pootle
Posts: 261
Joined: Wed Sep 04, 2013 10:20 am
Location: Staffordshire
Contact: Website

Re: Controlling analog gain on either v1.3 or v2 cameras

Thu Aug 11, 2016 1:09 pm

6by9 wrote:I'll see if I can build some motivation to do some hacking to the camera component again. If exposure mode is off, then it's just a matter of plumbing.
Ooh! that would be so nice - especially if the gains can get to the equivalent of iso1600. I have found with camera V2 that 10 seconds & iso800 is not quite enough sensitivity - another stop would help very much.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5536
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Controlling analog gain on either v1.3 or v2 cameras

Thu Aug 11, 2016 1:58 pm

pootle wrote:Ooh! that would be so nice - especially if the gains can get to the equivalent of iso1600. I have found with camera V2 that 10 seconds & iso800 is not quite enough sensitivity - another stop would help very much.
They calibrated IMX219 so that ISO100 is x1.84 gain (reading the config), so you may not be able to go as high as you are expecting. Generally going over x2.0 digital gain is bad news for the image quality due to noise.
Analogue gain is likely to be limited to x8.0 by the sensor hardware.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

twVolc
Posts: 21
Joined: Wed Mar 16, 2016 11:41 am

Re: Controlling analog gain on either v1.3 or v2 cameras

Thu Aug 11, 2016 3:21 pm

6by9 wrote: Daft as it'll sound, setting exposure mode to "off" will mean that your requested ISO mode gain is not requested. The ISO modes are just alternative automatic modes where the gain is (nearly) fixed at the appropriate point (There's a small amount of wiggle room still to allow for flicker avoidance to get reasonable results).
Yes, you should still be able to set the shutter speed explicitly even with ISO set, or with exposure mode off (that's the only way then).
When you say a small amount of wiggle room, do you know how significant this is? I'm using the cameras for remote sensing applications, and essentially any adjustment is not good, as we need truly consistent images, or as close as possible.
I have had thoughts on occasion of trying to add explicit analogue and digital gain parameters to the camera component, but have never got there due to other projects/issues. I'll see if I can build some motivation to do some hacking to the camera component again. If exposure mode is off, then it's just a matter of plumbing.
If you did ever get around to doing this I know I for one would be extremely grateful - perhaps others would find it useful too. I have next to no knowledge of how these things work, so I'm assuming this isn't a trivial task - but i always wondered how we can access the gain with camera.analog_gain but not edit it - I'm sure it's very complicated though!

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5536
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Controlling analog gain on either v1.3 or v2 cameras

Thu Aug 11, 2016 3:37 pm

twVolc wrote:When you say a small amount of wiggle room, do you know how significant this is? I'm using the cameras for remote sensing applications, and essentially any adjustment is not good, as we need truly consistent images, or as close as possible.
Meh, I'd fixed up OV5647 to only have gains of
- x1.0-1.2 for ISO100
- x1.9-2.1 for ISO200
- x3.9-4.2 for ISO400
but that hasn't been done for IMX219. There the ranges are
- x1.0-2.6 for ISO100
- x2.6-5.3 for ISO200
- x5.2-7.6 for ISO400
- x10 for ISO800 (I wasn't expecting it to go up that high. Checking the driver, there is an option for analogue gain of x16 but with caveats on other settings. It looks like it is currently limited to x12).
twVolc wrote:
I have had thoughts on occasion of trying to add explicit analogue and digital gain parameters to the camera component, but have never got there due to other projects/issues. I'll see if I can build some motivation to do some hacking to the camera component again. If exposure mode is off, then it's just a matter of plumbing.
If you did ever get around to doing this I know I for one would be extremely grateful - perhaps others would find it useful too. I have next to no knowledge of how these things work, so I'm assuming this isn't a trivial task - but i always wondered how we can access the gain with camera.analog_gain but not edit it - I'm sure it's very complicated though!
A large bunch of stats get attached to each image and are easy to extract and publish as one stats structure.
Setting the values requires adding a suitable parameter, and then passing it into the right bit of code with magic incantations to get it to actually apply those settings without further modification. Generally a plumbing job, but easy to get caught out on. I'll see what I can do...
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

twVolc
Posts: 21
Joined: Wed Mar 16, 2016 11:41 am

Re: Controlling analog gain on either v1.3 or v2 cameras

Thu Aug 11, 2016 4:11 pm

6by9 wrote: Meh, I'd fixed up OV5647 to only have gains of
- x1.0-1.2 for ISO100
- x1.9-2.1 for ISO200
- x3.9-4.2 for ISO400
but that hasn't been done for IMX219. There the ranges are
- x1.0-2.6 for ISO100
- x2.6-5.3 for ISO200
- x5.2-7.6 for ISO400
- x10 for ISO800 (I wasn't expecting it to go up that high. Checking the driver, there is an option for analogue gain of x16 but with caveats on other settings. It looks like it is currently limited to x12).
Just to confirm, when exposure mode is set to "off", there's no wiggle at all right?
6by9 wrote:A large bunch of stats get attached to each image and are easy to extract and publish as one stats structure.
Setting the values requires adding a suitable parameter, and then passing it into the right bit of code with magic incantations to get it to actually apply those settings without further modification. Generally a plumbing job, but easy to get caught out on. I'll see what I can do...
Definitely sounds complicated then! To be honest, for my application, if there were just a setting to fix everything at x1.0 (no wiggle, and not just at any number depending on where the camera settles - as I think is the case with exposure mode "off"?) that might be quite useful, and maybe easier to do, but that's from a selfish point of view. Overall control of the gains I'm sure would help a bunch of people.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5536
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Controlling analog gain on either v1.3 or v2 cameras

Thu Aug 11, 2016 4:21 pm

twVolc wrote:Just to confirm, when exposure mode is set to "off", there's no wiggle at all right?
When "off" the value will not change further.
twVolc wrote:Definitely sounds complicated then! To be honest, for my application, if there were just a setting to fix everything at x1.0 (no wiggle, and not just at any number depending on where the camera settles - as I think is the case with exposure mode "off"?) that might be quite useful, and maybe easier to do, but that's from a selfish point of view. Overall control of the gains I'm sure would help a bunch of people.
Give me a day or two. Boredom can sometimes be a motivator, and it may fall into place easily enough to be done in an evening.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

Rhemyd
Posts: 10
Joined: Tue Oct 11, 2016 2:10 am

Re: Controlling analog gain on either v1.3 or v2 cameras

Fri Oct 21, 2016 7:47 am

+1 for the ability to control the Analog and digital gains.

I'm using the v2 camera in a PhD project where I need to control as many variables as possible. Currently I am able to set the digital gain by setting ev -24 (or similar) however I am unable to set a definitive value for the analog gain. Have the camera wait for a set period of time (sleep(2)) does not provided enough control over the automatic tint control within the camera and as such I am seeing some variations within my results that I am attributing to the analog gain changing.

Two questions:
- Can this be done manually? If so, where is the code/git repository for me to assist in the project of added the control.
- Is there currently a way to document what the exact values of the gain are as by inspection it is not apparent within the EXIF data

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5536
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Controlling analog gain on either v1.3 or v2 cameras

Fri Oct 21, 2016 8:59 am

Rhemyd wrote:+1 for the ability to control the Analog and digital gains.

I'm using the v2 camera in a PhD project where I need to control as many variables as possible. Currently I am able to set the digital gain by setting ev -24 (or similar) however I am unable to set a definitive value for the analog gain. Have the camera wait for a set period of time (sleep(2)) does not provided enough control over the automatic tint control within the camera and as such I am seeing some variations within my results that I am attributing to the analog gain changing.
Automatic tint control? Do you mean auto white balance? AWB can be fixed using -awb=off -awbg=<red_gain>,<blue_gain> in raspistill/vid.
Rhemyd wrote:Two questions:
- Can this be done manually? If so, where is the code/git repository for me to assist in the project of added the control.
- Is there currently a way to document what the exact values of the gain are as by inspection it is not apparent within the EXIF data
I did look at it and it wasn't trivial to override the AGC algorithm. It's still on the list of things to investigate, but not a high priority. It's part of the GPU firmware, so sadly closed source.

You can read back the current gains using MMAL_PARAMETER_CAMERA_SETTINGS (see -set option in raspistill), or the analog_gain and digital_gain properties in PiCamera if using Python.

Analogue gain is stored in the EXIF, but it seems not digital - the Makernote tag is a text string along the lines of

Code: Select all

ev=-1 mlux=-1 exp=29235 ag=256 focus=255 gain_r=1.027 gain_b=2.753 greenness=9 ccm=6022,-2314,394,-936,4728,310,300,-4324,8126,0,0,0 md=0 tg=264 264 oth=0 0 b=0 f=264 264 fi=0 ISP Build Date: Apr 27 2016, 11:09:25 VC_BUILD_ID_VERSION: 9b52ab7b475f4a056658fd2d95d2440b32167390 (clean) VC_BUILD_ID_USER:  VC_BUILD_ID_BRANCH: master 
ag is analogue gain.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

Rhemyd
Posts: 10
Joined: Tue Oct 11, 2016 2:10 am

Re: Controlling analog gain on either v1.3 or v2 cameras

Fri Oct 21, 2016 10:42 am

Nice!

I didn't realise it was in the makernote.

I said tint as I saw it in an additional forum post but it may have been refering to awb; nothing else really makes sense.

Refering to and following on viewtopic.php?p=1056365#p1056365.

If the pipe converts the data to 13-16bit precision and returns, what data are we playing with under the bayer=True flag? Shouldn't the gains have nothing to do with this raw bayer data as it is before the execution of this data?

From what I'm seeing the digital gain is affecting the raw bayer data as taken from your dcraw.c file. I could be mistaken.

Ultimately, I'm trying to obtain raw bayer data as it eliminates almost all variables in the equation at the linked post.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5536
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Controlling analog gain on either v1.3 or v2 cameras

Fri Oct 21, 2016 1:20 pm

Rhemyd wrote:Nice!

I didn't realise it was in the makernote.

I said tint as I saw it in an additional forum post but it may have been refering to awb; nothing else really makes sense.

Refering to and following on viewtopic.php?p=1056365#p1056365.

If the pipe converts the data to 13-16bit precision and returns, what data are we playing with under the bayer=True flag? Shouldn't the gains have nothing to do with this raw bayer data as it is before the execution of this data?

From what I'm seeing the digital gain is affecting the raw bayer data as taken from your dcraw.c file. I could be mistaken.

Ultimately, I'm trying to obtain raw bayer data as it eliminates almost all variables in the equation at the linked post.
Adding the "bayer=True" (I'm deducing you're using PiCamera from that) saves the raw 10bpp data as it comes off the sensor with no processing on the SoC at all.

AFAIK You hadn't said you wanted Bayer until now, although I did wonder from the publication you linked to (denoise would have made that irrelevant on the YUV data). You should probably be looking at viewtopic.php?f=43&t=109137 instead of using the full camera stack. You can then have the raw Bayer data streamed to you to process as you fancy - no lens shading, AE, AWB, denoise, or anything.
There are a couple of pull requests I need to resolve and merge also expose manually setting shutter speed, analogue gain, flips, and add all the other sensor readout modes, but they need some rework for me to be happy with them.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

Rhemyd
Posts: 10
Joined: Tue Oct 11, 2016 2:10 am

Re: Controlling analog gain on either v1.3 or v2 cameras

Fri Oct 21, 2016 1:52 pm

Awesome thanks, I'll have a look at that. Are there methods to save the bayer to TIFF directly?

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5536
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Controlling analog gain on either v1.3 or v2 cameras

Fri Oct 21, 2016 2:04 pm

Rhemyd wrote:Awesome thanks, I'll have a look at that. Are there methods to save the bayer to TIFF directly?
Please read through the other threads on the Bayer data off the Pi camera as there is a huge amount of information already written.
viewtopic.php?f=43&t=44918&start=25#p361647 includes an old app that took the JPEG+RAW files and wrote an appropriate DNG header around the Bayer data. I suspect it hasn't been updated for the V2 camera though.
dcraw is only set up for taking the JPEG+RAW files that come out of raspistill - it won't work with just the raw buffers without some modifications.
Between them you should be able to get something useful.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

orydberg
Posts: 5
Joined: Tue Oct 04, 2016 6:48 am

Re: Controlling analog gain on either v1.3 or v2 cameras

Fri Oct 21, 2016 9:20 pm

6by9

Would this analog/digital gain setting mean we can also get a "first available exposed image" out?

Famous example of getting a manually setup 6s exposure image out in approx 6s, rather than having to wait 19-24s for what I understand is GPU taking up to 4 exposures to settle levels.
As mentioned in other threads I've experimented with V2 camera and raspiraw code, and 1st image back after enabling camera always seem fine with no garbage.

naroin
Posts: 1
Joined: Thu Jan 05, 2017 12:53 pm

Re: Controlling analog gain on either v1.3 or v2 cameras

Thu Jan 05, 2017 12:57 pm

6by9 wrote: I did look at it and it wasn't trivial to override the AGC algorithm. It's still on the list of things to investigate, but not a high priority. It's part of the GPU firmware, so sadly closed source.
Hi,

did you make some modifications on this point?
Is possible to change manually the gains, and if yes, what code needs to be changed (in the driver I suppose)?

thanks

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5536
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Controlling analog gain on either v1.3 or v2 cameras

Thu Jan 05, 2017 9:01 pm

naroin wrote:
6by9 wrote: I did look at it and it wasn't trivial to override the AGC algorithm. It's still on the list of things to investigate, but not a high priority. It's part of the GPU firmware, so sadly closed source.
Hi,

did you make some modifications on this point?
Is possible to change manually the gains, and if yes, what code needs to be changed (in the driver I suppose)?
Sorry, still not had a chance to get anything that actually works.
Setting ISO will set the combined analogue and digital gains, and analogue is always used in preference to digital.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

fooforever
Posts: 23
Joined: Wed Aug 15, 2012 11:21 pm

Re: Controlling analog gain on either v1.3 or v2 cameras

Thu Apr 13, 2017 8:38 am

So just to confirm 6by9, there's still currently no way to fix analog gains on the sensor?

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5536
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Controlling analog gain on either v1.3 or v2 cameras

Thu Apr 13, 2017 9:18 am

fooforever wrote:So just to confirm 6by9, there's still currently no way to fix analog gains on the sensor?
Correct.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

fooforever
Posts: 23
Joined: Wed Aug 15, 2012 11:21 pm

Re: Controlling analog gain on either v1.3 or v2 cameras

Wed Apr 19, 2017 8:44 am

Is there any 'easy'(ish) way to get there?

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5536
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Controlling analog gain on either v1.3 or v2 cameras

Fri Apr 21, 2017 10:25 am

fooforever wrote:Is there any 'easy'(ish) way to get there?
What specifically is your use case?

If you really want to get your hands dirty then there is raspiraw where everything is up to you but you do have direct access to exposure time and gain (and all the other I2C registers in the sensor).

I had looked at plumbing in direct analogue and digital gain control to the main camera stack and it wasn't trivial, which is why it hasn't been done.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

charles_ch
Posts: 9
Joined: Wed Jan 18, 2017 8:41 pm

Re: Controlling analog gain on either v1.3 or v2 cameras

Fri Aug 11, 2017 4:51 pm

6by9 wrote:
Fri Oct 21, 2016 8:59 am
Rhemyd wrote:+1 for the ability to control the Analog and digital gains.

I'm using the v2 camera in a PhD project where I need to control as many variables as possible. Currently I am able to set the digital gain by setting ev -24 (or similar) however I am unable to set a definitive value for the analog gain. Have the camera wait for a set period of time (sleep(2)) does not provided enough control over the automatic tint control within the camera and as such I am seeing some variations within my results that I am attributing to the analog gain changing.
Automatic tint control? Do you mean auto white balance? AWB can be fixed using -awb=off -awbg=<red_gain>,<blue_gain> in raspistill/vid.
Rhemyd wrote:Two questions:
- Can this be done manually? If so, where is the code/git repository for me to assist in the project of added the control.
- Is there currently a way to document what the exact values of the gain are as by inspection it is not apparent within the EXIF data
I did look at it and it wasn't trivial to override the AGC algorithm. It's still on the list of things to investigate, but not a high priority. It's part of the GPU firmware, so sadly closed source.

You can read back the current gains using MMAL_PARAMETER_CAMERA_SETTINGS (see -set option in raspistill), or the analog_gain and digital_gain properties in PiCamera if using Python.

Analogue gain is stored in the EXIF, but it seems not digital - the Makernote tag is a text string along the lines of

Code: Select all

ev=-1 mlux=-1 exp=29235 ag=256 focus=255 gain_r=1.027 gain_b=2.753 greenness=9 ccm=6022,-2314,394,-936,4728,310,300,-4324,8126,0,0,0 md=0 tg=264 264 oth=0 0 b=0 f=264 264 fi=0 ISP Build Date: Apr 27 2016, 11:09:25 VC_BUILD_ID_VERSION: 9b52ab7b475f4a056658fd2d95d2440b32167390 (clean) VC_BUILD_ID_USER:  VC_BUILD_ID_BRANCH: master 
ag is analogue gain.

Hi,

Do someone know the meaning of all the other data saved in the Makernote tag, and how to interpret them? (ccm, tg, greenness, ..)

Thanks!

Return to “Camera board”

Who is online

Users browsing this forum: No registered users and 6 guests