Author Topic: Hot Pixel Removal in Bayered Images  (Read 2789 times)

Offline dmcclain

  • PixInsight Addict
  • ***
  • Posts: 117
Hot Pixel Removal in Bayered Images
« on: 2016 January 20 13:50:44 »
I found the following to work well. It works directly on Bayered gray-scale images, rather than the un-Bayered color images.

A hot pixel is any peak that rides more than 10 MAD above the local median level defined by the nearest 4 pixels of the same Bayer cell pattern. Hence, it looks +/- 2 pixels from the pixel under consideration. Change the threshold with symbol f.

This avoids the deBayering process which smears a hot pixel into adjacent pixels and causes it to lose some of its defining character. In some cases, I want to split the Bayer channels, and not ever deBayer the image.

This is just dying to be parallelized into 4 concurrent streams... But it runs, more or less, acceptably fast in PI.

PixelMath expression:
------------------------
w=width($T)-2;
h=height($T)-2;
x1 = x();
y1 = y();
x0 = iif(x1<2,0,x1-2);
y0 = iif(y1<2,0,y1-2);
x2 = iif(x1>w,w+1,x1+2);
y2 = iif(y1>h,h+1,y1+2);
p11 = pixel($T,x1,y1);
p10 = pixel($T,x1,y0);
p12 = pixel($T,x1,y2);
p01 = pixel($T,x0,y1);
p21 = pixel($T,x2,y1);
pmed = med(p10,p12,p01,p21);
pmad = mdev(p10,p12,p01,p21);
f=10;
iif(p11 > pmed + f*pmad,pmed,p11)
--------------------------
PixelMath Symbols: w, h, x1, y1, x0, x2, y0, y2, p11, p01, p21, p10, p12, pmed, pmad, f

Offline pfile

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 4729
Re: Hot Pixel Removal in Bayered Images
« Reply #1 on: 2016 January 20 14:44:16 »
nice work.

i think CosmeticCorrection should be able to work on CFA images, did you try?

rob

Offline dmcclain

  • PixInsight Addict
  • ***
  • Posts: 117
Re: Hot Pixel Removal in Bayered Images
« Reply #2 on: 2016 January 20 16:02:23 »
Very good. Thanks for the suggestion.

Interestingly, it takes 5 sec to run against one image, while the PixelMath expression above takes about 6 sec. I would have expected a more dramatic performance increase from a C primitive.

Offline NKV

  • PTeam Member
  • PixInsight Guru
  • ****
  • Posts: 677
Re: Hot Pixel Removal in Bayered Images
« Reply #3 on: 2016 January 20 23:49:07 »
1sec... good! CC module wins!

Also, I hope, CC will win in multi file processing. Because CC have very fast file preloaded. ;)

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: Hot Pixel Removal in Bayered Images
« Reply #4 on: 2016 January 21 03:05:53 »
This version of your PixelMath expression should run faster:

x0 = iif( x() < 2, 0, x()-2 );
y0 = iif( y() < 2, 0, y()-2 );
x2 = iif( x() > w-2, w-1, x()+2 );
y2 = iif( y() > h-2, h-1, y()+2 );
p10 = pixel( $T, x(), y0 );
p12 = pixel( $T, x(), y2 );
p01 = pixel( $T, x0, y() );
p21 = pixel( $T, x2, y() );
pmed = med( p10, p12, p01, p21 );
pmad = mdev( p10, p12, p01, p21 );
iif( $T > pmed + f*pmad, pmed, $T )


With symbols defined as:

w = width(),
h = height(),
x0,
x2,
y0,
y2,
p01,
p21,
p10,
p12,
pmed,
pmad,
f = 10


The improvements are that now w, h and f are constants during the whole process, and the assignments to x1, y1 and p11 have been suppressed.

PixelMath is fast, especially considering that it is an interpreter (the source code is compiled to p-code and executed), but CosmeticCorrection works with a neighborhood of at least 25 pixels and its core operation is not separable (I say this after a very quick look at the source code; some neighborhoods used to calculate medians and means internally can be larger).
Juan Conejero
PixInsight Development Team
http://pixinsight.com/