Removing dust

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
Ravana
Posts: 5
Joined: 2011-11-17T17:44:01-07:00
Authentication code: 8675308

Removing dust

Post by Ravana »

Hello,

I have some tiff files from scanner where the alpha channel represent dust that I want to blur out.
I am trying with the same process as in the “Removing Text and Logos” example but I can’t get one step in the example to work properly.

http://www.imagemagick.org/Usage/photos/#removing
convert zelda_text.jpg \( zelda_text_mask.gif -negate \) \
-alpha off -compose CopyOpacity -composite \
-channel RGBA -blur 0x2 +channel -alpha off \
zelda_text_fill.jpg

This step from the example leaves a lot more of the text the in the blurred image when I run then in the example picture.
What am I missing?

And this is my command for removing the dust so far:
convert.exe ( scan0003.tif -alpha off ) scan0003_blur.tif ( scan0003.tif -channel matte -separate -negate ) -composite scan0003_fixed.tif
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Removing dust

Post by fmw42 »

can you post a link to your tif file so others can test and see what you are talking about?
Ravana
Posts: 5
Joined: 2011-11-17T17:44:01-07:00
Authentication code: 8675308

Re: Removing dust

Post by Ravana »

Here's an example image.

https://docs.google.com/file/d/0B5FjK44 ... sp=sharing

It doesn’t display properly in Google’s web viewer but you can download it.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Removing dust

Post by fmw42 »

I do not understand. I have separated your alpha channel and can clean it up completely. But why not just turn off the alpha channel.

convert scan0003.tif -alpha off result.png (or .tif)

If you want the alpha channel cleaned up except for the black specs then

convert scan0003.tif -alpha off scan0003_aoff.png
convert scan0003.tif -alpha extract scan0003_alpha.png
convert scan0003_alpha.png -negate -lat 10x10+20% -negate scan0003_alpha_lat.png
convert scan0003_aoff.png scan0003_alpha_lat.png -alpha off -compose copy_opacity -composite result2.png



If you want the alpha channel cleaned up to remove mostly the black specs, then

convert scan0003.tif -alpha off scan0003_aoff.png
convert scan0003.tif -alpha extract scan0003_alpha.png
convert scan0003_alpha.png -morphology close octagon:2 scan0003_alpha_close.png
convert scan0003_aoff.png scan0003_alpha_close.png -alpha off -compose copy_opacity -composite result3.png


Perhaps you can clarify what exactly you want cleaned up.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Removing dust

Post by snibgo »

I have no experience of this methods of scanning then removing dust, but the following (Windows 7 script) gives a reasonable result.

Code: Select all

convert ^
  scan0003.tif ^
  -set colorspace RGB ^
  -colorspace sRGB ^
  ( +clone -channel RGB -statistic maximum 4x4 ) ^
  +swap ^
  ( -clone 0 -channel A -level 70%%,75%% -separate -negate ) ^
  -alpha off ^
  -compose Over -composite ^
  dedust.png
Rather than blurring, which smears the dark spot, I have taken the lightest pixel within a certain radius. Perhaps that radius should be larger.

I have also taken the liberty of assuming the input is RGB, and the output should be sRGB.
snibgo's IM pages: im.snibgo.com
Ravana
Posts: 5
Joined: 2011-11-17T17:44:01-07:00
Authentication code: 8675308

Re: Removing dust

Post by Ravana »

fmw42 wrote:I do not understand. I have separated your alpha channel and can clean it up completely. But why not just turn off the alpha channel.
The alpha channel in my image is more of an error channel it holds the position of dust and other non-transparent impurities in the scanned film. Just turning it off, leaving the image as it was when scanned would obviously be the easiest thing to do. But I would like to improve the image as far as possible by blurring and replacing the error zones with adjacent good pixels.
fmw42 wrote:Perhaps you can clarify what exactly you want cleaned up.
I want to clean up whatever’s under the 'alpha' mask as it shouldn't be there.


snibgo wrote: Rather than blurring, which smears the dark spot, I have taken the lightest pixel within a certain radius. Perhaps that radius should be larger.

I have also taken the liberty of assuming the input is RGB, and the output should be sRGB.
Thanks, I will try to understand that command and experiment around a bit.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Removing dust

Post by fmw42 »

If you separate the alpha channel, you will see that it is not just white with a few black specs. It has some uneven gray toning to it.

My two commands above fixed each of those issues. The first command evened out the gray so that the background was white. The second command left the gray, but removed the black specs. If you combine those commands, you will get a pure white background, which is thus perfectly opaque image and the same as if you removed the alpha channel.

I still don't understand the need to clean up the alpha channel to the degree that it would be just like removing the alpha channel. But I have given you two commands to clean up the two different issues, if you want to just deal with one or the other.

