Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show histogram of RGB image in Matlab?

I read an image in matlab using

input = imread ('sample.jpeg');

Then I do

imhist(input);

It gives this error:

??? Error using ==> iptcheckinput
Function IMHIST expected its first input, I or X, to be two-dimensional.

Error in ==> imhist>parse_inputs at 275
iptcheckinput(a, {'double','uint8','logical','uint16','int16','single'}, ...

Error in ==> imhist at 57
[a, n, isScaled, top, map] = parse_inputs(varargin{:});

After running size(input), I see my input image is of size 300x200x3. I know the third dimension is for color channel, but is there any way to show histogram of this? Thanks.

like image 990
E_learner Avatar asked Feb 04 '13 08:02

E_learner


People also ask

How do you find the histogram of a RGB image?

We can create histograms of images with the np. histogram function. We can separate the RGB channels of an image using slicing operations. We can display histograms using the matplotlib pyplot figure() , title() , xlabel() , ylabel() , xlim() , plot() , and show() functions.

How do you display a histogram of an image in Matlab?

[ counts , binLocations ] = imhist( I , n ) specifies the number of bins, n , used to calculate the histogram. [ counts , binLocations ] = imhist( X , cmap ) calculates the histogram for the indexed image X with colormap cmap . The histogram has one bin for each entry in the colormap.


2 Answers

imhist displays a histogram of a grayscale or binary images. Use rgb2gray on the image, or use imhist(input(:,:,1)) to see one of the channel at a time (red in this example).

Alternatively you can do this:

hist(reshape(input,[],3),1:max(input(:))); 
colormap([1 0 0; 0 1 0; 0 0 1]);

to show the 3 channels simultaneously...

like image 133
bla Avatar answered Oct 12 '22 19:10

bla


I pefere to plot the histogram for Red, Green and Blue in one plot:

%Split into RGB Channels
Red = image(:,:,1);
Green = image(:,:,2);
Blue = image(:,:,3);

%Get histValues for each channel
[yRed, x] = imhist(Red);
[yGreen, x] = imhist(Green);
[yBlue, x] = imhist(Blue);

%Plot them together in one plot
plot(x, yRed, 'Red', x, yGreen, 'Green', x, yBlue, 'Blue');
like image 26
Philipp Hofmann Avatar answered Oct 12 '22 18:10

Philipp Hofmann