Remove photo from collage if it is plain/blank e.g sky

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
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Remove photo from collage if it is plain/blank e.g sky

Post by Bonzo »

Going back to some of my old code after reading a magazine artical I need to remove photos that are of not importance e.g. sky.

In the artical there was a bridge made up of photos but only the photos of the bridge - high energy photos ? - photos of the sky and water were not used.

This is not a great example but I had it to hand on my PC. I want all the photos except the top right photo which I want converted to transparent so it does not break the code/photo collage.

Image

Is there an easy way to determin if a photo is mostly one colour or boring!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove photo from collage if it is plain/blank e.g sky

Post by fmw42 »

if the photos are separate (i.e. before you collage them), then just check the standard deviation. If less than some critical amount, then it is flat (constant color).

convert image -format "%[fx:standard_deviation]" info:

range 0 to 1

or

convert image -format "%[standard_deviation]" info:

range 0 to quantumrange
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Remove photo from collage if it is plain/blank e.g sky

Post by Bonzo »

Output from your methods Fred contains a couple of strange results but works:
Method 1:
Image

Method 2:
Image

Method one works better; I mentioned energy in my original post as that is something used in Liquid rescale from memory but I have no idea how it would be found.

I will have to find a better image to try it on.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Remove photo from collage if it is plain/blank e.g sky

Post by Bonzo »

Results are very dependent on input image so not realy a practical application.
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove photo from collage if it is plain/blank e.g sky

Post by fmw42 »

It is strange that methods 1 and 2 would differ as they should be exactly scalable by quantumrange. So it must be that you chose a different threshold between the two measure (allowing for scaling by quantum range).

Yes, the threshold you choose probably should vary by data set as each set of images will have different characteristics. I know of no global measure that will work with all image sets. It really depends on the content of the image sets and how much you are willing to tolerate.

You could try my script, entropy, as a measure, if you can run this from PHP and not from the command line as I know you are on Windows. If that works, then perhaps it should be added to the IM stats information along with mean, min, max and std, etc.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Remove photo from collage if it is plain/blank e.g sky

Post by anthony »

More than likely it is a result of exactly what standard deviation is being used. Specifically if it is a specific channel, or all the channels. Perhaps a standard deviation of just a grayscale version of the image will work better for determining how variable the intensity is.

Also you may like to run the image through a edge detector, before doing a standard deviation. That will make any image with an edge much more important than those with only gradual changes, something that standard deviation may miss.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove photo from collage if it is plain/blank e.g sky

Post by fmw42 »

Anthony's idea of edge detection makes a lot of sense. One could also edge detect (and threshold or autolevel) and then get the mean rather than std.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Remove photo from collage if it is plain/blank e.g sky

Post by anthony »

fmw42 wrote:Anthony's idea of edge detection makes a lot of sense. One could also edge detect (and threshold or autolevel) and then get the mean rather than std.
Which will get you a percentage of edges, though that would be dependant on image size.

Essentually what you are asking is a sort of 'energy level' contained by the image.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove photo from collage if it is plain/blank e.g sky

Post by fmw42 »

Essentually what you are asking is a sort of 'energy level' contained by the image.
try my entropy script
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Remove photo from collage if it is plain/blank e.g sky

Post by Bonzo »

Essentually what you are asking is a sort of 'energy level' contained by the image.
I did mention this earlier as it is the method Liquid rescale?

For the two outputs showing the values I used the examples as Fred posted:

Code: Select all

convert $filename -format "%[standard_deviation]" info:
convert $filename -format "%[fx:standard_deviation]" info:
It is strange that some photo segments I expected to be removed were not but others were :?

I will give your entropy script a try over the weekend.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove photo from collage if it is plain/blank e.g sky

Post by fmw42 »

convert $filename -format "%[standard_deviation]" info:
convert $filename -format "%[fx:standard_deviation]" info:
The former will produce std values in the range 0 to QuantumRange

The latter will produce normalized std values in the range 0 to 1

Be careful that you scale them both to similar ranges for your threshold. I usually use percent in range 0 to 100 for the std and use percents for the thresholding for the test of acceptable or not.

Note that std is like a measure of energy. It is an edge detection (if applied locally on an image) so the larger the global value, the more edges or more energy there is in the image.


Another thought/approach might be to resize the image to half or a quarter size using both -resize and -liquid-rescale. Then do a compare on the two images. The more different the more the liquid-rescale has removed low energy parts.
Post Reply