Return histogram of image with pixel area 3x3 (4x4, 5x5, etc.)

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
tresorjoe
Posts: 15
Joined: 2018-04-12T02:19:12-07:00
Authentication code: 1152

Return histogram of image with pixel area 3x3 (4x4, 5x5, etc.)

Post by tresorjoe » 2019-12-04T07:27:18-07:00

Hello,
I think I understand how histogram works. It returns the color values of an image (e.g. RGB) and the amount of pixels of the same color value in the whole image. So far so good. Is there a way one could look up e.g. a 3x3 pixels area of the SAME color value and return only information of colors that at least are identical within such a 3x3 pixels area? This is to look for "flatness" of colors in an image.
Example attached:
This is a 9x9 pixel image. With the above approach using 3x3 pixel area the result would return 14x 255,0,0 | 0x 255,255,255 | 14x 0,255,0
If one would use 4x4 pixel area analysis the result would return 6x 255,0,0 | 0x 255,255,255 | 6x 0,255,0
https://www.dropbox.com/s/18xdtqv65xixm ... 1.png?dl=0

snibgo
Posts: 12400
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Return histogram of image with pixel area 3x3 (4x4, 5x5, etc.)

Post by snibgo » 2019-12-04T09:12:05-07:00

I'm not sure what you want.

In a 9x9 pixel image, there are nine 3x3 subimages that don't overlap, or forty-nine 3x3 subimages that do overlap.

However, in the red area, there are fourteen 3x3 subimages 3x3 subimages that do overlap. Is that what you mean?

A 3x3 subimage that is all one colour has a standard deviation of zero. The operation "-statistic standard-deviation 3x3" finds the SD for 3x3 subimages centred on all input pixels. You don't want the edge cases, so "-shave 1x1" will remove these, to give just the forty-nine cases.
snibgo's IM pages: im.snibgo.com

snibgo
Posts: 12400
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Return histogram of image with pixel area 3x3 (4x4, 5x5, etc.)

Post by snibgo » 2019-12-04T11:07:30-07:00

Another method: "-scale" the image down, then back up. Pixels that have changed were not at the centre of solid-colour regions. Pixels that remain the same were at the centre of solid-colour regions.

Related techniques can be used, eg windowed means.
snibgo's IM pages: im.snibgo.com

tresorjoe
Posts: 15
Joined: 2018-04-12T02:19:12-07:00
Authentication code: 1152

Re: Return histogram of image with pixel area 3x3 (4x4, 5x5, etc.)

Post by tresorjoe » 2019-12-05T01:45:03-07:00

Hello,

thanks for your answers. I have tried -statistic with standard-deviation (or better: RMS). Unfortunately with larger files (I tried one with 5000x5000px) the computation is extremely slow and the output text file is understandably extremely large - I got 1.3GB text file which will be impossible to process afterwards. What options do I have to directly create a "new" histogram with this information instead?

PS: this is the command that I used

Code: Select all

magick -verbose 3x3.tif -shave 1x1 -statistic RMS 3x3 3x3_out.txt
PPS: the -scale approach is too destructive or too prone to rounding factors for the purpose I have in mind. I might miss valuable information that I can't recover anymore

snibgo
Posts: 12400
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Return histogram of image with pixel area 3x3 (4x4, 5x5, etc.)

Post by snibgo » 2019-12-05T08:25:10-07:00

I don't see how RMS helps.

The initial processing can be made massively faster using Integral images. I wouldn't process "txt:" of large images, as there is so much data and processing would take so long.

Can you link to a sample input file?
snibgo's IM pages: im.snibgo.com

Post Reply