clib
Posts: 5
Joined: Tue Dec 20, 2016 2:56 pm

Lens color correction

Tue Dec 20, 2016 3:20 pm

Hello!

While taking a picture of overcast sky with NoIR Camera v2 I noticed that there was very slight fall-off in terms of color tint and brightness going from the center of the frame to the edges. The non-uniformity is especially apparent when comparing it to an image made by cell phone captured at the same time.

Please, does anyone know why there is that purple tint near the edges? I think I read somewhere that the camera module compensates in software for the optical "characteristics" of the built-in lens. If so, is it possible to disable the automatic color correction, preferably in Picamera?

I'm asking because I removed the supplied built-in lens so I could use the camera for prime focus photography with some other lenses or telescope.

Thank you!
Attachments
blank-noir.jpg
blank-noir.jpg (48.42 KiB) Viewed 6811 times
blank-phone.jpg
blank-phone.jpg (37.48 KiB) Viewed 6813 times

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 30151
Joined: Sat Jul 30, 2011 7:41 pm

Re: Lens color correction

Wed Dec 21, 2016 10:50 am

Yes, that is a lens correction issue. If you have removed the lens then you will see this as it is still applying the correction when not required. I'm not sure you can turn it off. What is really required is a algorithmic correction (it's currently a predefined matrix I believe but 6x9 can confirm) which would automatically compensate for any lens (I think...).
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Working in the Applications Team.

gordon77
Posts: 6120
Joined: Sun Aug 05, 2012 3:12 pm

Re: Lens color correction

Wed Dec 21, 2016 10:56 am

try raspistill with the -st option

or

viewtopic.php?t=105376&p=727703
Last edited by gordon77 on Wed Dec 21, 2016 11:23 am, edited 1 time in total.

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

Re: Lens color correction

Wed Dec 21, 2016 11:13 am

jamesh wrote:I'm not sure you can turn it off. What is really required is a algorithmic correction (it's currently a predefined matrix I believe but 6x9 can confirm) which would automatically compensate for any lens (I think...).
OV5647 (V1, 5MPix) it is a fixed matrix unless you request a stats pass (-st on the raspistill command line, or still_stats in PiCamera), or have set "ov5647_acls=1" in /boot/config.txt (I haven't convinced myself that that is a full solution, hence why it isn't the default).
IMX219 (V2, 8MPix) should be running the auto algorithm at all times.

The auto algorithm is still working within a configured range, based on the lens shading of a reference "golden" sensor. I don't know whether the range is sufficient to be "off" when you have no lens at all.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

clib
Posts: 5
Joined: Tue Dec 20, 2016 2:56 pm

Re: Lens color correction

Thu Dec 29, 2016 11:18 am

Thank you very much for all the answers!

Please, what can one do about the correction? For prime focus photography it is needed to remove the lens...

I thought it would be possible to somehow/somewhere disable the correction but if I understood correctly that is not possible (from user perspective).

Another option would be to "multiply-back" the correction matrix to neutralise its effect. Does anyone know how and what matrix gets multiplied, i.e. how precisely does the correction work? Is the code and the correction parameters publicly available somewhere? Lastly, to avoid clipping in highlights and shadows, one would need to apply the reversed matrix as soon as possible down in the image capture process. I guess "back-correcting" the "auto-correction" should be done in RAW?

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 30151
Joined: Sat Jul 30, 2011 7:41 pm

Re: Lens color correction

Thu Dec 29, 2016 10:32 pm

clib wrote:Thank you very much for all the answers!

Please, what can one do about the correction? For prime focus photography it is needed to remove the lens...

I thought it would be possible to somehow/somewhere disable the correction but if I understood correctly that is not possible (from user perspective).

Another option would be to "multiply-back" the correction matrix to neutralise its effect. Does anyone know how and what matrix gets multiplied, i.e. how precisely does the correction work? Is the code and the correction parameters publicly available somewhere? Lastly, to avoid clipping in highlights and shadows, one would need to apply the reversed matrix as soon as possible down in the image capture process. I guess "back-correcting" the "auto-correction" should be done in RAW?
Right now, I don't think it can be turned off, and the matrix for the OV5647 is not available. Not sure if the algorithmically generated one on the IMX219 can be retrieved at all.

Might have a chance to look at this in the New Year..
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Working in the Applications Team.

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

Re: Lens color correction

Thu Dec 29, 2016 11:05 pm

The lens shading tables are broadly unity in the middle, and then the gain in the corners is approx
- x4.2 for red
- x3.26 for green
- x3.12 for blue
It varies based on exposure time which is why absolute values can't be given.

If you're prepared to process raw images, then those are always stored with no image processing applied. Add the -raw option to raspistill and it'll concatenate the Bayer raw 10 to the JPEG. There are various tools out there to process those images (dcraw has some support and I updated for the V2 sensor a while back, but I've had reports that it isn't working - not had time to check it out yet).
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

clib
Posts: 5
Joined: Tue Dec 20, 2016 2:56 pm

Re: Lens color correction

Fri Dec 30, 2016 10:28 am

I apologise but I'm a bit confused now. Could you please clarify the following:
6by9 wrote:the gain in the corners
Does it mean the gain is zero in the middle and what at what rate/function does the gain grow from the middle to the corners?
6by9 wrote:raw images ... are always stored with no image processing applied
I thought this colour correction happens even before the RAW image, which the user is able to retrieve, is created. So just to check I understood: the colour corrections is part of the pipeline which develops JPEGs, and if I capture RAW images as described in https://picamera.readthedocs.io/en/rele ... a-captures the correction is not going to be present?

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

