How do you ignore dust/specks in background when cropping an image after a background color replace?

247 Views Asked by At

I use Irfanview at the moment, for batch replacing the background and cropping the resulting images. Then I resize and canvas them, to get nice standards to use on my website.

I have been playing with the tolerances to replace the near-white background to clear white. About half of them process without problems, but when there is a small speck in the background, the cropping tool reacts as if it a part of the main image, which it is not. If I increase the tolerance even more, it eats at the main image, and the result is not good enough to publish.

Is there a workaround to maybe first delete such specks, before I start replacing the background?

P.S.: Now I'm doing all of this offline. If necessary, I can move this online, and at that point I would prefer php, since it's the language I'm most familiar with

Source image (4752x3168, 3.40 MB): source image http://veilinghuiseeckhout.be/test/img/TEST00001.JPG

Result now (800x800, 136kB): resulting image http://veilinghuiseeckhout.be/test/img/TEST00001res.JPG

Desired result (800x800, 394kB): desired image http://veilinghuiseeckhout.be/test/img/TEST00001want.jpg

1

There are 1 best solutions below

0
On

I don't know Irfanview as I tend to use ImageMagick which is free and available for Windows, and OSX, Linux etc. It can also be scripted easily from the command line.

As you have some sizeable dust specks, my approach would be to blur the image quite heavily and threshold it to pure black and white before cropping it in order to get the crop coordinates and then go ahead and crop the unblurred original. It looks like this at the Command prompt:

convert original.jpg -blur 0x8 -threshold 80% -format "%@" info:
1185x1157+1615+947

That says... "take the original and blur it heavily, then threshold everything below 80% white to pure black and everything above 80% white to pure white. Then pretend I was going to crop and tell me where you would crop please."

The answer means ImageMagick would now crop a rectangle that measures 1185 pixels wide by 1157 pixels tall starting 1615 pixels from the left margin and 947 pixels from the top edge.

If I now draw that on the original image in red:

convert original.jpg -stroke red -fill none -draw "rectangle 1615,947 2799,2103" result.jpg

enter image description here

Or, I can actually crop it out too:

convert original.jpg -crop 1185x1157+1615+947 result.jpg

enter image description here

My Windows skills are limited, but in a BAT file, the cropping would look like this:

FOR /F %%G IN ('convert original.jpg -blur 0x8 -threshold 80%% -format "%%@" info:') DO SET GEOM=%%G
ECHO %GEOM%
convert original.jpg -crop %GEOM% result.jpg

Another option may be to reduce the height and width by a factor of say 10 or 20 to make the specks disappear, get the cropping rectangle then scale that back up by the same factor and apply to original image.

Another option would be to do a "Connected Component Analysis" with ImageMagick, so let me know if the above answer works for your other images, and we can develop other ideas if not.