Reputation: 2679
I want to see the histogram of an image using the built in function imhist()
but instead of giving me the expected histogram I am presented with a straight white line.
This is the test image which I am using to create a histogram: http://tenettech.com/content/images/thumbs/0002817_basys2_spartan_3e_250k_gates_fpga_board_600.jpeg
This is the code I am using to get my histogram:
img = imread('test.jpg');
gray = rgb2gray(img);
hist = imhist(gray);
imshow(hist);
This is what I get from imshow(hist);
https://i.sstatic.net/vqnlb.jpg
Its difficult to see properly but with a gray background it is just a white line with the black spot at top.
I was expecting a result like this: http://nf.nci.org.au/facilities/software/Matlab/techdoc/ref/graphiac.gif
Upvotes: 0
Views: 2110
Reputation: 12689
Just directly use:
img = imread('test.jpg');
gray = rgb2gray(img);
figure,imhist(gray)
figure, bar(imhist(img))
figure,bar(imhist(img,50))
Upvotes: 1
Reputation: 5014
The output of imhist is a vector of counts, not an image (i.e., displaying this 1D vector of numbers as an image through imshow
will produce the "black-dots line").
Either use plot
(or bar
) to display the histogram OR use imhist
without any output arguments to get a (default) histogram plot.
nBins = 100; %
[counts, x] = imhist(gray, nBins);
figure; bar(x, counts);
figure; imhist(gray, nBins)
Upvotes: 2