Enhancing starfield images

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
kevinbuckley70
Posts: 13
Joined: 2015-08-23T08:31:04-07:00
Authentication code: 1151

Enhancing starfield images

Post by kevinbuckley70 »

Having searched the forum there are discussions about creating starfields from scratch using ImageMagick but i can't find anything which addresses my specific need. And as a new user I'm struggling to figure out how to do what I want.

I have some images of the night sky which contain the sky: generally very dark and uniform, but not always totally black (or monochrome), stars: usually only a few pixels in size but of varying intensity, and the horizon: often in silhouette, but not always totally dark, and sometimes with distant lights which are generally large, compared with the stars.

I want to create a transformation which will identify the stars and ignore the larger light sources and other image elements, and then scale (or blur) the size of the stars based on their intensity. So bright stars look bigger in the final image than dimmer stars.

If I was writing an algorithm to do this I could think of a number of ways to achieve what I want but I'd appreciate some help understanding how to do this in ImageMagick.

Thanks!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Enhancing starfield images

Post by snibgo »

Advice is always easier if you put up a sample input image.

I'd break it down into two problems:

1. Which parts of the image are sky/stars to be enhanced, and which aren't? The answer may be: blur, then enhance the image that corresponds to very dark parts of the blur.

2. How to enhance the stars?

(1) might create a mask, so (2) is applied to the entire image, then composed over the original, with the mask.

For (2), some experimenting will give possibilities. For example, -morphology dilate disk.
snibgo's IM pages: im.snibgo.com
kevinbuckley70
Posts: 13
Joined: 2015-08-23T08:31:04-07:00
Authentication code: 1151

Re: Enhancing starfield images

Post by kevinbuckley70 »

This is a coarse mockup done in PhotoShop. Above is original, below is processed: https://www.dropbox.com/s/2d0gk88050maf ... e.jpg?dl=0
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Enhancing starfield images

Post by snibgo »

I quite like this (Windows BAT syntax):

Code: Select all

%IM%convert ^
  stars_example.jpg ^
  -profile sRGB.icc -strip ^
  -crop 1x2@ ^
  s.png

%IM%convert ^
  s-0.png ^
  -auto-level -auto-gamma ^
  -blur 0x10 ^
  -fill White -fuzz 10%% ^
  -draw "color 0,0 floodfill" ^
  -fill Black +opaque White ^
  smask.png

%IM%convert ^
  s-0.png ^
  -morphology dilate disk:2 ^
  sb.png

%IM%convert ^
  s-0.png ^
  sb.png ^
  smask.png ^
  -compose Over -composite ^
  sout.png
It would be much better to start from a better source image, not JPEG.

I convert to sRGB, and get the top half in s-0.png. Then I make a mask from a heavy blur, then flood-filling from top-left with white, and turning the rest black.

For the stars processing, I simply dilate (expand) them. I like this effect, for stars. It seems more "realistic" than a simple blur. (Dilating is actually a blur with a sharp cut-off.) Change the radius to 1 or 3 or whatever you like.

Then I compose the original with the dilated image, using the mask, so the foreground isn't dilated.

When the code does what you want, you could merge all the converts into a single command.
snibgo's IM pages: im.snibgo.com
kevinbuckley70
Posts: 13
Joined: 2015-08-23T08:31:04-07:00
Authentication code: 1151

Re: Enhancing starfield images

Post by kevinbuckley70 »

Cool. I'll have a play with that.

I've been playing with -morphology hitandmiss peaks:1 and contrast stretch to try to make a mask.

Kevin
kevinbuckley70
Posts: 13
Joined: 2015-08-23T08:31:04-07:00
Authentication code: 1151

Re: Enhancing starfield images

Post by kevinbuckley70 »

My originals are in RAW by the way. I can export from LightRoom in a different intermediate format if that's better.
kevinbuckley70
Posts: 13
Joined: 2015-08-23T08:31:04-07:00
Authentication code: 1151

Re: Enhancing starfield images

Post by kevinbuckley70 »

I just tried your suggestion and it's definitely headed in the right direction!

I'll go through it tomorrow and try to figure out what each step does.

Thanks.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Enhancing starfield images

Post by snibgo »

Jpeg is only 8 bits/channel/pixel, and usually lossy, which means the pixel colours are mangled. You can see this if you zoom in closely to a bright star.

Use 16-bit TIFF or PNG as an intermediate format.
snibgo's IM pages: im.snibgo.com
kevinbuckley70
Posts: 13
Joined: 2015-08-23T08:31:04-07:00
Authentication code: 1151

Re: Enhancing starfield images

Post by kevinbuckley70 »

Yes. I'll do that. These are actually part of a timelapse movie, so I want the 'final' images as jpg's for ease of handling. But if I know I'm going to include imagemagick as an intermediate step I can work in TIFF or PNG and presumably output the imagemagick finals as jpg's.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Enhancing starfield images

Post by snibgo »

Yes, if you are going to assemble into a movie (using ffmpeg or similar), JPEG as input to that process will save time and disk space, at very little quality loss. But keeping a high precision until then could make a big difference. I use "-auto-level -auto-gamma" to make the mask, which swings the very dark (almost black) image to mid-gray. Almost black 8-bit JPEG has very little precision at these levels. There are probably many more stars visible in high-precision images. IM can write JPEG. For this, I suggest a high quality setting, such as "-quality 100".

I should say: I converted to sRGB merely because I'm not accustomed to working in AdobeRGB. If you want to stay in AdobeRGB, remove my line "-profile sRGB.icc -strip". I don't know if ffmpeg handles AdobeRGB, or if it does, how well. Can output movies be AdobeRGB? If so, should they be? I don't know.