Each of those command sets can be combined into one command line or both can be combined into one command line similar to what snibgo has provided.
Ravana
Posts: 5
Joined: 2011-11-17T17:44:01-07:00
Authentication code: 8675308

Re: Removing dust

Post by Ravana »

fmw42 wrote:If you separate the alpha channel, you will see that it is not just white with a few black specs. It has some uneven gray toning to it.

My two commands above fixed each of those issues. The first command evened out the gray so that the background was white. The second command left the gray, but removed the black specs. If you combine those commands, you will get a pure white background, which is thus perfectly opaque image and the same as if you removed the alpha channel.
You’re right, I had missed that gray noise.
I will try around with -black-threshold and similar options as I am only interested in the serious errors.

fmw42 wrote: I still don't understand the need to clean up the alpha channel to the degree that it would be just like removing the alpha channel. But I have given you two commands to clean up the two different issues, if you want to just deal with one or the other
I don’t want to modify the alpha channel at all (not completely true with your discovery of noise), I want to fix the RGB channels using the ‘alpha’ as a mask to where the errors are. Then I will remove it completely.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Removing dust

Post by snibgo »

My script above has a bug: "-clone 0" should be "-clone 1".

In this scan, the "alpha" channel is actually (I think) infra-red. In theory, it ignores the image in the photographic negative, and picks up only dust on the film. Converting it to an image, it seems to me that the variation in the background tone (a low-frequency variation) is irrelevant, and only the small specks are important. My "-level" command is a crude way of removing irrelevant detail. A more sophisticated method will improve the result.

Then I use this as a mask to "fill in" pixels that are missing from the original because they were masked by dust. These pixels are darker than they should be. The question is, what values should we use for those pixels? The OP commands use a blurred copy of the original. This seems unsatisfactory to me, because it just smears the darkness. Pixels will still be too dark.

So I make each pixel the maximum value found within a rectangle centred on that pixel. It's difficult to do this for individual dust spots, so I make an entire image that way, but use the dust mask to use these "maximum" values only where there is dust.

My "maximum" technique is simple and fast, but a more accurate result might come from the rule, "for each dust pixel, use the value in the nearest non-dust pixel".

Here's an improved version, which is also sprinkled with "-write" commands so intermediate results can be seen. image.png is the acual image with dark dust spots. dust.png is the mask, showing dust as dark spots on a white background. dedust.png is the result.

Code: Select all

"%IMG685%convert" ^
  scan0003.tif ^
  -set colorspace RGB ^
  -colorspace sRGB ^
  ( +clone -channel RGB -alpha off -write image.png -statistic maximum 8x8 -write max.png ) ^
  +swap ^
  ( -clone 1 -write x.png -alpha on -channel A -level 70%%,75%% -separate -negate -write dust.png ) ^
  -alpha off ^
  -compose Over -composite ^
  dedust.png
When I open the three images as layers in Gimp, I can see the marks in image.png and dust.png don't quite line up. I use "-distort SRT" to line them up.

Code: Select all

"%IMG685%convert" ^
  scan0003.tif ^
  -set colorspace RGB ^
  -colorspace sRGB ^
  ( +clone -channel RGB -alpha off -write image.png -statistic maximum 8x8 -write max.png ) ^
  +swap ^
  ( -clone 1 -write x.png -alpha on -channel A -level 70%%,75%% -separate -negate -distort SRT 0,0,1,0,0.5,1.5 -write dust.png ) ^
  -alpha off ^
  -compose Over -composite ^
  dedust.png
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Removing dust

Post by fmw42 »

but a more accurate result might come from the rule, "for each dust pixel, use the value in the nearest non-dust pixel".
Nearest what? Spatial or graylevel?

-statistic may help? http://www.imagemagick.org/script/comma ... #statistic
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Removing dust

Post by snibgo »

I meant the nearest spatially. I can't see an easy way to achieve this, but it might involve morphology.

My "-statistic maximum 8x8" isn't ideal. It needs to be large enough to reach the edge of all dust spots, but if it is too large the dust spot correction may be lighter than it should be.

Another compromise: the infra-red image (dust.png) is offset with respect to the image. If this offset varies between scans, finding it every time would be a lot of hassle. Instead, dust.png could be blurred before the "-level", enlarging the apparent dust spots. However, this would correct pixels that don't need correcting.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Removing dust

Post by fmw42 »

I meant the nearest spatially.
Since the dust spot is more than one pixel wide, you may get multiple closest pixels. What about finding the dust spots and using -morphology edgeout to find the surrounding pixels and then average those surrounding pixel. Or using -sparse-color to fill in those areas with a shepards fill (though that will not be accurate as it averages all pixels but uses a distance squared measure, so the closest are weighted higher).
Post Reply