# plotting histogram with RGB value

Page 1 of 1

## 2 Replies - 4938 Views - Last Post: 15 January 2010 - 07:17 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=150042&amp;s=a1338607ae783f1d86a93945f51ad5bf&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 gnelkia

Reputation: 0
• Posts: 14
• Joined: 13-January 10

# plotting histogram with RGB value

Posted 15 January 2010 - 01:30 AM

Dear All.

i have come out with a code which able to plot a histogram for an image.
but the image first has to be converted into grayscale.

Does anyone know how can i plot a histogram directly with the RGB value obtain from the image. ? without convert the image into grayscale.

because when directly getting RGB value from an image, basically we will obtain 3 layer of values, thus mostly need 3 histogram to represent each, R , G and B.

is there any method that allow me to plot in 1 histogram ?

thank you so much.

```				int i, hist_size = 256;
float max_value,min_value;
float min_idx,max_idx;
float bin_w;
float mean =0, variance =0;

float range_0[]={0,256};
float *ranges[]={range_0};

//Create a single planed image of the same size as the original
IplImage* grayImage = cvCreateImage(cvSize(im->width,im->height),IPL_DEPTH_8U, 1);
//convert the original image to gray
cvCvtColor(im, grayImage, CV_BGR2GRAY);
//create a rectangular area to evaluate
CvRect rect = cvRect(0, 0, 500, 600 );
//apply the rectangle to the image and establish a region of interest
cvSetImageROI(grayImage, rect);

//create an image to hold the histogram
IplImage* histImage = cvCreateImage(cvSize(320,200), 8, 1);
//create a histogram to store the information from the image
CvHistogram* hist = cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);
//calculate the histogram and apply to hist
cvCalcHist( &grayImage, hist, 0, NULL );

//grab the min and max values and their indeces
cvGetMinMaxHistValue( hist, &min_value, &max_value, 0, 0);
//scale the bin values so that they will fit in the image representation
cvScale( hist->bins, hist->bins, ((double)histImage->height)/max_value, 0 );

//set all histogram values to 255
cvSet( histImage, cvScalarAll(255), 0 );
//create a factor for scaling along the width
bin_w = cvRound((double)histImage->width/hist_size);

for( i = 0; i < hist_size; i++ ) {
//draw the histogram data onto the histogram image
cvRectangle( histImage, cvPoint(i*bin_w, histImage->height),cvPoint((i+1)*bin_w,histImage->height - cvRound(cvGetReal1D(hist->bins,i))),cvScalarAll(0), -1, 8, 0 );
//get the value at the current histogram bucket
float* bins = cvGetHistValue_1D(hist,i);
//increment the mean value
mean += bins[0];
}
//finish mean calculation
mean /= hist_size;
//go back through now that mean has been calculated in order to calculate variance
for( i = 0; i < hist_size; i++ ) {
float* bins = cvGetHistValue_1D(hist,i);
variance += pow((bins[0] - mean),2);
}
//finish variance calculation
variance /= hist_size;

cvNamedWindow("Original", 0);
cvShowImage("Original", im );

cvNamedWindow("Gray", 0);
cvShowImage("Gray", grayImage );

cvNamedWindow("Histogram", 0);
cvShowImage("Histogram", histImage );

//hold the images until a key is pressed
cvWaitKey(0);

//clean up images
cvReleaseImage(&histImage);
cvReleaseImage(&grayImage);
cvReleaseImage(&im);

//remove windows
cvDestroyWindow("Original");
cvDestroyWindow("Gray");
cvDestroyWindow("Histogram");

```

Is This A Good Question/Topic? 0

## Replies To: plotting histogram with RGB value

### #2 NickDMax

Reputation: 2222
• Posts: 9,208
• Joined: 18-February 07

## Re: plotting histogram with RGB value

Posted 15 January 2010 - 12:03 PM

Well this is not really hard... just use 1 color value to produce the histogram. Then you can overlay them. since I don't know anything about whatever library you are using I don't know how to help you implement it.

### #3 gnelkia

Reputation: 0
• Posts: 14
• Joined: 13-January 10

## Re: plotting histogram with RGB value

Posted 15 January 2010 - 07:17 PM

NickDMax, on 15 Jan, 2010 - 11:03 AM, said:

Well this is not really hard... just use 1 color value to produce the histogram. Then you can overlay them. since I don't know anything about whatever library you are using I don't know how to help you implement it.

hai there~ thanks for comment..

currently i am using open cv lib.. is there any better lib for histogram plotting ?

overlay them.. maybe will end up with 3 peak ??

the RGB value is in pairs.. i mean 1 pixel with particular RGB value. if we plot them separately.. i am afraid of losing of data.. cannot retrieve back the RGB value for single pixel..

there is a function in MATLAB which could plot them in RGB value.. directly calculate how many times certain RGB appear.. is C++ with this kind of function ??

example:

at the end of program. i hope to know

RGB [111;12;33] ---> 20
RGB [122;33;222] ---> 12
and so on..

so i can know which is dominant RGB value.

Thank you so much~