Page 1 of 1

bg_removal script piles parts of image on top of each other

Posted: 2013-11-24T17:06:14-07:00
by stampguy
Version: ImageMagick 6.5.7-8 2012-08-17 Q16
Platform: Ubuntu Linux
API: shell command line

I make very large quantities of images of postage stamps for illustration purposes. The final result images of the postage stamps need to be a black background, like this:
http://jsa.viewimage.net/jsa/web/Lists/ ... 01_r_m.jpg
Sometimes the image shows only one stamp, sometimes several stamps.
*Often* the stamps are canceled/postmarked with a black postmark/cancellation that extends over the edge, like this:
http://jsa.viewimage.net/jsa/web/Lists/ ... 74_r_m.jpg
When the stamp images are scanned, there are *many* stamps in one scan. They are later divided up as appropriate.

Up until this time, the scanning has been done on a "black" (never is completely black) background. The background is then color-filled solid black in Gimp to get the pure black background.
However, when the black postmark/cancellation goes to the edge of the stamp, the black-filling "eats into" to stamp design, which is not desirable. No amount of threshold setting gets around that classic problem. Thus those situations have to be manually-selected or de-selected & black-filled in Gimp to make sure the black-fill does not "eat into" the stamp design.

For this reason, I have been experimenting with "chroma key" or "green screen" methods, using ImageMagick convert.

I found extensive and useful discussion of these methods starting here: http://www.imagemagick.org/Usage/masking/#bg_remove

I have read carefully and experimented. The results so far are extremely poor, but the biggest problem is that the *several* stamps in the image are become piled on top of each other.

I first tried the most simple methods (rather than the bg_removal script shown here: http://www.imagemagick.org/Usage/scripts/bg_removal )
However, both the simple scripts and the bg_removal script have approximately the same result ... everything piled on top of each other.
I can get a nice black background with the bg_removal script, but the stamps are all in a pile.

The problem seems to be related to the -compose and/or -composite. I have read and re-read the web pages about those, but I have not been able to understand what is going wrong.

IMAGE STARTS WITH:
*** I am posting a JPEG version for web viewing purposes.
http://jsa.viewimage.net/temp/im/source.jpg
*** The original TIFF version is here:
http://jsa.viewimage.net/temp/im/source.tif (6 MB)

IMAGE FINAL RESULT IS (WRONG):
http://jsa.viewimage.net/temp/im/result_threshold.jpg

The result *should* be just like the starting image, but with a black background.

Right now I am not concerned with the horrible color/pattern of the result -- just the fact that the stamps are all become piled on top of each other.

I suspect that this method would not have this problem if the source.tif were a single stamp, instead of a group of stamps, however, the whole point of using this type of process is to be able to process a large group of stamps (and then cut them apart and rotate them at a later stage).

The simple scripts, taken from http://www.imagemagick.org/Usage/masking/#difference are as follows:
I have only changed the file names.

####################

# create difference.tif which is only used for the subsequent step

convert source.tif \( +clone -fx 'p{0,0}' \) \
-compose Difference -composite \
-modulate 100,0 -alpha off difference.tif

# create threshold_mask.tif

convert difference.tif -threshold 15% threshold_mask.tif

# create result.tif using threshold_mask.tif

convert source.tif threshold_mask.tif \
-alpha off -compose CopyOpacity -composite \
result_threshold.tif

#####################

Alternative script-based methods are also very welcome, but the examples in http://www.imagemagick.org/Usage/masking/#difference were so good that I was hoping that method could work.

Thanks for any help on this.

Re: bg_removal script piles parts of image on top of each ot

Posted: 2013-11-24T18:33:42-07:00
by snibgo
When I adapt your script for Windows 7 and run it, using IM v6.8.7-0, I get the stamps in the original locations against a transparent background. (The stamps also have some transparency. The threshold_mask.tif need adjusting.)

Your http://jsa.viewimage.net/temp/im/result_threshold.jpg looks corrupt.

What version of IM are you using?

EDIT: Sorry, I've just seen, 6.5.7-8. That is very very old. I suggest you upgrade.

Re: bg_removal script piles parts of image on top of each ot

Posted: 2013-11-26T06:18:52-07:00
by stampguy
I have now updated to what seems to be the most recent version that can be installed on our Ubuntu Linux 10.0.4 Lucid (which is old; I don't have a newer OS available).

I am now running: Version: ImageMagick 6.6.2-6 2011-04-02 Q16

There was no "immediate apparent" change in the result.... but the comment that the resulting image seemed corrupted caused me to wonder about how I was viewing the result. I was viewing the result in Gimp -- that is my default image editor and it has never presented a problem. However, when I viewed the image in some other programs/viewers, I got drastically different results. I apologize for not trying other viewers earlier.

Gimp: Upon opening, Gimp pops up a "Raw Photo Loader" dialog to select various options. I had never seen that before, but did not think much about it. None of the different loading options change the "piled up" aspect, though they do change the background color.

Eye of GNOME 2.22.3 Image Viewer: Opens the image with the stamps where they are supposed to be, but with a transparent (checkerboard) background.

Evince Document Viewer 2.22.2: Opens the image with the stamps where they are supposed to be, with a black background, completely correct.

So, now my question is: What/Why/How is my image being "corrupted" (and/or not completely put back together) by the scripts... and how much of this is a Gimp problem vs an IM/convert problem?

Thanks.

Re: bg_removal script piles parts of image on top of each ot

Posted: 2013-11-26T11:55:32-07:00
by fmw42
When you view your result using IM display does it look correct?

Re: bg_removal script piles parts of image on top of each ot

Posted: 2013-11-26T12:31:42-07:00
by stampguy
I did not know about IM's dipslay. Thanks!

The answer is yes & no...

$ display result.tif

Shows the stamps positioned where they are supposed to be, but the background is checkerboard (which I am assuming is transparent). The background is supposed to be black and as mentioned above one viewer does show the background as black.

However when the "display" window opens, there is also a *tiny* window titled "Pan". In that tiny Pan window, the stamps are correctly positioned and the background IS black!

Oddly, (clicking in the display window) when I use the Commands (menu) window and go to Miscellany, Image Info, in the Image section of Image Info, Background color is stated as white!

The Image Info also shows that the result.tif image has an Alpha channel. I take that to mean that the image has not been flattened. I would have expected the script process to flatten everything back to the state that source.tif started. Is that part of my problem (both with the background not being black in all viewers, and with Gimp showing the stamps piled on top of each other)?

(BTW, After reading more documentation I realize that for purposes of examples and discussion, I should not have used the word "source" as a filename because "source" really means something else in the context of IM's compose / composite. My "source" is what documentation would call "background".)

Re: bg_removal script piles parts of image on top of each ot

Posted: 2013-11-26T13:07:01-07:00
by fmw42
when you use -compose copy_opacity, you are creating an alpha channel. Thus you should get a transparent background, unless the output is jpg, which does not support transparency. Some viewers cannot display transparency, so you just get the underlying color of black below the transparency.

I think things are working as expected and you have viewer issues.

If you do not want the transparency then you can add -background black -flatten or just -alpha remove.

Re: bg_removal script piles parts of image on top of each ot

Posted: 2013-11-26T13:54:56-07:00
by stampguy
Thanks Fred,

Per your suggestion I replaced (in the previously posted bg_removal.s script)

-background $background -alpha shape \

(The script was from the IM website; I had not modified it prior to this. Earlier in the script the background variable was being set to black. I have no idea what "-alpha shape" did.)

with

-background black -flatten \

and now everything seems to work including viewing in Gimp. The stamps are no longer piled on top of one another and the general background is black. Wonderful!

Now I have to figure out how to reduce the pixilization of the edges (between the stamps and the background) and reduce the "green shadow" (artifact of using a flatbed scanner) that is still remaining. But this is a great step forward.

Thanks.