Depending on where a target is, I can have terrible gradients and mottled backgrounds in my images due to sky and local light pollution. I struggle each time to try to salvage bad images when simple background elimination doesn’t work well enough. Here, I’d like to outline a new idea that seems to work pretty decently for my galaxy images. It may also work for nebula images, but I have yet to try.
The main goal is to remove dim background fluctuations while retaining real target data by using a multi-scale method. The MultiscaleMedianTransform is the tool of choice for this. I think if this tool was able to work at an arbitrary number of scales, it could be used on its own. However, as it is, the tool is limited to seven scales and a residual.
Remove large scale gradients
The first step is to remove any very large scale gradients by using the DynamicBackgroundExtraction tool. This will largely be a manual process of placing points that do not include any targets. I typically use the division operation on this step.
We can take a very bad integration, such as this,

And turn it into a more workable image,

You can see that large scale color gradients from reflections and poor flat correction are mostly removed. We are left now with smaller scale artifacts in the background.
Remove structure
We want to make a version of the image that only contains the background artifacts that we want to remove. This image should have no stars, galaxies, etc., in it, just background. These variations can be quite large and also somewhat small, such as dust motes. We begin by cloning the image twice and down-sampling both clones using IntegerResample. We will need to down-sample the images small enough so that the available scales in MMT are able to affect them. For my example, I down-scaled the image by a factor of four.
Once the image is down-scaled, I can use MMT to remove the background artifacts, which are essentially low-contrast noise. What we are aiming for is an image that contains the target data, but not the background artifacts. You may need to play around with the noise reduction thresholds, but I went full blast,

This should give an image that has a flat background,

We will now subtract this image from the other down-sampled clone using PixelMath,


You should now have a new image that mostly contains the background artifacts,

Apply background correction
Now that we have a model that should mostly be the background artifacts, we can start to apply it to the original image. However, we first need to clean up this background model. Again, we use MMT. We will use it to remove small scale detail from the image,

This will smooth the background model, removing noise and small scale detail. You can play around with what scales you include so that you can remove background fluctuations without affecting target data too drastically. The background model should now be smoother, and contain no stars or small scale data,

With the background model satisfactory, use IntegerResample to upscale it to match the original image. Finally, we divide the original image by the background model,

The original image should now have a much smoother background. However, we may have affected some areas of the target from doing this. One trick to alleviate this is to now make a luminance mask from this corrected image. Stretch it and scale it so the background would be affected, but very little of the target would be,

Then undo the division you did to the original image, apply this mask inverted, and then do the division again. It should now have mostly only affected the background. Much better!
