Limiting or Minimizing Histogram Output

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
dmfelder
Posts: 9
Joined: 2014-03-13T12:04:17-07:00
Authentication code: 6789

Limiting or Minimizing Histogram Output

Post by dmfelder »

I need to determine the count of "approximately-white" pixels in my art. I can write histogram output, import it & analyze, but the file is so large.

I use:

Code: Select all

convert input.jpg -format %c histogram:info:out.txt
and I get a very large list like this:
  • 396: (129,129,130,255) #818182 rgba(129,129,130,1)
    573: (129,130,130,255) #818282 rgba(129,130,130,1)
    294: (130,129,129,255) #828181 rgba(130,129,129,1)
    16: (130,130,129,255) #828281 rgba(130,130,129,1)
    2170: (130,130,130,255) #828282 grey51
    707: (130,130,131,255) #828283 rgba(130,130,131,1)
    1032: (130,131,131,255) #828383 rgba(130,131,131,1)
    1: (130,202,160,255) #82CAA0 rgba(130,202,160,1)
    203: (131,130,130,255) #838282 rgba(131,130,130,1)
    4: (131,131,130,255) #838382 rgba(131,131,130,1)
It's hugely redundant data (you don't need the second set of RGB values). If I could NOT print the second set of values, my file would be 1/2 the size. In fact, I only need the RGB OR the HEX (not both). And to top it off, I really only want to see "near-white" pixel quantities (north of 253, 253, 253).

I have tried preceding the code with the command:

Code: Select all

-define histogram:unique-colors=false
but I still don't get what I want.

All I need is the count and EITHER the RGB or the HEX.

Any other ideas?

Thanks so much!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Limiting or Minimizing Histogram Output

Post by snibgo »

For starters, I would pre-process the image to turn black all the pixels that were red<253 OR green<253 OR blue<253, or whatever conditions you want. This may eliminate most lines on the listing.

If you just want a count of the number of pixels that are approximately white, then turn them all exactly white and the others exactly black. Then your histogram has only two colours. You don't even need a histogram: "identify -verbose" would give the answer.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Limiting or Minimizing Histogram Output

Post by fmw42 »

try

Code: Select all

convert input.jpg -threshold XX% -format %c histogram:info:out.txt
where XX% is very large, as close to 100% as you want your tolerance for white.
dmfelder
Posts: 9
Joined: 2014-03-13T12:04:17-07:00
Authentication code: 6789

Re: Limiting or Minimizing Histogram Output

Post by dmfelder »

Thanks to both of you. Basically, Snibgo's idea is realized in Fred's threshold command.

This is a very simple and effective solution. I think the threshold command will create a maximum of 3 lines of output:
  • Closer to Black: 63: ( 0, 0, 0,255) #000000 black
    Closer to White: 25: (255,255,255,255) #FFFFFF white
    Transparent: 12: (255,255,255, 0) #FFFFFF00 rgba(255,255,255,0)
Given that my file has now reduced to a max of 3 lines, I have a follow-up question...

Is there a way to capture the variables instead of writing the output to a file? I tried using Identify, but I didn't get any result. Ideally, I'd love to avoid writing the file in the first place. If there is anyway to populate a variable with the data that is being written, I could avoid the complication of write/read.

Thanks

P.S. For reference, I use commands like this to obtain image data:

Code: Select all

imagedata = ObjMi.Identify("-units", "PixelsPerInch", "-format", "%x %y %w %h", myimagename)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Limiting or Minimizing Histogram Output

Post by fmw42 »

On Unix, this will put the data from the histogram into a variable. From there you can use sed and cut to get the parts you want as new variables.

Code: Select all

data=`convert rose: -threshold 95% -format "%c" histogram:info:`
echo "$data"
      3080: (  0,  0,  0) #000000 black
       140: (255,255,255) #FFFFFF white
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Limiting or Minimizing Histogram Output

Post by snibgo »

dmfelder wrote:P.S. For reference, I use commands like this to obtain image data:
Fine, but you don't say what language that is. I probably wouldn't know the answer for your language, even if you did say. You should look at documentation for that language.
snibgo's IM pages: im.snibgo.com
Post Reply