Page 1 of 1

Trim non-uniform backgrounds

Posted: 2015-07-10T01:43:35-07:00
by samejar
I have a number of product images that I need to process to convert from landscape/portrait into square images. The approach I have taken is to:

1. Trim
2. Add border
3. Pad to square

This is being done via im4java on OSX with ImageMagick 6.9.1-1.

I have some working commands for this process but a number of images are not being trimmed correctly due to artefacts in the image or non-uniform background. The trim command is very basic:

convert ./input.jpg -trim cropped.jpg

I think I need an additional step before the trim to clean up the background of the images but have no idea of the best way to do this. Samples of the images are below:

Image 1 - https://www.dropbox.com/s/5u9adlgxij05j ... 1.jpg?dl=0
Image 1 (adjusted to show issues) - https://www.dropbox.com/s/nuxzffapl58a8 ... d.jpg?dl=0
Image 2 - https://www.dropbox.com/s/94pvlwn8lkgvq ... 2.jpg?dl=0
Image 2 (adjusted to show issues) - https://www.dropbox.com/s/0j05upma44x9x ... d.jpg?dl=0

These images have all been processed quite heavily and I am looking for a way to ensure that they can be trimmed correctly.

As a bonus the 3rd example is on that appears to have got through without the processing (Background not set to white). I'm ok if I need to use a service like http://uk.pixelz.com/pricing/ for the few images that are like this but would be interested in whether ImageMagick can handle it.

Image 3 - https://www.dropbox.com/s/z519ingxhoomx ... 3.jpg?dl=0

For this one the entire background needs to be set to white so that is can be trimmed and re shaped withe a uniform white background.

If you think you can help please PM and let me know if you need any more info.

Re: Trim non-uniform backgrounds

Posted: 2015-07-10T02:17:54-07:00
by fmw42
I have moved your topic from the Consulting forum, since that is for Paid Consulting.

___________


For your second image1, you will not be able to automatically remove the region in the lower left corner. Most of the others should process reasonably using

Code: Select all

convert image -fuzz XX% -trim +repage result
where XX% is the percent close to your background color that you want to include as background.


For the image with the few specks, the second image2, without the red circles, you can likely clean it up using morphology before the above processing

Code: Select all

convert image -morphology close octagon:1 -fuzz XX% -trim +repage result
You may have to increase the octagon:1 to octagon:2 or larger if the specks are larger.

If you need to change the background to pure white, then you can modify the command by

Code: Select all

convert image -morphology close octagon:1 -fuzz XX% -trim +repage -fuzz YY% -fill white -opaque white result
where YY% is an amount that represents the difference in percent between your background and pure white.


see
http://www.imagemagick.org/Usage/crop/#trim_fuzz
http://www.imagemagick.org/Usage/morphology/#close
http://www.imagemagick.org/Usage/color_basics/#replace

Re: Trim non-uniform backgrounds

Posted: 2015-07-10T04:29:35-07:00
by samejar
Magic! Adding the fuzz & repage to the trim command did the trick.

The morphology works for setting the background to white but seems to smooth graduation of the shadow somewhat. It looks pretty similar to the processed images so I image this is similar to what was done originally. Would using a more complex mask approach e.g. http://www.imagemagick.org/Usage/scripts/bg_removal be more likely to leave the shadow intact or is it not realistic to expect to maintain the shadow?

Thanks

Re: Trim non-uniform backgrounds

Posted: 2015-07-10T09:08:33-07:00
by fmw42
Pretty much any filtering method for removing noise, will modify the rest of the data in one manner or another. Somethings may not look changed much, but others may be more obvious.

Re: Trim non-uniform backgrounds

Posted: 2015-07-10T09:24:26-07:00
by fmw42
You can mask out areas that you do not want processed.

You can use -mask. See http://www.imagemagick.org/Usage/masking/#write_mask

Or alternately, you can process the full image. Then use the mask image to composite the two images such that the region you do not want changed comes from the original image and the region you want changed comes from the processed image. See http://www.imagemagick.org/Usage/compose/#compose