1、H-S直方图:
#include<highgui.h>
#include<cv.h>
//宏定义
#define cvQueryHistValue_2D(hist,idx0,idx1)\
cvGetReal2D((hist)->bins,(idx0),(idx1)) using namespace std; using namespace cv; int main() { IplImage* image = cvLoadImage("E:\\project\\images\\baboon.jpg"); cvNamedWindow("image"); cvShowImage("image", image); IplImage* hsv = cvCreateImage(cvGetSize(image), 8, 3); cvCvtColor(image, hsv, CV_BGR2HSV); IplImage* h_image = cvCreateImage(cvGetSize(image), 8, 1); IplImage* s_image = cvCreateImage(cvGetSize(image), 8, 1); IplImage* v_image = cvCreateImage(cvGetSize(image), 8, 1); //拆分为单通道图像 cvSplit(hsv, h_image, s_image, v_image, 0); IplImage* images[] = { h_image, s_image }; //*****************计算直方图******************** //准备参数 int h_size = 256; int s_size = 180; int size[] = { h_size, s_size }; float h_range[] = { 0, 256 }; float s_range[] = { 0, 180 }; float* ranges[] = { h_range, s_range }; CvHistogram* hist = cvCreateHist(2, size, CV_HIST_ARRAY, ranges, 1); cvCalcHist(images, hist, 0, 0); //绘制图像 int scale = 2; IplImage* hist_image = cvCreateImage(cvGetSize(image), 8, 3); float hist_max = 0; cvGetMinMaxHistValue(hist, 0, &hist_max, 0, 0); for (int i = 0; i < h_size; i++) { for (int j = 0; j < s_size; j++) { float bin_value = cvQueryHistValue_2D(hist, i,j); int intensity = cvRound(bin_value * 256 / hist_max); cvRectangle(hist_image, CvPoint(i*scale, j*scale), CvPoint((i+1)*scale-1,(j+1)*scale-1), CV_RGB(intensity,intensity,intensity), CV_FILLED); } } cvNamedWindow("H-S Histogram"); cvShowImage("H-S Histogram", hist_image); cvWaitKey(0); return 0; }
2、B-G直方图:
#include<highgui.h>
#include<cv.h>
//宏定义
#define cvQueryHistValue_2D(hist,idx0,idx1)\
cvGetReal2D((hist)->bins,(idx0),(idx1)) using namespace std; using namespace cv; int main() { //二维R-G直方图 IplImage* image = cvLoadImage("E:\\project\\images\\baboon.jpg"); cvNamedWindow("image"); cvShowImage("image", image); IplImage* imgBlue = cvCreateImage(cvGetSize(image), 8, 1); IplImage* imgGreen = cvCreateImage(cvGetSize(image), 8, 1); IplImage* imgRed = cvCreateImage(cvGetSize(image), 8, 1); cvSplit(image, imgBlue, imgGreen, imgRed, 0); IplImage* images[] = { imgBlue,imgGreen }; int dims = 2; int b_size = 256; int g_size = 256; int sizes[] = { b_size, g_size }; float b_range[] = { 0, 255 }; float g_range[] = { 0, 255 }; float* ranges[] = { b_range, g_range }; CvHistogram* hist = cvCreateHist(dims,sizes,CV_HIST_ARRAY,ranges,1); cvCalcHist(images,hist,0,0); //绘制图像 //初始画布 int scale = 2; IplImage* hist_image = cvCreateImage(cvGetSize(image), 8, 3); cvZero(hist_image); float hist_max = 0; cvGetMinMaxHistValue(hist, 0, &hist_max, 0, 0); for (int i = 0; i < b_size; i++) { for (int j = 0; j < g_size; j++) { float bin_value = cvQueryHistValue_2D(hist, i, j); int intensity = cvRound(bin_value * 255 / hist_max); cvRectangle(hist_image, CvPoint(i*scale, j*scale), CvPoint((i + 1)*scale - 1, (j + 1)*scale - 1), CV_RGB(intensity, intensity, intensity), CV_FILLED); } } cvNamedWindow("B-G Histogram"); cvShowImage("B-G Histogram", hist_image); cvWaitKey(0); return 0; }
结果显示: