Cameras for Raspberry Pi

  1. Home
  2. Docs
  3. Cameras for Raspberry Pi
  4. Native Raspberry Pi Cameras
  5. Lens Shading Calibration (Fix the Red Tint After Swapping Lens)

Lens Shading Calibration (Fix the Red Tint After Swapping Lens)

Before and after the calibration

What is the lens shading effect we are talking about?

Sometimes when you replace the stock lens of the Raspberry Pi V1 or V2 camera with an Arducam Lens, you will encounter a color issue where the outer side of the picture looks pink or magenta. That is a lens shading artifact, which is expected but can be solved.

It is expected because the default compensation is only valid for the stock lens of the Pi cameras, while different lens should have been compensated differently for their optical results. It can be solved because the file to alter the compensation – the lens shading table – can be overridden to match your own lens.

How should I tune the configuration file to match my lens?

Well, you don’t have to tune the compensation yourself because Arducam has already done the work for you. You just need to double check which Arducam lens you have on hand and know what the corresponding files you will be using.

Here is the matching relationship between the FoV, SKU, and Part number of the most used Arducam M12 lenses, and you will be referring to the FoV degree numbers in the first column for the target file. As for the CS lens, there is a single general config file.

HFoV on 1/4″Part NumberSKU
120M25170H12 LN007
Arducam lens matching relationship

How to use the Arducam presets for lens shading calibration?

Arducam has provided two methods to compensate the lens shading effect. The first is precompiled raspistill tools, and the second is pre-configured lens shading tables to be recompiled in your raspistill.c code. If you want to apply the compensation with raspivid, refer to the second method.

1. Use the precompiled raspistill tool

1.1 Download the customized Raspistill Tool

Arducam has provided tools for M12 lenses and CS-Mount lenses, and you can get the files here, or you can clone the repository.

git clone

1.2 Copy the file to the Pi Directory

raspistill precompiled

Check the table of matching relationships above to find the target file in NativePiCamera/bin, then copy it to the Pi directory.

For example

If you are using Arducam M40210M09S (LN014), you can know from the table that its FoV on V1&V2 Pi cameras is 90 degrees. Therefore, you need to copy the raspistill_M12_90_lens to /home/pi path.

1.3 Enter the Pi Directory

cd /home/pi

1.4 Run the tool

Let’s assume that we are still using the last example with the M40210M09S (LN014) lens, you can run the following command for a preview.

./ raspistill_M12_90_lens –t 0

2. Override the lens shading table and recompile

2.1 Download the repository

The lens shading table is in the same repository as the above. You can skip it if you’ve already done this from the first method. The lens shading table is in the NativePiCamera/ls_table folder.

git clone

Now recompile the raspistill source code to correct the lens vignetting using the generated lens gain table data (ubuntu16 cross compilation)

2.2 Download userland source code

 git clone

2.3 Install cmake

 sudo apt-get install cmake

2.4 Rename and copy the lens table

lens table calibration native

​​Rename the ls_table_M12_xx.h to ls_table.h and copy the file to the /userland/host_applications/linux/apps/raspicam path


2.5 Change the Raspistill.c file

There are two ways to change the file. You can use our file or alter yours.

  • copy the RaspiStill.c file to your default folder
sudo cp RaspiStill.c ./userland/host_applications/linux/apps/raspicam/RaspiStill.c
2.5.2 Alter your RaspiStill.c file

Open the raspistill.c file add some changes to the code.

  • Before #include "interface/vcos/vcos.h", add
#include "user-vcsm.h"
  • Before } RASPISTILL_STATE; add:
int lens_shading;
  • After mmal_port_parameter_set(camera->control, &cam_config.hdr); }, add:
      void *grid;

      #include "ls_table.h"

      ls.enabled = MMAL_TRUE;
      ls.grid_cell_size = 64;
      ls.grid_width = ls.grid_stride = grid_width;
      ls.grid_height = grid_height;
      ls.ref_transform = ref_transform;

      state->lens_shading = vcsm_malloc(ls.grid_stride*ls.grid_height*4, "ls_grid");
      ls.mem_handle_table = vcsm_vc_hdl_from_hdl(state->lens_shading);

      grid = vcsm_lock(state->lens_shading);

      memcpy(grid, ls_grid, vcos_min(sizeof(ls_grid), ls.grid_stride*ls.grid_height*4));


      status = mmal_port_parameter_set(camera->control, &ls.hdr);
      if (status != MMAL_SUCCESS)
         vcos_log_error("Failed to set lens shading parameters - %d", status);

2.6 Change the RaspiVid.c file

There are two ways to change the file. You can use our file or alter yours.

  • copy the RaspiVid.c file to your default folder
sudo cp RaspiVid.c ./userland/host_applications/linux/apps/raspicam/RaspiVid.c
2.6.2 Alter your RaspiVid.c file

Refer to 2.5.2 for the same steps.

2.7 Recompile

In your terminal, type the following command:

cd userland
~/userland/build/arm-linux/release ~/userland

Then you will get the new raspistill tool which is in the build/bin/ path


The expected results of the Calibration

  • M12 FOV:10°
  • M12 FOV:20°
  • M12 FOV:40°
  • M12 FOV:45°
  • M12 FOV:55°
  • M12 FOV:60°
  • M12 FOV:65°
  • M12 FOV:70°
  • M12 FOV:80°
  • M12 FOV:100°
  • M12 FOV:120°
  • M12 FOV:140°
  • M12 FOV:160°
  • M12 FOV:200°
Was this article helpful to you? Yes 5 No