用opencv画两幅图片的直方图

31 篇文章 0 订阅
27 篇文章 1 订阅
//计算直方图,并用图片显示出来直方图结果图像; 
//原创crazyMu
 void  DrawHis(IplImage *image1,IplImage *image2)
{
    int size=256;  
    float range[]={0,255};  
    float* ranges[]={range}; 
    double binnum[256]={0};

    CvHistogram* hist1=cvCreateHist(1,&size,CV_HIST_ARRAY,ranges,1); 
    CvHistogram* hist2=cvCreateHist(1,&size,CV_HIST_ARRAY,ranges,1);  
    cvCalcHist(&image1,hist1,0,NULL);  
    cvCalcHist(&image2,hist2,0,NULL);  

    float max1=0; 
    float max2=0; 
    cvGetMinMaxHistValue(hist1,NULL,&max1,NULL,NULL); 
    cvGetMinMaxHistValue(hist2,NULL,&max2,NULL,NULL);  

    IplImage* dst=cvCreateImage(cvSize(400,300),8,3);  
    cvSet(dst,cvScalarAll(255),0);  

    double bin_width=(double)dst->width/size;  
    double bin_unith1=(double)dst->height/max1;  
    double bin_unith2=(double)dst->height/max2; 
    for(int i=0;i<size;i++)  
    {  
        CvPoint p0=cvPoint(i*bin_width,dst->height);  
        CvPoint p1=cvPoint((i+1)*bin_width,dst->height-cvGetReal1D(hist1->bins,i)*bin_unith1);  
        cvRectangle(dst,p0,p1,cvScalar(0,0,255),-1,8,0);  //用红色显示直方图

        CvPoint p2=cvPoint((i+1)*bin_width,dst->height-cvGetReal1D(hist2->bins,i)*bin_unith2); 
        cvRectangle(dst,p0,p2,cvScalar(255,0,0),-1,8,0);    //用绿色显示直方图
    }  

    cvShowImage("gray image histogram",dst); 
    cvReleaseHist(&hist1);
    cvReleaseHist(&hist2);
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值