Page 1 of 1

Dark frame removal question

Posted: 2010-08-29T05:48:50-07:00
by hwttdz
So I have a digital camera I'm using to take what is essentially a time lapse series of images. During the night exposures obviously noise is a big issue. I took a dark frame and I'm subtracting that from the night images, but I'm a little confused about
1) how to correct light images
2) how to determine if an image is a night image or a dark image

Just subtracting the dark frame leaves dark spots on a daytime image. Perhaps I shouldn't be performing simple subtraction during nighttime (but it seems to work).

So what I would like to do is
1) take a picture - set here, I'm using fswebcam, and I'm happy to share what I've figured out if anyone has questions
2) compute average color of entire image - I'm currently using resize to 1x1 pixel and saving that as a text file, I can then parse the text file in the wrapping program
3) compute some correction based on the brightness and the dark frame
4) perform dark frame removal as a function of brightness - output_image = f(input_image, input_brightness, dark_frame)

So I'm looking for some help with 3 and 4.

Re: Dark frame removal question

Posted: 2010-08-29T06:11:27-07:00
by hwttdz
One idea that I have for correction
output_image(x,y) = input_image(x,y) - dark_frame(x,y) + dark_frame(x,y)*(gaussian average of local pixels, excluding x,y)

which has the nice characteristics that if dark(x,y) = 0, output(x,y) = input(x,y)
and amount of correction is proportional to the value of the dark frame.

This is pseudo-code, but I suppose one would implement it on a per-channel basis, where values range from [0,1] . i.e.
output_green = input_green(x,y) - dark_green(x,y) + dark_green(x,y)*(local gaussian green average excluding x,y)

Re: Dark frame removal question

Posted: 2010-08-29T17:52:33-07:00
by hwttdz
This is roughly my solution, seems to be working ok. I have more plans, but they don't relate to the original question (summing images if the output image is too dark, since I can't increase the exposure time further).

The end:
final = start - start*dark + dark*local_color = (1-dark) * start + dark * local
This function seems to exhibit the properties I want. It's weighted average between the original image and a severely blurred image. If dark=0 (i.e. actually dark), then it just outputs the original image, if dark=1 (i.e. hot pixel), it outputs the average of the surrounding pixels (current pixel is not discounted, but it seems pretty ok).

And the specifics. It's actually contained in a perl wrapper, that way I can easily change filenames and whatnot.

fswebcam --config ./configs/webcamconfig # grabs the image
convert -compose multiply -composite $mydark $start $startbydark # the product of the dark frame and the starting image is now in startbydark
convert -evaluate Multiply 2.54 $startbydark $startbydark # I multiply by 2.54, as the math has some weird 255=100 thing, I'm sure it's documented, but...

#start - dark
convert -compose minus -composite $startbydark $start $lessdark # lessdark = start - start*dark = start (1-dark)

#blurs
system("convert -gaussian-blur 20x$radius $start $start\_blur.png # so I can easily get the local color

#local * dark
convert -compose multiply -composite $mydark $start\_blur.png $corrections # corrections = dark * local_color
convert -evaluate Multiply 2.54 $corrections $corrections # fixing the math again, yes I realize this should be 2.55 and not 2.54

convert -compose plus -composite $lessdark $corrections final.png # final = start - start*dark + dark*local_color = (1-dark) * start + dark * local

Re: Dark frame removal question

Posted: 2010-08-29T18:23:05-07:00
by fmw42
3) compute some correction based on the brightness and the dark frame
4) perform dark frame removal as a function of brightness - output_image = f(input_image, input_brightness, dark_frame)

So I'm looking for some help with 3 and 4.
You might look into -compose divide to correct dark images as per the example at http://www.imagemagick.org/Usage/compose/#divide

Don't know if that is relevant or will help.

Re: Dark frame removal question

Posted: 2010-08-29T18:28:09-07:00
by hwttdz
As I see it the divide function as outlaid there seems to be more useful for removing large scale discolorations (i.e. the blurred image has the same undesirable characteristic as the original). In my case blurring the image actually totally masks the effect of having faulty pixels (which is why I'm using the blurred image to lookup correction values). Of course I'm sure there are a load of uses for it of which I haven't yet thought.

In fact I think I rather light the weighted average of the blurred image and the original with the weights coming from the dark frame. I think it really makes sense, and so far it seems to produce some pretty good images. I found that at night I was getting noisy images, but during the day the images were quite good. And the standard solution (simple subtraction of dark frame), left dark "holes" in the day images. This correction has almost no effect at day (or on bright images), and does a very good job of noise removal at night.

I have since investigated this sort of correction on my digital camera (as opposed to a cheap webcam). Turns out digital cameras have very low noise in the first place.

Re: Dark frame removal question

Posted: 2010-08-29T18:33:34-07:00
by anthony
There are two aspects to this.

These is a 'constant' noise component (specific pixels brighter or darker than others)
and photon noise which in night photos is quite random, varing from one image to the next.

For some details look at the links...
http://dpfwiw.com/c-2000z/low-light/index.htm
http://www.guillermoluijk.com/article/n ... dex_en.htm

The second one looks at noise problems in detail in the second half of the artical, but also deals with merging of images using multiple 'f-stops' or exposure levels to get 'perfect' pictures, to generate High Dynamic Range Pictures (HDRI).

NOTE: this is nothing to do with the HDRI of IM whcih is really a miss-naming of a floating-point memory compile time option.

Re: Dark frame removal question

Posted: 2010-08-29T18:35:36-07:00
by hwttdz
Right, and I'm only correcting for the constant noise. The improvement is quite marked and satisfactory. This is more just for fun and not so much for science, so I'd say on the noise removal front, I've certainly hit my "good enough" level.

I'm actually a little confused if sharpness should be set up or down when taking the dark frame. The article says down, but if there is post processing inside the camera (or the computer for that matter), it would blur your hot pixels. On the other hand if you have sharpness set high and there is post processing you might introduce artifacts around the hot pixels.

Re: Dark frame removal question

Posted: 2010-08-29T18:53:54-07:00
by anthony
This is where 'raw' camera image formats come in.

You can switch your camera to generate 'raw' images so you get the raw pixel data, and can process it yourself. Look at your camera's options menu.

But if you have it good enough, that is fine. Others following this discussion now, (or in a couple of years time) may find the links useful.