Histogram of a (possible) binary image

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
tmelorc
Posts: 30
Joined: 2016-12-01T06:07:55-07:00
Authentication code: 1151

Histogram of a (possible) binary image

Post by tmelorc »

Version: ImageMagick 7.0.4-2 Q16 x86_64 2017-01-02
Consider the file input.jpg. I applied some tools to obtain a kind of binary image.

Code: Select all

convert input.jpg -threshold 62% -morphology smooth Octagon bin-input.jpg
convert bin-input.jpg -define connected-components:area-threshold=60000 -define connected-components:keep=0 -define connected-components:verbose=false -define connected-components:mean-color=true -connected-components 4 id0-input.jpg 
The output is id0-input.jpg.

1. I have no idea why that black noise (small black portion) - this is not so important.

2. Since I'm going to do some computations using the black pixels, I decided to count how many black/white pixels there are. But the histogram shows not only B/W pixels, but also other gray levels.

Code: Select all

convert id0-input.jpg -define histogram:unique-colors=true -format %c histogram:info:-
   1006292: (  0,  0,  0) #000000 gray(0)
      6350: (  1,  1,  1) #010101 gray(1)
      1784: (  2,  2,  2) #020202 gray(2)
       287: (  3,  3,  3) #030303 gray(3)
        40: (  4,  4,  4) #040404 gray(4)
         2: (250,250,250) #FAFAFA gray(250)
        37: (251,251,251) #FBFBFB gray(251)
       351: (252,252,252) #FCFCFC gray(252)
      2061: (253,253,253) #FDFDFD gray(253)
      7682: (254,254,254) #FEFEFE gray(254)
   2953114: (255,255,255) #FFFFFF gray(255)
3. The result above was a surprise to me since from the IM guide one has:
-threshold value{%}

Apply simultaneous black/white threshold to the image.

Any pixel values (more specifically, those channels set using ‑channel) that exceed the specified threshold are reassigned the maximum channel value, while all other values are assigned the minimum.
So I supposed that id0-input.jpg would contain only gray(0) and gray(255) pixels.

4. What is happening in this case? How to make id0-input.jpg with only B/W pixels?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Histogram of a (possible) binary image

Post by snibgo »

You save the images as JPEG. But that is a lossy format. That means pixel values (colours) are changed. If you save as JPEG, the file will contains colours that you didn't put there.

JPEG is a useful format for showing photos on the web. It should not be used for image processing.
snibgo's IM pages: im.snibgo.com
tmelorc
Posts: 30
Joined: 2016-12-01T06:07:55-07:00
Authentication code: 1151

Re: Histogram of a (possible) binary image

Post by tmelorc »

snibgo wrote: 2017-03-18T12:58:59-07:00 You save the images as JPEG. But that is a lossy format. That means pixel values (colours) are changed. If you save as JPEG, the file will contains colours that you didn't put there.

JPEG is a useful format for showing photos on the web. It should not be used for image processing.
So, what do you suggest? To convert to png first? I'm not able to obtain another input image in other format.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Histogram of a (possible) binary image

Post by snibgo »

tmelorc wrote:To convert to png first?
You save an image to JPG. This is lossy. You read that, process it, and save it again to JPG, which is lossy again. Then you read that result and find it doesn't contain the colours you expect.

I suggest you never write to JPG unless you know you really want to. For solid colour, PNG is smaller and might be faster. Most importantly, it is lossless.
snibgo's IM pages: im.snibgo.com
tmelorc
Posts: 30
Joined: 2016-12-01T06:07:55-07:00
Authentication code: 1151

Re: Histogram of a (possible) binary image

Post by tmelorc »

OK. I converted jpg to png first. Then, I tried exactly the same codes as above, using png everytime. I got the following histogram:

Code: Select all

   1015144: (    0,    0,    0) #000000000000 gray(0)
   2962856: (65535,65535,65535) #FFFFFFFFFFFF gray(255)
What is the meaning of 65535 instead of 255?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Histogram of a (possible) binary image

Post by fmw42 »

From the image uploaded, I get

Code: Select all

convert ZpEm88c.jpg -define histogram:unique-colors=true -format %c histogram:info:
   1006292: (  0,  0,  0) #000000 gray(0)
      6350: (  1,  1,  1) #010101 gray(1)
      1784: (  2,  2,  2) #020202 gray(2)
       287: (  3,  3,  3) #030303 gray(3)
        40: (  4,  4,  4) #040404 gray(4)
         2: (250,250,250) #FAFAFA gray(250)
        37: (251,251,251) #FBFBFB gray(251)
       351: (252,252,252) #FCFCFC gray(252)
      2061: (253,253,253) #FDFDFD gray(253)
      7681: (254,254,254) #FEFEFE gray(254)
   2953115: (255,255,255) #FFFFFF gray(255)
So I am not sure why you get values like 65535. But you have processed your 8-bit per channel input on your IM, which is compiled (by default) as Q16. That means that processed pixels may have graylevel (channel) values are in the range 0 to 655535. So those are likely the numbers for the triplet values in parenthesis. Try adding -depth 8 to your command right after you read the processed image before the histogram and you should get values in the range 0 to 255.

Code: Select all

convert ZpEm88c.jpg -depth 8 -define histogram:unique-colors=true -format %c histogram:info:
tmelorc
Posts: 30
Joined: 2016-12-01T06:07:55-07:00
Authentication code: 1151

Re: Histogram of a (possible) binary image

Post by tmelorc »

fmw42 wrote: 2017-03-18T16:13:45-07:00

Code: Select all

convert ZpEm88c.jpg -depth 8 -define histogram:unique-colors=true -format %c histogram:info:
Oh, thanks. Now using ong and -depth 8 I got exactly

Code: Select all

(255,255,255)
Post Reply