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

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

 

// 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二维直方图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值