【问题标题】:implementation sample of a color histogram on an rgb image without using Matlab built-in functions不使用 Matlab 内置函数的 rgb 图像上的颜色直方图的实现示例
【发布时间】:2014-11-29 18:49:38
【问题描述】:

我一直在尝试实现一个名为 histogram(image) 的函数,该函数执行 RGB 图像的颜色直方图。 谁能给我看一个易于分析的样本,以便了解它究竟是如何工作的。

非常感谢您的帮助。

【问题讨论】:

  • 到目前为止你有没有尝试过? “不使用内置函数”是什么意思?
  • 我的意思是不使用imhist()或其他Matlab函数,我需要知道颜色直方图的逻辑,因为我还是Matlab的初学者,我什至不知道如何开始我的功能。
  • 你也可以看看这篇文章。我编写的代码将颜色直方图实现为一维直方图,其中特定颜色元组被转换为单个一维索引。 stackoverflow.com/questions/25830225/…

标签: matlab image-processing histogram rgb


【解决方案1】:

以下是针对您的问题的不同算法:

im = imread('lena.png'); % imshow(im);
histogram(im)

function histogram(im)
    [rowSize, colSize, rgb] = size(im);
    nshades                 = 256;
    hist                    = zeros(rgb, nshades);

    figure,
    RGB   = ['r', 'g', 'b'];
    names = [{'Red Channel'}, {'Green Channel'}, {'Blue Channel'}];
    x     = 0 : 255;

    for colour = 1 : rgb
        for k = 1 : rowSize
            for m = 1 : colSize
                for n = 0 : (nshades - 1)         % 0 - 255
                    if im(k, m, colour) == n
                        hist(colour, n + 1) = hist(colour, n + 1) + 1;
                    end
                end
            end
        end

        subplot(3, 1, colour)
        bar(x, hist(colour, :), RGB(colour)); title(names(colour));
    end
end

【讨论】:

    【解决方案2】:

    这是绘制图像每个颜色通道的直方图的代码。

    I=imread('lena.png');
    r=I(:,:,1);
    g=I(:,:,2);
    b=I(:,:,3);
    
    totalNumofPixel=size(I,1)*size(I,2);
    FrequencyofRedValues=zeros(256,1);
    FrequencyofGreenValues=zeros(256,1);
    FrequencyofBlueValues=zeros(256,1);
    
    for x=0:255
        FrequencyofRedValues(x+1)=size(r(r==x),1);   // number of pixels whoose intensity is x
        FrequencyofGreenValues(x+1)=size(g(g==x),1);
        FrequencyofBlueValues(x+1)=size(b(b==x),1);
    end
    stem(0:255,FrequencyofRedValues,'.r');
    title('Red Channel Histogram');
    figure
    
    stem(0:255,FrequencyofGreenValues,'.g');
    title('Green Channel Histogram');
    figure
    
    stem(0:255,FrequencyofBlueValues,'.b');
    title('Blue Channel Histogram');
    

    【讨论】:

      猜你喜欢
      • 2014-12-18
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      • 2012-01-02
      • 2013-01-18
      • 2013-10-17
      • 2011-05-26
      相关资源
      最近更新 更多