A few days ago I took a picture of the night sky using my camera, right in the middle of nowhere. I expected a nice clear picture but no matter where I looked, there was always some background light, and the sensor noise was quite strong:
I wanted to play with the python version of OpenCV for a long time, so this was a great opportunity to try to improve the result! And I’m in love with the python bindings: making Numpy, OpenCV and Scipy work together is really easy !
- Estimate the background light on blocks of 256x256 pixels using the maximum of the histogram on the block, for each color channel.
- Create a map of the background light by upscaling the estimation to the size of the image.
- Remove the background map from the image.
- Apply a threshold to the image in order to remove the sensor noise.
- Apply a small gaussian filter to smoothen the edges left by the thresholding.
- Adjust the gamma correction to brighten the image
Depending on the calibration on your screen and your environment, the effect may not be very noticeable. You can switch between the full size images to get a better view, see the gallery at the end of the post.
The subtracting step in this script is not really correct. We are subtracting the pixels value of the background from the image, but the pixel value in RGB space is not a linear function of the amount of light. It works well here because the background map does not change much, so that each star is dimmed approximately by the same value. I should work on the raw image, straight out of the sensor, where the value of pixels is proportional to the number of photons received.
Local orientation estimation
As a bonus, I wanted to estimate the orientation of the star trails in the images. I designed a simple estimator using the gradient. For each block in the image, the computation of the angle is done as follow:
- Estimate the gradient image with a Sobel filter on X and Y.
- Select the points corresponding to the star trails with a simple adaptative threshold on the gradient value (low value = background noise).
- Compute the angle for each selected pixel with the atan function. The norm of the gradient is also generated, and will be used as weight for the histogram generation.
- Generate the histogram of the angles for the selected pixels.
- Select the highest value of the histogram as the angle for the block.