As with JPEG compression, there is a case for keeping in the original colorspace until the final conversion.
snibgo's IM pages: im.snibgo.com
kevinbuckley70
Posts: 13
Joined: 2015-08-23T08:31:04-07:00
Authentication code: 1151

Re: Enhancing starfield images

Post by kevinbuckley70 »

Working my way through this, although I should probably stop now due to the lateness of the hour!

I need to look at it again tomorrow but it seems the main effect is to increase the size of all the point sources in the 'sky' - which it does very well.

But what I want is for the size of those point sources to be in some way more directly related to their brightness, not their original 'size'. Whilst retaining the original brightness in the final image. I'm not convinced it is doing that.

So for example (in 8-bit RGB terms), if a star is mostly #FFFFFF and occupies 13 pixels and another star is #BBBBBB and occupies 5 pixels (as brighter points tend to smear out a bit) then in the processed image the first star might now occupy 41 pixels of mostly #FFFFFF and the second one might be 13 pixels of around #BBBBBB. And stars occupying say 5 pixels of #888888 would stay that way.

However, a star occupying 5 pixels of around #FFFFFF would also be scaled up to 41 pixels because the original pixel count is most likely a lens/sensor artifact.

Apologies if I have misunderstood your example!

I was trying to find a way to:
  • Mask the things which aren't 'sky'
  • Convert areas of brightness above a threshold (so a star) to a single, central pixel, of brightness equal to the average of that area
  • Expand (blur?) that single pixel by an amount (distance) related to it's brightness and then change the average brightness of the blurred spot to be the same as the brightness of the single pixel
  • Overlay that modified starfield back onto the original image
Does that make sense?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Enhancing starfield images

Post by snibgo »

smask.png is white where there is sky, and black elsewhere, roughly. It could be improved.

With my script, bright stars seem to be unchanged in colour, but dim stars become slightly lighter. This is fixable, so they all remain the same colour.

Dilate should increase the stars by the same number of pixels. I understand you would like more pixels added to bright stars than are added to dim stars. A simple threshold can do this:

Code: Select all

%IM%convert ^
  s-0.png ^
  -colorspace gray ^
  -threshold 15%% ^
  ss3.png
Note the noise around the bright stars, possibly from JPEG noise.

ss3.png could then be merged with my results above to give the output size of stars.

Another tool for the toolbox: we can spread the lightest values into a square shape, any size we want:

Code: Select all

%IM%convert ^
  s-0.png ^
  -statistic maximum 10x10 ^
  sx.png
We could then use ss3.png as a mask, like a black sheet with pinprick holes, through which we can see colours in sx.png. But the noise would create problems.

Incidentally, we can massively increase the saturation:

Code: Select all

%IM%convert s-0.png -modulate 100,1000,100 sat.png
But it's getting late here, as well.
snibgo's IM pages: im.snibgo.com
kevinbuckley70
Posts: 13
Joined: 2015-08-23T08:31:04-07:00
Authentication code: 1151

Re: Enhancing starfield images

Post by kevinbuckley70 »

Just FYI, I put some actual images in the dropbox. RAW, TIFF, PNG and JPG of the same image. This is what I'm using as my test image.

https://www.dropbox.com/sh/3r285p0x1c1a ... _ECxa?dl=0
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Enhancing starfield images

Post by snibgo »

Very nice. Shame about the cloud, and light pollution.

I see that stars in the top corners are blurred in a radial direction way from the centre of the image. I suspect this is a characteristic of the very wide-angle lens. (What lens is it? 14mm, on a 24x36mm sensor?) If we depolar the image, the blur becomes vertical. We could then do a Lucy-Richardson deconvolution to deblur, then re-polar the image. The deconvolution might have to be done a number of times (eg 3 times), corresponding to different radii of the photograph, and the results blended together.

For stars.tif, a reasonable mask that is white where the sky is, and black elsewhere (Windows BAT syntax):

Code: Select all

set SRC=stars.tif

for /F "usebackq" %%L in (`%IM%identify ^
  -format "WW=%%w\nHH=%%h\nWm1=%%[fx:w-1]\nHm1=%%[fx:h-1]\nW_2=%%[fx:w/2]\nH_2=%%[fx:h/2]" ^
  %SRC%`) do set %%L

%IM%convert ^
  %SRC% ^
  -auto-level -auto-gamma ^
  -blur 0x10 ^
  -fill White -fuzz 6%% ^
  ( -clone 0 ^
    -draw "color 0,0 floodfill" ^
  ) ^
  ( -clone 0 ^
    -draw "color %W_2%,0 floodfill" ^
  ) ^
  ( -clone 0 ^
    -draw "color %Wm1%,0 floodfill" ^
  ) ^
  -delete 0 ^
  -background Red ^
  -compose Lighten -layers flatten ^
  -fill Black +opaque White ^
  -morphology erode disk:10 ^
  smask.png
This blurs to remove the stars, then finds pixels that are a similar colour to top-left, top-middle or top right.
snibgo's IM pages: im.snibgo.com
kevinbuckley70
Posts: 13
Joined: 2015-08-23T08:31:04-07:00
Authentication code: 1151

Re: Enhancing starfield images

Post by kevinbuckley70 »

The lens is a Samyang 10mm f2.8 - I haven't got around to finding the LightRoom distortion parameters for it yet. It has quite a bit of vignetting too which I roughly took out in LR.

I'm out and about today but I'll play with this tommorow. Thanks again for your help.
Post Reply