OpenCV1.0图像直方图

#include "cv.h"
#include "highgui.h"
#include "cxcore.h"

IplImage* histdraw(CvHistogram* hist,float scalex=1,float scaley=1)
{
float histmax=0;
cvGetMinMaxHistValue(hist,0,&histmax,0);

IplImage* result=cvCreateImage(cvSize(256*scalex,64*scaley),8,1);
cvZero(result);

for(int i=0;i<255;i++)
{
float cvnowvalue=cvQueryHistValue_1D(hist,i);
float cvnextvalue=cvQueryHistValue_1D(hist,i+1);

CvPoint pt0=cvPoint(i*scalex,64*scaley);
CvPoint pt1=cvPoint((i+1)*scalex,64*scaley);
CvPoint pt2=cvPoint((i+1)*scalex, (1-(cvnextvalue/histmax))*64*scaley);
CvPoint pt3=cvPoint(i*scalex,(1-(cvnowvalue/histmax))*64*scaley);


int numpts=5;
CvPoint pts[5];
pts[0]=pt0;pts[1]=pt1;pts[2]=pt2;pts[3]=pt3;pts[4]=pt0;

cvFillConvexPoly(result,pts,numpts,cvScalar(255));

return (result);
}
}



int main(int argc, char* argv[])
{

IplImage* scr=cvLoadImage("lena.jpg",0);
cvNamedWindow("win");
cvShowImage("win",scr);
cvWaitKey(0);

//直方图创建
int dims=1;
int size=256;
float rang[]={0,255};
float* rangs[]={rang};
CvHistogram* hist;
hist=cvCreateHist(dims,&size,CV_HIST_ARRAY,rangs,1);
cvClearHist(hist);


//彩色图分解为三通道
IplImage* imgblue=cvCreateImage(cvGetSize(scr),8,1);
IplImage* imggreen=cvCreateImage(cvGetSize(scr),8,1);
IplImage* imgred=cvCreateImage(cvGetSize(scr),8,1);
cvSplit(scr,imgblue,imggreen,imgred,NULL);

//计算直方图
cvCalcArrHist(&imgblue,hist,0,0);
IplImage* blue=histdraw(hist);//调用显示函数
cvClearHist(hist);

cvCalcArrHist(&imggreen,hist,0,0);
IplImage* green=histdraw(hist);
cvClearHist(hist);

cvCalcArrHist(&imgred,hist,0,0);
IplImage* red=histdraw(hist);
cvClearHist(hist);

cvNamedWindow("b");
cvNamedWindow("g");
cvNamedWindow("r");

cvShowImage("b",blue);
cvShowImage("g",green);
cvShowImage("r",red);
cvWaitKey(0);

cvReleaseImage(&scr);
cvReleaseImage(&blue);
cvReleaseImage(&green);
cvReleaseImage(&red);
cvDestroyWindow("b");
cvDestroyWindow("g");
cvDestroyWindow("r");

return 0;
}


 

#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
  
void FillWhite(IplImage *pImage)  
{  
    cvRectangle(pImage, cvPoint(0, 0), cvPoint(pImage->width, pImage->height), CV_RGB(255, 255, 255), CV_FILLED);  
}  
// 创建灰度图像的直方图  
CvHistogram* CreateGrayImageHist(IplImage **ppImage)  
{  
    int nHistSize = 256;  
    float fRange[] = {0, 255};  //灰度级的范围    
    float *pfRanges[] = {fRange};    
    CvHistogram *pcvHistogram = cvCreateHist(1, &nHistSize, CV_HIST_ARRAY, pfRanges);  
    cvCalcHist(ppImage, pcvHistogram);  
    return pcvHistogram;  
}  
// 根据直方图创建直方图图像  
IplImage* CreateHisogramImage(int nImageWidth, int nScale, int nImageHeight, CvHistogram *pcvHistogram)  
{  
    IplImage *pHistImage = cvCreateImage(cvSize(nImageWidth * nScale, nImageHeight), IPL_DEPTH_8U, 1);  
    FillWhite(pHistImage);  

    //统计直方图中的最大直方块  
    float fMaxHistValue = 0;  
    cvGetMinMaxHistValue(pcvHistogram, NULL, &fMaxHistValue, NULL, NULL);  

    //分别将每个直方块的值绘制到图中  
    int i;  
    for(i = 0; i < nImageWidth; i++)  
    {  
        float fHistValue = cvQueryHistValue_1D(pcvHistogram, i); //像素为i的直方块大小  
        int nRealHeight = cvRound((fHistValue / fMaxHistValue) * nImageHeight);  //要绘制的高度  
        cvRectangle(pHistImage,  
            cvPoint(i * nScale, nImageHeight - 1),  
            cvPoint((i + 1) * nScale - 1, nImageHeight - nRealHeight),  
            cvScalar(i, 0, 0, 0),   
            CV_FILLED  
);   
    }  
    return pHistImage;  
}  
int main( int argc, char** argv )  
{     
    const char *pstrWindowsSrcTitle = "原图";  
    const char *pstrWindowsGrayTitle = "灰度图";  
    const char *pstrWindowsHistTitle = "直方图";  

    // 从文件中加载原图  
    IplImage *pSrcImage = cvLoadImage("lena.jpg", CV_LOAD_IMAGE_UNCHANGED);  
    IplImage *pGrayImage = cvCreateImage(cvGetSize(pSrcImage), IPL_DEPTH_8U, 1);  
    // 灰度图  
    cvCvtColor(pSrcImage, pGrayImage, CV_BGR2GRAY);  

    // 灰度直方图  
    CvHistogram *pcvHistogram = CreateGrayImageHist(&pGrayImage);  

    // 创建直方图图像  
    int nHistImageWidth = 255;  
    int nHistImageHeight = 150;  //直方图图像高度  
    int nScale = 2;              
    IplImage *pHistImage = CreateHisogramImage(nHistImageWidth, nScale, nHistImageHeight, pcvHistogram);  

    // 显示  
    cvNamedWindow(pstrWindowsSrcTitle, CV_WINDOW_AUTOSIZE);  
    cvNamedWindow(pstrWindowsGrayTitle, CV_WINDOW_AUTOSIZE);  
    cvNamedWindow(pstrWindowsHistTitle, CV_WINDOW_AUTOSIZE);  
    cvShowImage(pstrWindowsSrcTitle, pSrcImage);  
    cvShowImage(pstrWindowsGrayTitle, pGrayImage);  
    cvShowImage(pstrWindowsHistTitle, pHistImage);  


    cvWaitKey(0);  

    cvReleaseHist(&pcvHistogram);  

    cvDestroyWindow(pstrWindowsSrcTitle);  
    cvDestroyWindow(pstrWindowsGrayTitle);  
    cvDestroyWindow(pstrWindowsHistTitle);  
    cvReleaseImage(&pSrcImage);  
    cvReleaseImage(&pGrayImage);  
    cvReleaseImage(&pHistImage);  
    return 0;  
}

你可能感兴趣的:(java,开发语言)