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.
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.
[ 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.
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...
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With