本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/koriya/archive/2008/11/21/3347369.aspx
三維直方圖擷取
/****************************
* change image histogram example
* date:08-11-1
* author: wml
* copyright:1.0
*****************************/
#include <cv.h>
#include <highgui.h>
#include <cvaux.h>
#include <cxcore.h>
#include <iostream.h>
#include <string.h>
#include <stdio.h>
#pragma comment(lib,"cv")
#pragma comment(lib,"cvaux")
#pragma comment(lib,"cxcore")
#pragma comment(lib,"highgui")
int Histogram3DBlock[3] = {256,256,256};
float HistogramRange1[6]={0,255,0,255,0,255};
float *HistogramRange[3]={&HistogramRange1[0],&HistogramRange1[2],&HistogramRange1[4]};
void Print3DHistogram(CvHistogram *Histogram,int BlockSize);
int main()
{
CvHistogram *Histogram1;
IplImage *Image1=cvLoadImage("D://image test c++//histrogm3/1.bmp",1);
IplImage *RedImage=cvCreateImage(cvGetSize(Image1),8,1);
IplImage *GreenImage=cvCreateImage(cvGetSize(Image1),8,1);
IplImage *BlueImage=cvCreateImage(cvGetSize(Image1),8,1);
IplImage *ImageArray[3]={RedImage,GreenImage,BlueImage};
cvSplit(Image1,BlueImage,GreenImage,RedImage,0);
Histogram1 = cvCreateHist(3,Histogram3DBlock,CV_HIST_SPARSE,HistogramRange);
cvCalcHist(ImageArray,Histogram1);
printf("3D Historgram Data/n");
Print3DHistogram(Histogram1,256);
cvNamedWindow("Riverbank",1);
cvShowImage("Riverbank",Image1);
cvWaitKey(0);
}
void Print3DHistogram(CvHistogram *Histogram,int BlockSize)
{
for(int i=0;i<BlockSize;i++)
{
for(int j=0;j<BlockSize;j++)
{
for(int k=0;k<BlockSize;k++)
{
if(cvQueryHistValue_3D(Histogram,i,j,k)>10)
{
printf("%5.f",cvQueryHistValue_3D(Histogram,i,j,k));
}
}
}
}
}
面是三個維度的統計直方圖,分別為R維度,G維度,B維度,並且分別被量化成256等分的區塊,同樣的,也是使用LUT的方法,由於這個統計直方圖非常的大,它所佔的區域為256*256*256的大小,而實際上累積的分佈只有一點點,所以說,對於高維度的統計直方圖就要用到稀疏矩陣,要不然會浪放大量的記憶體空間,但是對於多維度的圖表呈現,OpenCV最多也只能支援到一維的方式,二維的方式可以用OpenGL(glut)呈現,當然高的維度本來就很難用視覺化的方式呈現.