Remove background gradient

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?".
Post Reply
geoland
Posts: 60
Joined: 2015-05-14T05:11:38-07:00
Authentication code: 6789

Remove background gradient

Post by geoland » 2017-10-30T22:56:35-07:00

I would like to remove background gradients in images taken at night, where the background is a function of street light intensity and colour.

Is the process described possible with IM.

"Simple gradients can be effectively removed by creating an image containing only a smooth plane, with a slope matching that of the background of your image and then subtracting this from your image. The background will be leveled out and the gradient eliminated

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

Re: Remove background gradient

Post by snibgo » 2017-10-31T01:08:43-07:00

Yes, it's possible. The classic example is a photo of text on light paper. If we simulate the blank paper with a gradient, we can divide the image by the gradient to get a white background for the text.

The hard part is figuring out the required gradient.

Can you link to an example?
snibgo's IM pages: im.snibgo.com

geoland
Posts: 60
Joined: 2015-05-14T05:11:38-07:00
Authentication code: 6789

Re: Remove background gradient

Post by geoland » 2017-10-31T01:49:43-07:00

Thanks. This is a half size jpg of the original linear tiff. I want to get rid of the muddy red/brown colour/gradient. I know there are other programs that do this, for this type of image. I'm interested in an IM solution to add to my work flow. I have tried various methods by following the documentation without success, but I think it is more complex than at first glance.

https://www.dropbox.com/s/wc0c63z2j86l4 ... .jpeg?dl=0

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

Re: Remove background gradient

Post by snibgo » 2017-10-31T03:17:16-07:00

Gosh, that's a lot of stars.

Ideally you would have a dark-field image, a photo taken at the same time with the same exposure but with the lens cap on. This would give us the background noise of the sensor, which could be subtracted from the image.

(Code is IM v6, Windows CMD syntax. For v7, use "magick". Please say what version IM you use, on what platform.)

Or:

Code: Select all

convert stack_OB.jpeg -format "%[fx:minima.r] %[fx:minima.g] %[fx:minima.b]" info:
0.0117647 0 0
The values are on a scale of 0.0 to 1.0. So we can make an image of that colour and subtract.

Code: Select all

convert stack_OB.jpeg ( +clone -fill rgb(1.17647%,0%,0%) -colorize 100 +write x.png ) -compose MinusSrc -composite m.png
We can be more clever. We might assume that pixels lighter than a local average is a star, and other pixels are background. Replace star pixels with the local average. Then use that as the image to be subtracted.

Or...
snibgo's IM pages: im.snibgo.com

geoland
Posts: 60
Joined: 2015-05-14T05:11:38-07:00
Authentication code: 6789

Re: Remove background gradient

Post by geoland » 2017-10-31T03:52:51-07:00

Lots of stars... Orion. The belt region. The image is a stack of 21 frames. They are all dark subtracted, as well as bias subtraction and flat division before stacking. I see what you're doing. Interstingly, the red contribution is expected. The camera's IR filter has been removed and replaced with a special astronomical filter, sensitive to Hydrogen alpha / Red. I'll try this out.

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

Re: Remove background gradient

Post by snibgo » 2017-10-31T04:22:39-07:00

Ah yes, Orion's belt -- I have happy memories of cold winter nights with binoculars.

Another possibility is to assume the red values should be multiplied by a number less than one. What number? Let's try the average green divided by the average red, which is about 0.613. IM v7 command:

Code: Select all

magick stack_OB.jpeg -channel R -evaluate Multiply %[fx:mean.g/mean.r] +channel divred.png
The background is less red, but so are the stars -- they've become blue.

We could mask out the stars from the multiplication, but I don't know if that is technically justified. Or...

Code: Select all

convert stack_OB.jpeg -fuzz 30% -transparent Black -scale "1x1^!" -format %[fx:mean.g/mean.r] info:
0.89002
This make pixels within 30% of black transparent, so the scale will find the average colour of the brighter stars. We might assume that this average should have the same red and blue value, so we should multiply the red channel by 0.89002. That result looks good to me (a non-astronomer).
snibgo's IM pages: im.snibgo.com

geoland
Posts: 60
Joined: 2015-05-14T05:11:38-07:00
Authentication code: 6789

Re: Remove background gradient

Post by geoland » 2017-10-31T12:24:52-07:00

Thanks snigbo, greatly appreciated. With a better idea of the process, I'll play with it and see how it all shapes up.

Bighter stars are often used as a reference for whitebalance in astro images. This region is somewhat dominated by the three blue giants shedding their luminance across the field. These naturally over expose capturing the darker areas of interstellar dust. The dynamic range is huge.

http://synergous.com/flatpress/fp-conte ... obstds.jpg
Last edited by geoland on 2017-10-31T13:18:01-07:00, edited 1 time in total.

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

Re: Remove background gradient

Post by snibgo » 2017-10-31T13:15:12-07:00

Yeah, that's cool.

I suppose someone publishes spectral data about stars, and hence what they should look like when photographed. But that might give weak results like "a slightly blueish white" and "a slightly pinkish white", so boosting the saturation would make it look better. As often happens, a balance between accuracy and aesthetics.

Code: Select all

f:\web\im>%IM%convert stack_obstds.jpg -colorspace HCL -verbose info:

Image: stack_obstds.jpg
:
:
    Green:
      min: 0 (0)
      max: 133 (0.521569)
      mean: 20.9241 (0.0820553)
      standard deviation: 16.9627 (0.0665203)
      kurtosis: 5.31616
      skewness: 1.98782
      entropy: 0.800732
The "green" channel of HCL is C, for Chroma, a variety of saturation. The maximum is 0.52 (on a scale of 0.0 to 1.0), with a low mean (0.08). So we can divide the C by 0.52 to use the full range, and raise it to a power to boost the middle. (We could use "-auto-level -auto-gamma" instead.)

Code: Select all

convert stack_obstds.jpg -colorspace HCL -channel G -evaluate Divide 0.52 -evaluate Pow 0.4 +channel -colorspace sRGB -resize 600x600 -quality 40 starsboosted.jpg
Image
Excessive? Perhaps.
snibgo's IM pages: im.snibgo.com

geoland
Posts: 60
Joined: 2015-05-14T05:11:38-07:00
Authentication code: 6789

Re: Remove background gradient

Post by geoland » 2017-10-31T13:22:18-07:00

Perhaps, in this case, but not far off. A bit of tweaking will fix that. Good stuff. Many thanks again. I made a few changes to my previous post, btw.

geoland
Posts: 60
Joined: 2015-05-14T05:11:38-07:00
Authentication code: 6789

Re: Remove background gradient

Post by geoland » 2017-11-12T19:23:08-07:00


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

Re: Remove background gradient

Post by snibgo » 2017-11-13T03:56:09-07:00

Very cool.
snibgo's IM pages: im.snibgo.com

Post Reply