0

I've been trying to implement a function called histogram(image) that perform the color histogram of an RGB image. Can someone show me a sample that is easy to analyze in order to understand how does it exactly work.

I would really appreciate your help.

Alladin
  • 1,010
  • 3
  • 27
  • 45
  • Did you try something so far? What do you mean by 'without using built-in function'? – Cape Code Nov 29 '14 at 18:54
  • what i meant is without using the imhist() or other Matlab functions, i need to know the logic of the color histogram, because i am still beginner in Matlab, i don't even know how to start my function. – Alladin Nov 29 '14 at 18:56
  • You can also take a look at this post too. The code I wrote implements the colour histogram as a 1D histogram where a particular colour tuple gets converted into a single 1D index. http://stackoverflow.com/questions/25830225/content-based-image-retrieval-and-precision-recall-graphs-using-color-histograms/25830848#25830848 – rayryeng Dec 01 '14 at 00:11

2 Answers2

4

Here is the code which draws histogram of each color channels of an image.

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');
Muhammet Ali Asan
  • 1,486
  • 22
  • 39
4

Here is a different algorithm for your question:

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
b4hand
  • 9,550
  • 4
  • 44
  • 49
mehmet
  • 1,631
  • 16
  • 21