#include "stdafx.h" void myShowHist(IplImage* image1,IplImage* image2); IplImage* cvShowHist(IplImage* src); int main() { //對彩色圖像進行均衡化 IplImage * image= cvLoadImage("E:\\C_VC_code\\Text_Photo\\girl004.jpg"); IplImage* eqlimage=cvCreateImage(cvGetSize(image),image->depth,3); //信道分離 IplImage* redImage=cvCreateImage(cvGetSize(image),image->depth,1); IplImage* greenImage=cvCreateImage(cvGetSize(image),image->depth,1); IplImage* blueImage=cvCreateImage(cvGetSize(image),image->depth,1); cvSplit(image,blueImage,greenImage,redImage,NULL);//用 cvSplit 函數分解圖像到單個色彩通道上 /* cvNamedWindow("red",CV_WINDOW_AUTOSIZE); cvNamedWindow("green",CV_WINDOW_AUTOSIZE); cvNamedWindow("blue",CV_WINDOW_AUTOSIZE); cvShowImage("red",redImage); cvShowImage("green",greenImage); cvShowImage("blue",blueImage); */ //cvEqualizeHist()是適用於灰度圖象直方圖均衡化,因此必須先將圖片分解到單通道上 //分別均衡化每一個信道 cvEqualizeHist(redImage,redImage); cvEqualizeHist(greenImage,greenImage); cvEqualizeHist(blueImage,blueImage); /* cvNamedWindow("red2",CV_WINDOW_AUTOSIZE); cvNamedWindow("green2",CV_WINDOW_AUTOSIZE); cvNamedWindow("blue2",CV_WINDOW_AUTOSIZE); cvShowImage("red2",redImage); cvShowImage("green2",greenImage); cvShowImage("blue2",blueImage); */ //信道合併 cvMerge(blueImage,greenImage,redImage,NULL,eqlimage); //顯示圖片和直方圖 cvNamedWindow( "source", 1 ); cvShowImage("source",image); cvNamedWindow( "Equalized", 1 ); cvShowImage("Equalized",eqlimage); cvSaveImage("equalized.jpg",eqlimage); myShowHist(image,eqlimage); cvWaitKey(0); cvDestroyWindow("source"); cvDestroyWindow("result"); cvReleaseImage( &image ); cvReleaseImage( &eqlimage ); } void myShowHist(IplImage* image1,IplImage* image2) { IplImage* hist_image1=cvShowHist(image1); IplImage* hist_image2=cvShowHist(image2); cvNamedWindow( "H-S Histogram1", 1 ); cvShowImage( "H-S Histogram1", hist_image1 ); cvNamedWindow( "H-S Histogram2", 1 ); cvShowImage( "H-S Histogram2", hist_image2 ); cvSaveImage("Histogram1.jpg",hist_image1); cvSaveImage("Histogram2.jpg",hist_image2); } IplImage* cvShowHist(IplImage* src) { IplImage* hsv = cvCreateImage( cvGetSize(src), 8, 3 ); IplImage* h_plane = cvCreateImage( cvGetSize(src), 8, 1 ); IplImage* s_plane = cvCreateImage( cvGetSize(src), 8, 1 ); IplImage* v_plane = cvCreateImage( cvGetSize(src), 8, 1 ); IplImage* planes[] = { h_plane, s_plane }; /** H 份量劃分爲16個等級,S份量劃分爲8個等級 */ int h_bins = 16, s_bins = 8; int hist_size[] = {h_bins, s_bins}; /** H 份量的變化範圍 */ float h_ranges[] = { 0, 180 }; /** S 份量的變化範圍*/ float s_ranges[] = { 0, 255 }; float* ranges[] = { h_ranges, s_ranges }; /** 輸入圖像轉換到HSV顏色空間 */ cvCvtColor( src, hsv, CV_BGR2HSV ); cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 ); /** 建立直方圖,二維, 每一個維度上均分 */ CvHistogram * hist = cvCreateHist( 2, hist_size, CV_HIST_ARRAY, ranges, 1 ); /** 根據H,S兩個平面數據統計直方圖 */ cvCalcHist( planes, hist, 0, 0 ); /** 獲取直方圖統計的最大值,用於動態顯示直方圖 */ float max_value; cvGetMinMaxHistValue( hist, 0, &max_value, 0, 0 ); /** 設置直方圖顯示圖像 */ int height = 240; int width = (h_bins*s_bins*6); IplImage* hist_img = cvCreateImage( cvSize(width,height), 8, 3 ); cvZero( hist_img ); /** 用來進行HSV到RGB顏色轉換的臨時單位圖像 */ IplImage * hsv_color = cvCreateImage(cvSize(1,1),8,3); IplImage * rgb_color = cvCreateImage(cvSize(1,1),8,3); int bin_w = width / (h_bins * s_bins); for(int h = 0; h < h_bins; h++) { for(int s = 0; s < s_bins; s++) { int i = h*s_bins + s; /** 得到直方圖中的統計次數,計算顯示在圖像中的高度 */ float bin_val = cvQueryHistValue_2D( hist, h, s ); int intensity = cvRound(bin_val*height/max_value); /** 得到當前直方圖表明的顏色,轉換成RGB用於繪製 */ cvSet2D(hsv_color,0,0,cvScalar(h*180.f / h_bins,s*255.f/s_bins,255,0)); cvCvtColor(hsv_color,rgb_color,CV_HSV2BGR); CvScalar color = cvGet2D(rgb_color,0,0); cvRectangle( hist_img, cvPoint(i*bin_w,height), cvPoint((i+1)*bin_w,height - intensity), color, -1, 8, 0 ); } } return hist_img; }