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

Re: Custom lens shading tables

Sun Jun 10, 2018 7:25 pm

drich wrote:
Sun Jun 10, 2018 5:51 pm
@6by9, currently my tool can import the ch1-4.bin files, are you OK by any chance to let me integrate your code (and credits you of course), allowing me to simply open JPEG-RAW images ?
Feel free. I'm all for people improving tools.
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.

zbarna
Posts: 15
Joined: Mon Apr 23, 2018 10:16 am

Re: Custom lens shading tables

Sun Aug 19, 2018 4:17 pm

Hi!

I also would like to override the lens shading table, because we have to use a custom lens with the picamera in our project.

I've read that I have to use a special version of the Raspicam.c.

My program has written in Python, and I'm using the Picamera class to show the preview of the camera, take pictures, record video, change the settings of the camera.
Is it possible to overwrite the lens shading table if I'm using the picamera this way?
Could you give me some hint how should I start?

Thanks in advance!

Best Regards,
Zoltán

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

Re: Custom lens shading tables

Mon Aug 20, 2018 8:47 am

zbarna wrote:
Sun Aug 19, 2018 4:17 pm
My program has written in Python, and I'm using the Picamera class to show the preview of the camera, take pictures, record video, change the settings of the camera.
Is it possible to overwrite the lens shading table if I'm using the picamera this way?
Could you give me some hint how should I start?
You'll need to add support to the PiCamera library.
...
Actually it looks like someone has done that work already - https://github.com/waveform80/picamera/pull/470. I'm guessing waveform80 is busy on other things at the moment so it hasn't been merged. You'll have to build the updated library for yourself (instructions at https://picamera.readthedocs.io/en/late ... pment.html).
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.

zbarna
Posts: 15
Joined: Mon Apr 23, 2018 10:16 am

Re: Custom lens shading tables

Mon Aug 20, 2018 10:01 am

Ah okay it's clear, thanks!
I'm going to try it! :)

zbarna
Posts: 15
Joined: Mon Apr 23, 2018 10:16 am

Re: Custom lens shading tables

Mon Sep 03, 2018 7:56 am

Hi!

I successfully changed the lens shading tables in the picamera python module with the help of the link you posted above.

I tried a lot of different calibrator, and I reached the best result with this one:
https://github.com/rwb27/openflexure_mi ... penloop.py
It works similar way like 6by9's solution.

With this calibration, the color shading issue went away if I put white or black thing in front of the camera, but with other colored things it comes back :( .

Here you can see sample pictures (name of the picture tells the real color of the item):
https://drive.google.com/drive/folders/ ... kGH-ooF0WG

Could you give me some hint? Why does it work with black and white things, but if I put a red or yellow item it can't handle it.

Thanks in advance!
Best regards,
Zoltán

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

Re: Custom lens shading tables

Mon Sep 03, 2018 10:01 am

zbarna wrote:
Mon Sep 03, 2018 7:56 am
Could you give me some hint? Why does it work with black and white things, but if I put a red or yellow item it can't handle it.
Not off the top of my head.
There is an algorithm normally running to try and automatically adjust the lens shading tables as the modules aren't calibrated on the production line and there is always a small amount of variation, but that should be disabled with a custom table.
The fact that black and white comes through normally would imply that your tables are correct - there is no monochrome element in the sensor, only red green, and blue pixels arranged in the Bayer pattern. White balance would have the potential to interfere slightly as it alters the red and blue pipeline gains, but that is after lens shading correction and relies on lens shading to have been dealt with already before the statistics that it works from.

I'll try to find a few minutes to give it a whirl myself again.
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.

zbarna
Posts: 15
Joined: Mon Apr 23, 2018 10:16 am

Re: Custom lens shading tables

Tue Sep 04, 2018 6:39 am

Thanks for your time!

I hope that we are very close to the solution :) .

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

Re: Custom lens shading tables

Tue Sep 04, 2018 1:00 pm

Running it myself with a debugger, the lens shading table appears to be correctly set based on the table passed in. I'm therefore unsure as to how come you're seeing such variation.
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.

zbarna
Posts: 15
Joined: Mon Apr 23, 2018 10:16 am

Re: Custom lens shading tables

Tue Sep 04, 2018 3:40 pm

I don't understand what's happening with my camera.
This is my code with the modified python picamera module which can set the lens shading table:

Code: Select all

from picamera import PiCamera
import numpy as np
import time

with PiCamera() as cam:
    lst_shape = cam._lens_shading_table_shape()

npz = np.load('/home/pi/LensShadingTest/lens_shading_table.npy')

with PiCamera(resolution=cam.MAX_RESOLUTION, lens_shading_table=npz) as cam:
    cam.start_preview()
    time.sleep(600)
I create the lens_shading_table.npy with the calibrator.

With this code the lens shading table changed, because if I put a white paper in front of the sensor the lens shading issue went away.
I also tried to set the awb_mode->'off', but it didn't help me :S .

I will make a try with your raspistill.c, maybe the problem is in the modified picamera python module :S .

zbarna
Posts: 15
Joined: Mon Apr 23, 2018 10:16 am

Re: Custom lens shading tables

