[1/2] utils: raspberrypi: ctt: Fix NaNs in lens shading tables
diff mbox series

Message ID 20250428103604.151551-2-david.plowman@raspberrypi.com
State New
Headers show
Series
  • Raspberry Pi Camera Tuning Tool fixes
Related show

Commit Message

David Plowman April 28, 2025, 10:36 a.m. UTC
The problem occurs when the calculation could lead to a final row (or
column) of grid squares with no pixels in them (and hence, NaNs).

One specific case is a Pi 5 with an image width (or height) of 1364,
so that's 682 Bayer quads. To give 32 grid squares it was calculating
22 quads per cell. However, 31 * 22 = 682 leaving nothing in the final
column.

The fix is to do a rounding-down division by the number of cells minus
one, rather than a rounding-up division by the number of cells. This
turns the corner case from one where the final row/column has no
pixels to one where we don't quite cover the full image, which is how
we have to handle these cases.

Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
---
 utils/raspberrypi/ctt/ctt_alsc.py | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

Comments

Naushir Patuck April 28, 2025, 1:20 p.m. UTC | #1
Hi David,

On Mon, 28 Apr 2025 at 11:36, David Plowman
<david.plowman@raspberrypi.com> wrote:
>
> The problem occurs when the calculation could lead to a final row (or
> column) of grid squares with no pixels in them (and hence, NaNs).
>
> One specific case is a Pi 5 with an image width (or height) of 1364,
> so that's 682 Bayer quads. To give 32 grid squares it was calculating
> 22 quads per cell. However, 31 * 22 = 682 leaving nothing in the final
> column.
>
> The fix is to do a rounding-down division by the number of cells minus
> one, rather than a rounding-up division by the number of cells. This
> turns the corner case from one where the final row/column has no
> pixels to one where we don't quite cover the full image, which is how
> we have to handle these cases.
>
> Signed-off-by: David Plowman <david.plowman@raspberrypi.com>

Reviewed-by: Naushir Patuck <naush@raspberrypi.com>

> ---
>  utils/raspberrypi/ctt/ctt_alsc.py | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
>
> diff --git a/utils/raspberrypi/ctt/ctt_alsc.py b/utils/raspberrypi/ctt/ctt_alsc.py
> index 1d94dfa5..f4fd09e3 100644
> --- a/utils/raspberrypi/ctt/ctt_alsc.py
> +++ b/utils/raspberrypi/ctt/ctt_alsc.py
> @@ -127,11 +127,12 @@ def alsc(Cam, Img, do_alsc_colour, plot=False, grid_size=(16, 12), max_gain=8.0)
>      channels = [Img.channels[i] for i in Img.order]
>      """
>      calculate size of single rectangle.
> -    -(-(w-1)//32) is a ceiling division. w-1 is to deal robustly with the case
> -    where w is a multiple of 32.
> +    The divisions here must ensure the final row/column of cells has a non-zero number of
> +    pixels.
>      """
>      w, h = Img.w/2, Img.h/2
> -    dx, dy = int(-(-(w-1)//grid_w)), int(-(-(h-1)//grid_h))
> +    dx, dy = (w - 1) // (grid_w - 1), (h - 1) // (grid_h - 1)
> +
>      """
>      average the green channels into one
>      """
> --
> 2.34.1
>
Kieran Bingham April 28, 2025, 1:47 p.m. UTC | #2
Quoting Naushir Patuck (2025-04-28 14:20:26)
> Hi David,
> 
> On Mon, 28 Apr 2025 at 11:36, David Plowman
> <david.plowman@raspberrypi.com> wrote:
> >
> > The problem occurs when the calculation could lead to a final row (or
> > column) of grid squares with no pixels in them (and hence, NaNs).
> >
> > One specific case is a Pi 5 with an image width (or height) of 1364,
> > so that's 682 Bayer quads. To give 32 grid squares it was calculating
> > 22 quads per cell. However, 31 * 22 = 682 leaving nothing in the final
> > column.
> >
> > The fix is to do a rounding-down division by the number of cells minus
> > one, rather than a rounding-up division by the number of cells. This
> > turns the corner case from one where the final row/column has no
> > pixels to one where we don't quite cover the full image, which is how
> > we have to handle these cases.
> >
> > Signed-off-by: David Plowman <david.plowman@raspberrypi.com>
> 
> Reviewed-by: Naushir Patuck <naush@raspberrypi.com>


Acked-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

Will merge these two.

Would you like to add any Bug/Fixes reference so it appears in the
libcamera release notes ?

--
Kieran



> 
> > ---
> >  utils/raspberrypi/ctt/ctt_alsc.py | 7 ++++---
> >  1 file changed, 4 insertions(+), 3 deletions(-)
> >
> > diff --git a/utils/raspberrypi/ctt/ctt_alsc.py b/utils/raspberrypi/ctt/ctt_alsc.py
> > index 1d94dfa5..f4fd09e3 100644
> > --- a/utils/raspberrypi/ctt/ctt_alsc.py
> > +++ b/utils/raspberrypi/ctt/ctt_alsc.py
> > @@ -127,11 +127,12 @@ def alsc(Cam, Img, do_alsc_colour, plot=False, grid_size=(16, 12), max_gain=8.0)
> >      channels = [Img.channels[i] for i in Img.order]
> >      """
> >      calculate size of single rectangle.
> > -    -(-(w-1)//32) is a ceiling division. w-1 is to deal robustly with the case
> > -    where w is a multiple of 32.
> > +    The divisions here must ensure the final row/column of cells has a non-zero number of
> > +    pixels.
> >      """
> >      w, h = Img.w/2, Img.h/2
> > -    dx, dy = int(-(-(w-1)//grid_w)), int(-(-(h-1)//grid_h))
> > +    dx, dy = (w - 1) // (grid_w - 1), (h - 1) // (grid_h - 1)
> > +
> >      """
> >      average the green channels into one
> >      """
> > --
> > 2.34.1
> >

Patch
diff mbox series

diff --git a/utils/raspberrypi/ctt/ctt_alsc.py b/utils/raspberrypi/ctt/ctt_alsc.py
index 1d94dfa5..f4fd09e3 100644
--- a/utils/raspberrypi/ctt/ctt_alsc.py
+++ b/utils/raspberrypi/ctt/ctt_alsc.py
@@ -127,11 +127,12 @@  def alsc(Cam, Img, do_alsc_colour, plot=False, grid_size=(16, 12), max_gain=8.0)
     channels = [Img.channels[i] for i in Img.order]
     """
     calculate size of single rectangle.
-    -(-(w-1)//32) is a ceiling division. w-1 is to deal robustly with the case
-    where w is a multiple of 32.
+    The divisions here must ensure the final row/column of cells has a non-zero number of
+    pixels.
     """
     w, h = Img.w/2, Img.h/2
-    dx, dy = int(-(-(w-1)//grid_w)), int(-(-(h-1)//grid_h))
+    dx, dy = (w - 1) // (grid_w - 1), (h - 1) // (grid_h - 1)
+
     """
     average the green channels into one
     """