示例程序014--彩色图像各个通道的直方图显示

参考自:http://blog.csdn.net/xiaowei_cqu/article/details/7600666

// 020 一维直方图(二).cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<cv.h>
#include<highgui.h>

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

  IplImage * src= cvLoadImage("baboon.jpg");
  IplImage* gray_plane = cvCreateImage(cvGetSize(src),8,1);
  IplImage* red_plane=cvCreateImage(cvGetSize(src),8,1);
  IplImage* green_plane=cvCreateImage(cvGetSize(src),8,1);
  IplImage* blue_plane=cvCreateImage(cvGetSize(src),8,1);
  cvCvtColor(src,gray_plane,CV_BGR2GRAY);
  cvSplit(src,blue_plane,green_plane,red_plane,NULL);

  //int hist_size = 100;    //直方图尺寸
  int hist_size =256;
  int hist_height = 256;
  float range[] = {0,255};  //灰度级的范围
  float* ranges[]={range};

  //创建一维直方图,统计图像在[0 255]像素的均匀分布
  CvHistogram* gray_hist = cvCreateHist(1,&hist_size,CV_HIST_ARRAY,ranges,1);
  CvHistogram* red_hist = cvCreateHist(1,&hist_size,CV_HIST_ARRAY,ranges,1);
  CvHistogram* green_hist = cvCreateHist(1,&hist_size,CV_HIST_ARRAY,ranges,1);
  CvHistogram* blue_hist = cvCreateHist(1,&hist_size,CV_HIST_ARRAY,ranges,1);

  //计算灰度图像的一维直方图
  cvCalcHist(&gray_plane,gray_hist,0,0);
  cvCalcHist(&red_plane,red_hist,0,0);
  cvCalcHist(&green_plane,green_hist,0,0);
  cvCalcHist(&blue_plane,blue_hist,0,0);


  //归一化直方图
  cvNormalizeHist(gray_hist,1.0);
  cvNormalizeHist(red_hist,1.0);
  cvNormalizeHist(green_hist,1.0);
  cvNormalizeHist(blue_hist,1.0);

 // int scale = 2;
  int scale=4;
  //创建一张一维直方图的“图”,横坐标为灰度级,纵坐标为像素个数(*scale)
  IplImage* hist_image = cvCreateImage(cvSize(hist_size*scale,hist_height),8,3);
  cvZero(hist_image);
  //统计直方图中的最大直方块
  float max_value = 0;
  float max_value_r=0;
  float max_value_g = 0;
  float max_value_b = 0;

  cvGetMinMaxHistValue(gray_hist, 0,&max_value,0,0);
  cvGetMinMaxHistValue(red_hist, 0,&max_value_r,0,0);
  cvGetMinMaxHistValue(green_hist, 0,&max_value_g,0,0);
  cvGetMinMaxHistValue(blue_hist, 0,&max_value_b,0,0);

  for(int i=0;i<hist_size;i++)
  {
   float bin_val = cvQueryHistValue_1D(gray_hist,i); //像素i的概率,即第i个直方块的值
   float bin_val_r = cvQueryHistValue_1D(red_hist,i); 

   float bin_val_g = cvQueryHistValue_1D(green_hist,i); 

   float bin_val_b = cvQueryHistValue_1D(blue_hist,i); 

   int intensity = cvRound(bin_val*hist_height/max_value);  //要绘制的高度,四舍五入
   int intensity_r = cvRound(bin_val_r*hist_height/max_value);  

   int intensity_g = cvRound(bin_val_g*hist_height/max_value);  

   int intensity_b = cvRound(bin_val_b*hist_height/max_value);   

  cvRectangle(hist_image,                                 //灰度直方图
    cvPoint(i,hist_height-1),
    cvPoint((i+1) - 1, hist_height - intensity),
    CV_RGB(128,128,128)); 
   cvRectangle(hist_image,                                    //红色直方图
    cvPoint(i+256,hist_height-1),
    cvPoint((i+1)+256 - 1, hist_height - intensity_r),
    CV_RGB(255,0,0));
   cvRectangle(hist_image,                                  //绿色直方图
    cvPoint(i+512,hist_height-1),
    cvPoint((i+1)+512 - 1, hist_height - intensity_g),
    CV_RGB(0,2128,0)); 
   cvRectangle(hist_image,                                  //蓝色直方图
    cvPoint(i+768,hist_height-1),
    cvPoint((i+1)+768 - 1, hist_height - intensity_b),
    CV_RGB(0,0,255)); 
  }

  cvNamedWindow( "GraySource", 1 );
  cvShowImage("GraySource",gray_plane);
  cvNamedWindow( "H-S Histogram", 1 );
  cvShowImage( "H-S Histogram", hist_image );
  
  cvWaitKey(0);

 cvReleaseImage(&src);
  cvReleaseImage(&gray_plane);
  cvReleaseImage(&red_plane);
  cvReleaseImage(&blue_plane);
  cvReleaseImage(&gray_plane);
  cvReleaseImage(&hist_image);
  cvDestroyAllWindows();
 return 0;
}

 

运行结果:

示例程序014--彩色图像各个通道的直方图显示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值