Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good algorithm or library for cropping images to avoid whitespace or empty areas?

I have a whole bunch of images of illustrations that I would like to crop to a smaller preview size.

The problem is that I want to crop them to show an "interesting" part of the illustration (ie avoid areas of whitespace).

The images typically have a flat color or a subtle gradient for the background. They are mostly vector style artwork with fairly distinct shapes.

Here are some examples: link ;-)

I've been thinking about using some sort of image feature detection algorithm with a sliding window to find the area with the greatest number of features.

I'm implementing this in PHP, but I don't mind implementing it myself if there isn't a library or extension available.

Ideas?

like image 623
user139107 Avatar asked Mar 01 '10 00:03

user139107


4 Answers

ImageMagick has a trim operation. It's available as a library but I don't know how hard it is to use from PHP. There are some PHP interfaces.

like image 122
lhf Avatar answered Sep 29 '22 21:09

lhf


OK, so here's what I would've done, after looking at the examples:

Sum all rows and all columns of each image. You'll get two arrays, both looking like this:

      /-----\  /--\
    _/       --    |
___-                \_________

By looking at these arrays for a few images, find a suitable threshold (probably something just above zero). Then the leftmost and the rightmost crossing of this threshold is where you have to crop. I hope I've managed to make it clear enough, if not -- ask!

like image 42
AVB Avatar answered Sep 29 '22 19:09

AVB


Here's a fairly simple approach using an edge-detection filter, and then cropping around the center-of-edginess of the image to generate a thumbnail. It works pretty well on most images, but not if there are more than one subject. I'm open to suggestions on other ways of identifying the "interesting" points in a source image.

like image 24
Jue Avatar answered Sep 29 '22 19:09

Jue


Well, you might want to consider just using an edge detection algorithm. Pick the area with the largest number of edges. Give higher weight to edges that are not blurry (as they may be from the background).

like image 26
Brian Avatar answered Sep 29 '22 21:09

Brian