IplImage* colorImg=cvLoadImage("Desert.jpg");
cvNamedWindow("Color",CV_WINDOW_AUTOSIZE);
cvShowImage("Color",colorImg);
IplImage* grayImg = cvCreateImage(cvSize(colorImg->width,colorImg->height),8,1);
for(int i = 0;i < colorImg->height;i ++)
for(int j = 0;j < colorImg->width;j ++)
{
double k = 0;
k += ((unsigned char*) (colorImg->imageData))[i * colorImg->widthStep + j * 3] * 0.114;
k += ((unsigned char*) (colorImg->imageData))[i * colorImg->widthStep + j * 3 + 1] * 0.587;
k += ((unsigned char*) (colorImg->imageData))[i * colorImg->widthStep + j * 3 + 2] * 0.299;
((unsigned char*) (grayImg->imageData))[i * grayImg->widthStep + j] = (int)k;
}
IplImage* hsv = cvCreateImage(cvGetSize(colorImg),8,3);
cvCvtColor(colorImg,hsv,CV_BGR2HSV); //色彩空间转换->HSV
IplImage* h_plane = cvCreateImage(cvGetSize(colorImg),8,1);
IplImage* s_plane = cvCreateImage(cvGetSize(colorImg),8,1);
IplImage* v_plane = cvCreateImage(cvGetSize(colorImg),8,1);
IplImage* planes[] = {h_plane,s_plane};
cvCvtPixToPlane(hsv,h_plane,s_plane,v_plane,0); //提取各个通道
int h_bins = 32,s_bins = 30;
CvHistogram* hist;
{
int hist_size[] = {h_bins,s_bins};
float h_ranges[] = {0,180}; //hue is [0,180]
float s_ranges[] = {0,255};
float* ranges[] = {h_ranges,s_ranges};
hist = cvCreateHist(2,hist_size,CV_HIST_ARRAY,ranges,1);
}
cvCalcHist(planes,hist,0,0); //compute histogram
cvNormalizeHist(hist,1.0);
//Create an image to use to visualize our histogram
int scale = 10;
IplImage* hist_img = cvCreateImage(cvSize(h_bins * scale,s_bins * scale),8,3);
cvZero(hist_img);
//populate our visualization with little gray squares
float max_value = 0;
cvGetMinMaxHistValue(hist,0,&max_value,0,0);
for(int h = 0;h < h_bins;h ++)
{
for(int s = 0;s < s_bins;s ++)
{
float bin_val = cvQueryHistValue_2D(hist,h,s);
int intensity = cvRound(bin_val * 255 / max_value);
cvRectangle(hist_img,cvPoint(h * scale,0),cvPoint((h + 1) * scale - 1,(s + 1) * scale - 1),CV_RGB(intensity,intensity,intensity),CV_FILLED);
}
}
cvNamedWindow("H-S Histogram",CV_WINDOW_AUTOSIZE);
cvShowImage("H-S Histogram",hist_img);
cvNamedWindow("hsv",CV_WINDOW_AUTOSIZE);
cvShowImage("hsv",hsv);
cvNamedWindow("h_plane",CV_WINDOW_AUTOSIZE);
cvShowImage("h_plane",h_plane);
cvNamedWindow("s_plane",CV_WINDOW_AUTOSIZE);
cvShowImage("s_plane",s_plane);
cvNamedWindow("v_plane",CV_WINDOW_AUTOSIZE);
cvShowImage("v_plane",v_plane);
cvNamedWindow("Gray",CV_WINDOW_AUTOSIZE);
cvShowImage("Gray",grayImg);
cvWaitKey(0);
cvReleaseImage(&hist_img);
cvDestroyWindow("H-S Histogram");
cvReleaseImage(&grayImg);
cvDestroyWindow("Gray");
cvReleaseImage(&hsv);
cvDestroyWindow("hsv");
cvReleaseImage(&h_plane);
cvDestroyWindow("h_plane");
cvReleaseImage(&s_plane);
cvDestroyWindow("s_plane");
cvReleaseImage(&v_plane);
cvDestroyWindow("v_plane");
//rgb单通道提取并显示
IplImage* rImg = cvCreateImage(cvGetSize(colorImg),8,1);
IplImage* gImg = cvCreateImage(cvGetSize(colorImg),8,1);
IplImage* bImg = cvCreateImage(cvGetSize(colorImg),8,1);
cvSplit(colorImg,rImg,gImg,bImg,0);
cvNamedWindow("red",1);
cvNamedWindow("green",1);
cvNamedWindow("blue",1);
cvShowImage("red",rImg);
cvShowImage("green",gImg);
cvShowImage("blue",bImg);
//rgb单通道转三通道显示
IplImage* rImg3 = cvCreateImage(cvGetSize(colorImg),8,3);
IplImage* gImg3 = cvCreateImage(cvGetSize(colorImg),8,3);
IplImage* bImg3 = cvCreateImage(cvGetSize(colorImg),8,3);
cvSetZero(rImg3);
cvSetZero(gImg3);
cvSetZero(bImg3);
cvMerge(0,0,rImg,0,rImg3);
cvMerge(0,gImg,0,0,gImg3);
cvMerge(bImg,0,0,0,bImg3);
cvNamedWindow("rImg3",1);
cvShowImage("rImg3",rImg3);
cvNamedWindow("gImg3",1);
cvShowImage("gImg3",gImg3);
cvNamedWindow("bImg3",1);
cvShowImage("bImg3",bImg3);
cvWaitKey(0);