Author Topic: Pixel replacement for saturated areas.  (Read 13791 times)

Offline Jack Harvey

  • PTeam Member
  • PixInsight Padawan
  • ****
  • Posts: 975
    • PegasusAstronomy.com & Starshadows.com
Re: Pixel replacement for saturated areas.
« Reply #15 on: 2009 December 08 09:05:31 »
This is a jpeg of the result I got trying to replace the saturated pixels in the trapezium.

Jack Harvey, PTeam Member
Team Leader, SSRO/PROMPT Imaging Team, CTIO

Offline Niall Saunders

  • PTeam Member
  • PixInsight Jedi Knight
  • *****
  • Posts: 1456
  • We have cookies? Where ?
Re: Pixel replacement for saturated areas.
« Reply #16 on: 2009 December 08 09:34:37 »
Hmmm, yep - for sure THAT didn't work  ;D

What I am now thinking is that Juan's 2nd 'Mosaic' tutorial may actually have the solution.

Obviously the first parameter that you need is the one that actually defines the 'edge' of the transition - in other words, you need to set the 'break-point' parameter by trial and error on your first image.

You then need to use that parameter to create a 'Black and White Mask', from the first image, that you can then apply to the second image (I hope I am remembering Juan's steps correctly here  :-\)

Then you need the 'two-parameter' PixelMath equation that first allows you to set the 'background' level of the second image - such that the edges blend, and then you finally adjust the 'foreground' to suit.

Obviously the exact steps are not identical to Juan's tutorial - because your transition point is actually AT a level that you have set by the first parameter. But your 'second' parameter should give you the ability to 'disguise' the edge. And you might be able to adjust the 'third' parameter by trying to create equal brightness levels in the highlights once again.

No matter what though, there is no getting away from the fact that 'Feather In' and 'Feather Out' algorithms are a huge bonus when it comes to working with Masks - I have been using them in Corel PhotoPaint for over ten years now (even though I now also have PS2, I just 'don't like it' as much as I like the Corel suite). I must admit that, if I really needed a 'feathered edge', I would probably drop out of PI at the moment, and would consider creating the feathered edges in CPP prior to bringing the mask back into PI). Sledgehammer to crack a nut, I'm afraid.

HTH
Cheers,
Niall Saunders
Clinterty Observatories
Aberdeen, UK

Altair Astro GSO 10" f/8 Ritchey Chrétien CF OTA on EQ8 mount with homebrew 3D Balance and Pier
Moonfish ED80 APO & Celestron Omni XLT 120
QHY10 CCD & QHY5L-II Colour
9mm TS-OAG and Meade DSI-IIC

Offline Simon Hicks

  • PixInsight Old Hand
  • ****
  • Posts: 333
Re: Pixel replacement for saturated areas.
« Reply #17 on: 2009 December 08 11:38:01 »
Try this....it might work!

Two images, L and S. First get them the same illumination using  Juan's expression

    (L - Med(L)) * AvgDev(S)/AvgDev(L) + Med(S) applied to L with rescale enabled.

Now create a mask M by taking the luminance of L. Stretch it, feather the edges using ATW, etc.

Now in PixelMath, with Rescale enabled, use;

   (L*(1-M) ) + (S*M) 

This seems to give an image with the bright parts of S and the dim parts of L with a smooth transition between them determined by the structure of the mask.

However, I think the multiplication brings some non-linearities into the final image....and I don't know how to get round that. Maybe someone can improve on it?

Cheers

          Simon
« Last Edit: 2009 December 09 00:11:47 by simonhicks »

Offline Astrocava

  • PixInsight Addict
  • ***
  • Posts: 180
    • Astrocava.com
Re: Pixel replacement for saturated areas.
« Reply #18 on: 2009 December 10 03:55:45 »

Easy. Enter this expression in the RGB/K slot:

Code: [Select]
iif( $T < low, fix_value, $T )
and this in the Symbols slot:

Code: [Select]
low=0.025, fix_value=0.05
where the numbers are just examples; replace them with your actual values.

Don't forget to disable the Rescale option if you don't want to rescale the image to the [0,1] range at the end of the process.


I think this can be useful too to kill hot pixels changing  more or less in this way:

Code: [Select]
iif( $T > A, B, $T )
Where A can be a number (Almost all my variable hot pixels are above certain value and

B should be the median value of surrounding pixels with a different expressions for monochrome cameras and color cameras.

What do you think?

Sergio


Moonfish ED80 over a Meade LX200GPS 8"

Offline Niall Saunders

  • PTeam Member
  • PixInsight Jedi Knight
  • *****
  • Posts: 1456
  • We have cookies? Where ?
Re: Pixel replacement for saturated areas.
« Reply #19 on: 2009 December 10 05:45:37 »
Quote
B should be the median value of surrounding pixels

So, we can already quickly create an expression in PixMath like the following
Code: [Select]
ImgMed=med($T)
Wouldn't it be nice to be able to create the following
Code: [Select]
AnnulusMed=AMed($T)
where AnnulusMed (only selected as an example; AnnMax, AnnMin, AnnAvg, AnnSD, etc. might also be immediately accessible) is calculated 'on the fly' for every pixel in the target image, in this case being the Median value of either the 'centre', the 'ring' or the 'outside' of an annular area, centred on the current pixel, as the PCL core scans all pixels in an image.

Similar approaches could be made using the notion of 'square' or 'circular' target areas which, like the Annular target area described above, could have user-defined pixel dimensions.

I see this as a way of quickly obtaining some statistical value that is applicable to a SPECIFIC PIXEL, based on those pixels surrounding this target pixel.

Having this type of data available, easily, in PixMath, would allow for the ingenious types of pixel substitution that we are looking for, the final substitution also being controlled either by an overall external Mask, or by judicious use of IIF(a,b,c) statements.

Not being next to my PI computer, can anyone confirm whether a PixMath 'Expression' can be of the form
Code: [Select]
PV_TopRight_Bu = pix(x-1,y+1,2)
where x and y do not have to be defined by way of any 'For-Next' loop statements, as PixMath already 'scans' an image, for all pixels of all rows, and all columns and all colour planes
where the '2' in the pix(x,y,c) statement refers to the 'Blue' channel

and where PV_TopRight_Bu will be assigned the image pixel value of the pixel that is one row above, and one column to the right of the current pixel, in the Blue colour channel

If this IS possible, then presumably further expressions could be created for the 'other' surrounding pixels, leading to the ability to calculate either the Mean or Median of these surrounding pixels - obviously providing a means of permitting 'intelligent' pixel substitution by way of a set of pre-defined, 'loadable' PixMath process icons (all of which were fundamentally image-independent)

If THAT sort approach is 'do-able', then huge 'power' can be given to a user - without the complication of immersing them in the details of PJSR, etc.

Cheers,
Cheers,
Niall Saunders
Clinterty Observatories
Aberdeen, UK

Altair Astro GSO 10" f/8 Ritchey Chrétien CF OTA on EQ8 mount with homebrew 3D Balance and Pier
Moonfish ED80 APO & Celestron Omni XLT 120
QHY10 CCD & QHY5L-II Colour
9mm TS-OAG and Meade DSI-IIC

Offline Jack Harvey

  • PTeam Member
  • PixInsight Padawan
  • ****
  • Posts: 975
    • PegasusAstronomy.com & Starshadows.com
Re: Pixel replacement for saturated areas.
« Reply #20 on: 2009 December 10 07:04:01 »
Boy did not take long for this to get over my head.  I think I will wait for the script<G>
Jack Harvey, PTeam Member
Team Leader, SSRO/PROMPT Imaging Team, CTIO

Offline Simon Hicks

  • PixInsight Old Hand
  • ****
  • Posts: 333
Re: Pixel replacement for saturated areas.
« Reply #21 on: 2009 December 10 07:21:52 »
Jack....keep the faith.....I will have something for you to try tomorrow!

Offline Jack Harvey

  • PTeam Member
  • PixInsight Padawan
  • ****
  • Posts: 975
    • PegasusAstronomy.com & Starshadows.com
Re: Pixel replacement for saturated areas.
« Reply #22 on: 2009 December 10 08:20:37 »
Hey that would be great.  I will look forward to it for sure.  Have a batch of 900 sec and 120 sec images to try.
Jack Harvey, PTeam Member
Team Leader, SSRO/PROMPT Imaging Team, CTIO

Offline Jack Harvey

  • PTeam Member
  • PixInsight Padawan
  • ****
  • Posts: 975
    • PegasusAstronomy.com & Starshadows.com
Re: Pixel replacement for saturated areas.
« Reply #23 on: 2009 December 10 13:27:06 »
Simon sent a .psm file that I tried on my images.  Here are crops of the trapezium area.  One is from the Master Blue 900 sec with the trap burned out.  The other is using the 900sec and a 120 sec blue master frame combined with Simon's Pixel Math psm.  Looks great to me.  Does require a bit of work, but now that the maths prove out perhaps a script in the offing?







Jack Harvey, PTeam Member
Team Leader, SSRO/PROMPT Imaging Team, CTIO

Offline Jack Harvey

  • PTeam Member
  • PixInsight Padawan
  • ****
  • Posts: 975
    • PegasusAstronomy.com & Starshadows.com
Re: Pixel replacement for saturated areas.
« Reply #24 on: 2009 December 10 14:37:17 »
One other comment I want to be assured about.  It is important not to just merge the two images but to use the data from the short duration image to replace the saturated stars and nebula of the long duration image.  FOr instance anything above 40,000 ads gets replaced by short duration data.
Jack Harvey, PTeam Member
Team Leader, SSRO/PROMPT Imaging Team, CTIO

Offline Simon Hicks

  • PixInsight Old Hand
  • ****
  • Posts: 333
Re: Pixel replacement for saturated areas.
« Reply #25 on: 2009 December 10 15:15:34 »
Hi Jack....really glad it worked!

If anyone wants to try it out then you can download the .psm file from http://cid-7754931c69f7958d.skydrive.live.com/self.aspx/Public/ImageMergeV3.psm .

Then use Process / Process Icons / Load Process Icons and locate the file....it will then pop up on the PI background as a new PixelMath instance with the equations and setting altready loaded. To use it you need to do the following;

Open a short exposure image and rename it S.
Open a long exposure image and rename it L. Hopefully L and S have been registered to each other.
Scale L down to S, i.e. maybe as simple as L/10 if L was 10X the exposure duration of S.

Create a mask. This could be just taking the luminance of L (or S). The transition from dark to bright in the mask will control the transition from long exposure to short exposure in the process.

Then apply the PixelMath instance to L. You will then get the combined image, hopefully with the good bits from each. Try changing the symbol F, anywhere in the range from 0 to 1 and see the effect. Also changing the mask will have an effect.....maybe dilation or erosion or stretching to suit the effect you are try to get.

Quote
One other comment I want to be assured about.  It is important not to just merge the two images but to use the data from the short duration image to replace the saturated stars and nebula of the long duration image.  FOr instance anything above 40,000 ads gets replaced by short duration data.

You can achieve this by making everything above 40,000ADU in the mask white. Better to do a histogram stretch so you still have a gradient of some sort for seamless edges and a smooth transition. Where the mask is white you have 100% short exposure image, where it is black you have 100% long exposure image. The factor sets the intensity at which these two have equal weight, and the mask sets the spatial and gradient of the transition.

If anyone can turn it into a user friendly script.....with proper image name recognition, a slider for the factor, and maybe live preview then that would be superb!

Good luck

Simon

Offline Simon Hicks

  • PixInsight Old Hand
  • ****
  • Posts: 333
Re: Pixel replacement for saturated areas.
« Reply #26 on: 2009 December 11 09:01:33 »
For anyone who is prepared to convert this into a script....here's a bit of background.....

The underlying equation is quite straight forward. It is basically;

Combined Image = [(L * F) / ( F + M )]  + [( S * M ) / (F + M )]

Where;
    S is the short exposure image
    L is the long exposure image that is registered to S and scaled to it as well.
    M is the greyscale mask taken from the luminance of S or L....maybe stretched.
    F is the scaling factor.

If we substitute F* for F where;

   F* = (1-(1/(F+0.000001))-0.9999)

then it lets F go from 0 to 1 and gets it round the correct sense in the first equation, i.e. zero is all S and 1 is all L. The 0.000001 and the 0.9999 are there as a trick to avoid a 'divide by zero' error. I'm sure some of you will know a better way than this.

So I can imagine a script that would let you pick two open images. It would then automatically scale the brighter one to the dimmer one. It could either let you specify an already created mask, or it could automatically create one by taking the luminance of the brighter image and clipping the background to black and stretching the bright parts to white. It could apply a quick smoothing of the mask by removing the first one or two wavelet layers?

It would then have a slider bar that varies F from zero to 1 with a live preview. The resultant image and the live preview would be 'rescaled'.

I think that's all there is to it. Unfortunately I don't have a scooby how to write a script, so someone with more skill than me would need to do it. Any takers?

Cheers
         Simon

Offline Simon Hicks

  • PixInsight Old Hand
  • ****
  • Posts: 333
Re: Pixel replacement for saturated areas.
« Reply #27 on: 2009 December 12 09:22:39 »
Hi Jack,

I've been thinking about the practical usage of the psm file. I've realised it might be easier to use it in the following way;

Create all the S, L and M files as before. Leave the factor F at a value of 0.5. Now inspect your scaled long exposure image L and decide at what value of intensity you want to change over to the short unsaturated image. I have my image info dispalying intensities between 0 and 1, so on my image it might be at say 0.7. The obvious place to change over is just less than the point at which L becomes saturated. So once you have scaled L to S, then look at the intensity of the saturated trapezium. If this value is say 0.8 then maybe use 0.7 as the cross over point.

Then open the mask image M and open Histogram. You will see the big median peak and a very low tail towards the brighter end. Drag the left hand side clip to say 0.6 and drag the right hand clip to say 0.8. So you now have a very steep straight line centered around an intensity of 0.7. Everything below 0.6 should be black, and everything above 0.8 should be white.

If you apply this Histogram to the mask, and then run the .psm process then the following will happen;

1) Everything above 0.8 will be replaced by 100% short exposure unsaturated image
2) Everything below 0.6 will be replaced with 100% long exposure low S/N image.
3) Everything between 0.6 and 0.8 will be blended between the two images with the proportion of L decreasing, and the proportion of S increasing as the intensity changes from 0.6 to 0.8.

So this allows you to inspect your image, make an educated choice of where you want the change over to happen, and also lets you change the range of intensities over which the change will happen, i.e. the smoothness.

Maybe the above method would make for a more automated script?

I hope this helps.

Cheers
          Simon