如何在Matlab中显示RGB图像的直方图



我在matlab中使用读取图像

input = imread ('sample.jpeg');

然后我做

imhist(input);

它给出了以下错误:

??? 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{:});

在运行size(input)之后,我看到我的输入图像的大小是300x200x3。我知道第三个维度是用于颜色通道的,但有什么方法可以显示直方图吗?谢谢

imhist显示灰度二进制图像的直方图。在图像上使用rgb2gray,或使用imhist(input(:,:,1))一次查看一个通道(本例中为红色)。

或者你可以这样做:

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

同时显示3个频道。。。

我希望在一个图中绘制红色、绿色和蓝色的直方图:

%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');

直方图将具有强度级别的像素数。你的是rgb图像。因此,您首先需要将其转换为强度图像。

这里的代码是:

input = imread ('sample.jpeg');
input=rgb2gray(input);
imhist(input);
imshow(input);

你将能够得到图像的直方图。

img1=imread('image.jpg');
img1=rgb2gray(img1);
subplot(2,2,1);
imshow(img1);
title('original image');
grayImg=mat2gray(img1);
subplot(2,2,2);
imhist(grayImg);
title('original histogram');

记住包括mat2gray();因为它将矩阵A转换为强度图像grayImg。返回的矩阵grayImg包含0.0(黑色)到1.0(全强度或白色)范围内的值。

直方图可用于分析图像中的像素分布。直方图绘制图像中像素数相对于强度值的关系。

img1=imread('image.jpg');
hist(img1);

最新更新