【问题标题】:How to show histogram of RGB image in Matlab?如何在 Matlab 中显示 RGB 图像的直方图?
【发布时间】:2013-01-18 22:16:21
【问题描述】:

我使用 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。我知道第三维是用于颜色通道的,但是有什么方法可以显示直方图吗?谢谢。

【问题讨论】:

    标签: image matlab histogram


    【解决方案1】:

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

    您也可以这样做:

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

    同时显示 3 个频道...

    【讨论】:

      【解决方案2】:

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

      这里的代码是:

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

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

      【讨论】:

        【解决方案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');
        

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

        【讨论】:

          【解决方案4】:

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

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

          【讨论】:

            【解决方案5】:

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

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

            【讨论】:

              猜你喜欢
              • 2013-08-08
              • 2018-04-04
              • 2018-02-05
              • 2016-07-22
              • 1970-01-01
              • 2020-09-28
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多