I'm going to identify currency notes base on color.I used histogram for this.I could identify only R,G,B values.How can i detect yellow and brown color.This is what i'm done so far.are there any other methods to detect color with out going for histogram.
#include "stdafx.h"
#include <cv.h>
#include <iostream>
#include <highgui.h>
#include <cxcore.h>
int _tmain(int argc, _TCHAR* argv[])
{
IplImage* img = cvLoadImage("500.jpg");
if( !img )
return -1;
IplImage* channel = cvCreateImage( cvGetSize(img), 8, 1 );
IplImage *hist_img = cvCreateImage(cvSize(300,240), 8, 3);
IplImage *hist_imgfinal = cvCreateImage(cvSize(300,240), 8, 3);
IplImage *hist_red1 = cvCreateImage(cvSize(300,240), 8, 3);
IplImage *hist_blue1 = cvCreateImage(cvSize(300,240), 8, 3);
IplImage *hist_green1 = cvCreateImage(cvSize(300,240), 8, 3);
IplImage *mer1 = cvCreateImage(cvSize(300,240), 8, 3);
IplImage *mer2 = cvCreateImage(cvSize(300,240), 8, 3);
IplImage *mer3 = cvCreateImage(cvSize(300,240), 8, 3);
cvSet( hist_img, cvScalarAll(255), 0 );
CvHistogram *hist_red;
CvHistogram *hist_green;
CvHistogram *hist_blue;
int hist_size = 256;
float range[]={0,256};
float* ranges[] = { range };
float max_value = 0.0;
float max_valueb = 0.0;
float max_valuer = 0.0;
float max_valueg = 0.0;
float max = 0.0;
float maxb = 0.0;
float maxg = 0.0;
float w_scale = 0.0;
hist_red = cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);
hist_green = cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);
hist_blue = cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);
cvSetImageCOI(img,3);
cvCopy(img,channel);
cvResetImageROI(img);
cvCalcHist( &channel, hist_red, 0, NULL );
cvSetImageCOI(img,2);
cvCopy(img,channel);
cvResetImageROI(img);
cvCalcHist( &channel, hist_green, 0, NULL );
cvSetImageCOI(img,1);
cvCopy(img,channel);
cvResetImageROI(img);
cvCalcHist( &channel, hist_blue, 0, NULL );
cvGetMinMaxHistValue( hist_red, 0, &max_valuer, 0, 0 );
cvGetMinMaxHistValue( hist_green, 0, &maxg, 0, 0 );
max_value = (maxg > max_valuer) ? maxg : max_valuer;
cvGetMinMaxHistValue( hist_blue, 0, &maxb, 0, 0 );
max_valueb = (maxb > max_value) ? maxb : max_value;
printf("Maximum Histogram Value: %f\n",max_valueb );
printf("Maximum Histogram Value blue");
cvScale( hist_red->bins, hist_red->bins, ((float)hist_img->height)/max_value, 0 );
cvScale( hist_green->bins, hist_green->bins, ((float)hist_img->height)/max_value, 0 );
cvScale( hist_blue->bins, hist_blue->bins, ((float)hist_img->height)/max_value, 0 );
w_scale = ((float)hist_img->width)/hist_size;
for( int i = 0; i < hist_size; i++ )
{
cvRectangle( hist_img, cvPoint((int)i*w_scale , hist_img->height),
cvPoint((int)(i+1)*w_scale, hist_img->height - cvRound(cvGetReal1D(hist_red->bins,i))),
CV_RGB(255,0,0), -1, 8, 0 );
cvRectangle( hist_img, cvPoint((int)i*w_scale , hist_img->height),
cvPoint((int)(i+1)*w_scale, hist_img->height - cvRound(cvGetReal1D(hist_green->bins,i))),
CV_RGB(0,255,0), -1, 8, 0 );
cvRectangle( hist_img, cvPoint((int)i*w_scale , hist_img->height),
cvPoint((int)(i+1)*w_scale, hist_img->height - cvRound(cvGetReal1D(hist_blue->bins,i))),
CV_RGB(0,0,255), -1, 8, 0 );
}
for( int i = 0; i < hist_size; i++ )
{
cvRectangle( hist_red1, cvPoint((int)i*w_scale , hist_img->height),
cvPoint((int)(i+1)*w_scale, hist_img->height - cvRound(cvGetReal1D(hist_red->bins,i))),
CV_RGB(255,0,0), -1, 8, 0 );
}
for( int i = 0; i < hist_size; i++ )
{
cvRectangle( hist_green1, cvPoint((int)i*w_scale , hist_img->height),
cvPoint((int)(i+1)*w_scale, hist_img->height - cvRound(cvGetReal1D(hist_green->bins,i))),
CV_RGB(0,255,0), -1, 8, 0 );
}
for( int i = 0; i < hist_size; i++ )
{
cvRectangle( hist_blue1, cvPoint((int)i*w_scale , hist_img->height),
cvPoint((int)(i+1)*w_scale, hist_img->height - cvRound(cvGetReal1D(hist_blue->bins,i))),
CV_RGB(0,0,255), -1, 8, 0 );
}
cvAdd(hist_red1,hist_green1,mer1,0);
cvAdd(hist_red1,hist_blue1,mer2,0);
cvAdd(hist_green1,hist_blue1,mer3,0);
return 0;
}