1

I have binary image (only 2 color, black and white), Binary Image

I want to create histogram of the image. I have tried with these code:

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('bin_003.png')
color = ('b','g','r')
for i,col in enumerate(color):
    histr = cv2.calcHist([img],[i],None,[256],[0,256])
    plt.plot(histr,color = col)
    plt.xlim([0,256])
plt.show()

But the code shows Histogram instead of showing only 0 and 1 at the x axis.

2
  • 1
    White is (255,255,255) so I don't know why you're surprised. Commented Jun 7, 2018 at 16:13
  • Actually I'm not surprised, i mean, i want to make the axis become only "0" and "1". For example "0" for 0 and "1" for 255 @MarkRansom Commented Jun 7, 2018 at 16:31

1 Answer 1

4

Since you have a black and white image here, it should only have a single channel. You wouldn't need RGB channels. You may create a single histogram by using plt.hist.

from matplotlib import pyplot as plt

img = plt.imread('https://i.sstatic.net/y19dr.png')

plt.hist(img.flatten(), bins=[-.5,.5,1.5], ec="k")
plt.xticks((0,1))
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.