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

时间:2022-06-04 21:19:55

I read an image in matlab using

我使用matlab读取图像

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.

在运行size(input)之后,我看到我的输入图像大小为300x200x3。我知道第三个维度是颜色通道,但是有没有办法显示这个的直方图?谢谢。

5 个解决方案

#1


26  

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).

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

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...

同时显示3个通道…

#2


13  

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

#3


4  

An histogarm plot will have number of pixels for the intensity levels. Yours is an rgb image. So you first need to convert it to an intensity image.

histogarm图形将具有强度级别的像素数。你的是rgb图像。你首先需要把它转换成一个强度图像。

The code here will be:

这里的代码将是:

input = imread ('sample.jpeg');

input=rgb2gray(input);

imhist(input);

imshow(input);

You will be able to get the histogram of the image.

你可以得到图像的直方图。

#4


3  

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

Remember to include mat2gray(); because it converts the matrix A to the intensity image grayImg. The returned matrix grayImg contains values in the range 0.0 (black) to 1.0 (full intensity or white).

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

#5


0  

Histogram is useful to analyze pixel distribution in an image. Histogram plots number of pixel in an image with respect to intensity value.

直方图对于分析图像中的像素分布很有用。直方图表示图像中相对于强度值的像素个数。

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

#1


26  

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).

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

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...

同时显示3个通道…

#2


13  

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

#3


4  

An histogarm plot will have number of pixels for the intensity levels. Yours is an rgb image. So you first need to convert it to an intensity image.

histogarm图形将具有强度级别的像素数。你的是rgb图像。你首先需要把它转换成一个强度图像。

The code here will be:

这里的代码将是:

input = imread ('sample.jpeg');

input=rgb2gray(input);

imhist(input);

imshow(input);

You will be able to get the histogram of the image.

你可以得到图像的直方图。

#4


3  

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

Remember to include mat2gray(); because it converts the matrix A to the intensity image grayImg. The returned matrix grayImg contains values in the range 0.0 (black) to 1.0 (full intensity or white).

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

#5


0  

Histogram is useful to analyze pixel distribution in an image. Histogram plots number of pixel in an image with respect to intensity value.

直方图对于分析图像中的像素分布很有用。直方图表示图像中相对于强度值的像素个数。

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