0% found this document useful (0 votes)
64 views

Color Histogram Matlab Code

This document contains code to compute and plot the color histogram of an RGB image. It reads in an image, converts it to HSV color space, counts the number of pixels in each color bin, and plots the histogram with different colors representing each bin. It first displays the original image, then computes the histogram by finding the hue value of each pixel, locating black/white pixels, and calculating the percentage of total pixels in each color range. Finally, it plots the histogram with filled regions to visualize the distribution of colors in the image.

Uploaded by

Usha Shanmugam
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Color Histogram Matlab Code

This document contains code to compute and plot the color histogram of an RGB image. It reads in an image, converts it to HSV color space, counts the number of pixels in each color bin, and plots the histogram with different colors representing each bin. It first displays the original image, then computes the histogram by finding the hue value of each pixel, locating black/white pixels, and calculating the percentage of total pixels in each color range. Finally, it plots the histogram with filled regions to visualize the distribution of colors in the image.

Uploaded by

Usha Shanmugam
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 1

%Compute t h e hi s t o g r am of t h e c o l o u r image

clc ;
close all;

%program to get color histogram of rgb image


I=imread('betal4.jpg');

figure (1)
imshow(I),title('original image' ) ;
%//IPDt o o l b o x
J = im2double ( I ) ;
[ index , map ] = RGB2Ind ( I ) ;
%//IPD t o o l b o x
pixels = prod ( size ( index ) ) ;
hsv = rgb2hsv ( map ) ;
h = hsv (: ,1) ;
s = hsv (: ,2) ;
v = hsv (: ,3) ;

% // Fi n d s l o c a t i o n of b l a c k and w hi t e p i x e l s
darks = find (v <0.2) ;
lights = find (s <0.05 & v >0.85) ;
h ([ darks lights ]) = -1;
% // Ge t s t h e number of a l l p i x e l s f o r ea c h c o l o u r bi n
black = length ( darks ) / pixels ;
white = length ( lights ) / pixels ;
red = length ( find (( h > .9167 | h <= .083) & h ~= -1)) / pixels ;
yellow = length ( find (h > .083 & h <= .25) ) / pixels ;
green = length ( find ( h > .25 & h <= .4167) )/ pixels ;
cyan = length ( find (h > .4167 & h <= .5833) ) / pixels ;
blue = length ( find (h > .5833 & h <= .75) ) / pixels ;
magenta = length ( find (h > .75 & h <= .9167) ) / pixels ;
x=sum( find (( h > .9167 | h <= .083) & h ~= -1));
z=sum( find (h > .083 & h <= .25));
% // P l o t s hi s t o g r am
figure (2)
hold on
fill([0 0 1 1],[0 red red 0],'r')
fill([1 1 2 2],[0 yellow yellow 0],'y')
fill([2 2 3 3],[0 green green 0],'g')
fill([3 3 4 4],[0 cyan cyan 0],'c')
fill([4 4 5 5 ],[0 blue blue 0],'b')
fill([5 5 6 6 ],[0 magenta magenta 0],'m')
fill([6 6 7 7 ],[0 white white 0],'w')
fill([7 7 8 8 ],[0 black black 0],'k')

axis([0 8 0 1]);

You might also like