程序比较简单,代码及注释:

 

// 046 绘制二维直方图 2.3版.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;
int main()
{


//储存基准图像
  Mat src, hsv;

 // src = imread( "Lena.jpg" );
  src = imread( "Baboon.jpg" );

 

  /// 转换到 HSV
  cvtColor( src, hsv, CV_BGR2HSV );

 

  /// 对hue通道使用50个bin,对saturatoin通道使用60个bin
  //int h_bins = 50; int s_bins = 60;
  int h_bins=256;int s_bins=256;
  int histSize[] = { h_bins, s_bins };

 

  // hue的取值范围从0到256, saturation取值范围从0到180
  float h_ranges[] = { 0, 255 };
  float s_ranges[] = { 0, 255 };
  const float* ranges[] = { h_ranges, s_ranges };

 

  // 使用第0和第1通道
  int channels[] = { 0, 1 };

 

  //创建直方图画布
  int scale=2;
  int hist_w=h_bins*2;
  int hist_h=s_bins*2;
  Mat hist_image(hist_h,hist_w,CV_8UC3,Scalar(0,0,0));

 

  /// 直方图矩阵,二维
  MatND hist_base;

  /// 计算基准HSV图像的二维直方图
  calcHist( &hsv, 1, channels, Mat(), hist_base, 2, histSize, ranges, true, false );
  normalize( hist_base, hist_base, 0, hist_h, NORM_MINMAX, -1, Mat() );


  int h,s;
  //绘制二维直方图,需要绘制每个点
  for(h=0;h<h_bins;h++)
  {
   for(s=0;s<s_bins;s++)
   {
    int intensity=cvRound(hist_base.at<float>(h,s));
    rectangle(hist_image,Point(h*scale,s*scale),Point((h+1)*scale-1,(s+1)*scale-1),CV_RGB(intensity,intensity,intensity),
     CV_FILLED);
   }

  }

  namedWindow("H-S Histogram",1);
  imshow("H-S Histogram",hist_image);

  waitKey(0);
  return 0;


}

运行结果:

示例程序038--H-S二维直方图

相关文章:

  • 2021-12-23
  • 2022-01-17
  • 2021-04-05
  • 2022-01-18
  • 2022-12-23
  • 2021-11-19
  • 2021-08-29
  • 2021-10-10
猜你喜欢
  • 2021-11-17
  • 2022-01-07
  • 2021-04-25
  • 2021-11-06
  • 2022-12-23
  • 2021-11-12
  • 2022-01-08
相关资源
相似解决方案