Wed Sep 05, 2018 7:22 am

Hi!

I converted my lens shading table from the npz to the ls_table.h, built a new instance from the raspistill.c, and I got the same result. With white and black colored items the vignetting issue went away, with yellow, red or other colored items it came back.

I think this picture is very interesting. Here in the background it's a black item, and front of it a yellow colored item:
Image
On the right side it's correct where the color is black, but on the left side it should be yellow, but it isn't.

Here you can see the colors in the real life:
Image

I talked to the rwb27, who implemented the lens_shading change for the picamera module and the lensshading recalibration I used. You can read his answer here:
https://github.com/waveform80/picamera/ ... -418532710.
His answer sounds reasonable, but in this case we cannot solve this problem with just calibrate the lens shading this way.

Could you suggest something, or what should I do now? :S

Thanks in advance!
Best Regards,
Zolán

drich
Posts: 23
Joined: Tue Jul 28, 2015 7:36 pm

Re: Custom lens shading tables

Wed Sep 05, 2018 8:28 pm

Looks like the problem I'm facing when white balance changes.
Can you try with fixed AWB gains ?

zbarna
Posts: 15
Joined: Mon Apr 23, 2018 10:16 am

Re: Custom lens shading tables

Thu Sep 06, 2018 6:57 am

Yes, I tried to set the awb_mode->'off', but it didn't help me. :(

Naro
Posts: 3
Joined: Thu Mar 21, 2019 5:50 pm

Re: Custom lens shading tables

Mon May 20, 2019 3:26 pm

Its been some time since the last post. I just wanted to bump a comment to gauge if there's been any updates don't already know about. I've spent about a month or two trying to figure out a decent ls table for the V2 cameras.

I essentially ended up where cpixip ended up.

I managed to get a fairly flat image of the grey card I calibrated on:
post1.jpg
post1.jpg (113.48 KiB) Viewed 445 times
And after getting very excited I then realized that other colors were still falling off on the edges... This one is specifically a red card.
post2.jpg
post2.jpg (132.78 KiB) Viewed 445 times
Like cpixip I also made my own python script, and one of the thing I ended up doing was creating lsTables, applying them, taking regular jpg snap shots, then using those to make a new LsTable (Using the green value twice) and finally multiplying the two LsTables together.
This actually gave the best results when coupled with my attempt at creating an evenly lit grey card, but it is definitely hacky and in no way a sophisticated solution.

I would appreciate any updates, thank you. I understand the engineers are busy people and are handling higher priorities, again I just want an update on wether or the situation is still the same as it was in 2017.

cpixip
Posts: 28
Joined: Mon May 01, 2017 7:56 am

Re: Custom lens shading tables

Thu May 23, 2019 7:32 pm

Hi, let me just summarize what has been found out by various people with respect to lens shading and the Raspi cameras, for the benefit of all.

Lens shading as implemented in the Raspi camera pipeline can counteract vignetting and the like. This is what it is designed for.

However...

The color shifts people are experiencing when they change the standard lens of a v1- or v2-camera is something that can only partially be compensated by the lens shading algorithm.

The reason is a tiny microlens array in front of the sensors of of the Raspi cameras. These microlenses have a different pitch compared to the pixel array below.

This is done to enhance optical performance of the camera when the chip/microlens array is combined with a standard mobile phone lens - which feature focal lengths of 3-5 mm usually. The microlens array "guides" the incoming light perfectly onto the pixels of the sensor, even so the angle of the incoming light ray change dramatically from center to edge of the sensor.

Exchanging such a mobil lens with a substantially longer focal length (say: a standard photo lens of 50 mm) changes the optical pathway dramatically. The light falling through a single filter patch (say, a RED-filter) no longer gets focused on the red pixel alone, but also on neighbouring green and blue pixels. In technical terms, this is called crosstalk. The lens shading as implemented in the Raspi camera pipeline can not compensate this crosstalk. The crosstalk is spatially varying and the largest toward the image edges (compare the image of the red card in the post above).

In fact, the crosstalk is much more pronouned with the v2-camera, compared to the v1-camera. A number of people trying to correct the color shifts of Raspi cams fitted with other lenses found that out. Here's for example some information from a guy realizing a film scanner with Raspi hardware https://github.com/dgalland/yart/blob/master/yart.md ( - in French, use a translator). He found out that the results with the v1-sensor are better, colorwise, than with the v2-sensor.

In summary: if you need to change the optics of the Raspi cam for your project, you just will have to live with some color shifts across the image. You can not compensate this fully with the available lens shading. The remaining color shifts will be noticably smaller if you use a v1-sensor instead of a v2-sensor. If you can, redesign your optical system in such a way that you can leave the standard lens on the camera. For example, imaging a small Super-8 frame, you could use a 50 mm lens and place the camera about 200 mm away from the frame, with the lens 100 mm in front of the sensor chip - that will get you the approx. 1:1 scaling you need. Or: you could leave the standard lens on the camera and use in addition a good enlarger lens. By keeping the standard lens, the microlens array will work as designed, no color shifts will occur. Some examples of that approach can be found for example at the Kinograph forums (https://forums.kinograph.cc/).

- cpixip

Return to “Camera board”