Re: Lens color correction

Fri Dec 30, 2016 10:43 am

clib wrote:I apologise but I'm a bit confused now. Could you please clarify the following:
6by9 wrote:the gain in the corners
Does it mean the gain is zero in the middle and what at what rate/function does the gain grow from the middle to the corners?
Unity = x1.0, not x0 (which would always give 0).
I'm afraid I'm not able to give you the complete curve, but it looks roughly exponential.
clib wrote:
6by9 wrote:raw images ... are always stored with no image processing applied
I thought this colour correction happens even before the RAW image, which the user is able to retrieve, is created. So just to check I understood: the colour corrections is part of the pipeline which develops JPEGs, and if I capture RAW images as described in https://picamera.readthedocs.io/en/rele ... a-captures the correction is not going to be present?
Raw images are direct off the sensor, so exposure setting and analogue gain are the only two settings that affect them. All other processing (black level, lens shading, white balance, denoise, etc) is done on the SoC.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

clib
Posts: 5
Joined: Tue Dec 20, 2016 2:56 pm

Re: Lens color correction

Fri Dec 30, 2016 11:08 am

6by9 wrote:Unity = x1.0, not x0
Of course, stupid me!

Thank you!

User avatar
Burngate
Posts: 6483
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Lens color correction

Fri Dec 30, 2016 11:40 am

6by9 wrote:... It varies based on exposure time ...
That seems strange, to me.
... unless the pixels are grossly non-linear, I can't see why it would need to change.
Is there a (short) explanation?

Is gamma correction applied before the raw data is produced, so before lens correction?
I would have thought lens correction would be applied before gamma correction, but I'm often wrong.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 30151
Joined: Sat Jul 30, 2011 7:41 pm

Re: Lens color correction

Fri Dec 30, 2016 6:38 pm

Burngate wrote:
6by9 wrote:... It varies based on exposure time ...
That seems strange, to me.
... unless the pixels are grossly non-linear, I can't see why it would need to change.
Is there a (short) explanation?

Is gamma correction applied before the raw data is produced, so before lens correction?
I would have thought lens correction would be applied before gamma correction, but I'm often wrong.
Because exposure changes the gain, and the gain is produced by both analog, on the sensor, and digital gain, in the ISP. It's the analog gain on the sensor that will change the raw. Everything elses is done in the ISP. IIRC, analog gain is used in preference to digital, as it doesn't reduce the range of the data.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Working in the Applications Team.

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

Re: Lens color correction

Fri Dec 30, 2016 9:28 pm

Burngate wrote:
6by9 wrote:... It varies based on exposure time ...
That seems strange, to me.
... unless the pixels are grossly non-linear, I can't see why it would need to change.
Is there a (short) explanation?
I'm afraid I can't give the full explanation - I'm generally the plumber who gets API #1 to talk to API #2. Broadcom used to have a team of very clever people who were always tinkering with these settings to produce the best results possible and this is part of their work.
All I can say is that in the sensor tuning there are two LSC grids and the tuner interpolates between them based on a normalisation of exposure time, aperture (fixed on the Pi cameras), analogue gain, and digital gain.
Burngate wrote:Is gamma correction applied before the raw data is produced, so before lens correction?
I would have thought lens correction would be applied before gamma correction, but I'm often wrong.
EVERYTHING except exposure time and analogue gain are after the raw image data is produced.
The sensor is used in as dumb a mode as possible, hence only exposure time and analogue gain. That data is sent over the CSI2 bus and written to RAM. That is the image that is written out as the raw, and is also the same data fed into the ISP to be processed into a more useful form.
Within the ISP, lens shading correction is before gamma (one's in the Bayer domain, the other is after demosaicing).
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

User avatar
Burngate
Posts: 6483
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Lens color correction

Sun Jan 01, 2017 11:05 am

Thank you, both of you.

So as I hoped, gamma is applied after raw is produced, so it hasn't got anything to do with that - though, thinking more about it, that was a red herring anyway, as it's the wrong sort of nonlinearity.
... the tuner interpolates between them based on a normalisation of exposure time, aperture (fixed on the Pi cameras), analogue gain, and digital gain
Lens aperture will certainly alter the shading, but I just don't see how, eg. doubling the scene illumination then halving the exposure time can affect the ratio of centre photons per pixel to corner photons per pixel, hence electron numbers or charge, hence ...
And gain - either analogue or digital - is I assume the same at the centre and the corners, so can't affect shading.

I need to do some more research, googling, wiki-ing, what-ever. And the result will take me ever further from anything Pi-related.
But thank you, anyway

User avatar
Gavinmc42
Posts: 6304
Joined: Wed Aug 28, 2013 3:31 am

Re: Lens color correction

Sun Jan 01, 2017 12:33 pm

It also depends on the lens too.
Use a M12/M8 lens that is for a 6mm+ image sensor on the 3.6mm Pi sensor and you are using the center of the field of view and there will be less distortion and colour effects than the M6 standard lens.

You can see this as purple or green rings around the outside.
Higher quality narrower angle lens will have less or nearly no colour effects.
You could also use first surface mirror lens.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

Return to “Camera board”