【问题标题】:Histograms in OpencvOpencv中的直方图
【发布时间】:2011-12-01 20:00:24
【问题描述】:

我正在尝试在 MATLAB 中将这个特定命令实现到 opencv。我在 Linux Ubuntu 中工作。你能帮我找出 opencv 中的代码吗?

out_vector=hist(G_vector,0:17:255); G_vector 是一个大小为 1X100 的数组,代表图像的 1 个分量。

我正在使用以下代码

vector<Mat> rgb_planes;
       split(image,rgb_planes);
       int histSize = 255;

         /// Set the ranges ( for R,G,B) )
         float range[] = { 0,17, 255 } ;
         const float* histRange = { range };

         bool uniform = true; bool accumulate = false;

         Mat r_hist, g_hist, b_hist,g_hist1;

         /// Compute the histograms:
         calcHist( &rgb_planes[0], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate );
         calcHist( &rgb_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate );
         calcHist( &rgb_planes[2], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate );

         // Draw the histograms for R, G and B
         int hist_w = 400; int hist_h = 400;
         int bin_w = cvRound( (double) hist_w/histSize );

         Mat histImage( hist_w, hist_h, CV_8UC3, Scalar( 0,0,0) );

         /// Normalize the result to [ 0, histImage.rows ]
         normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
         normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );
         normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat() );

您能建议我应该在哪里进行更改

【问题讨论】:

  • 2小时内没有回复。请大家帮帮我
  • 为什么要改?什么地方出了错?我可以建议的一件事是0:17:255 意味着您的range[] 应该是{0,17,34,51,...}(即添加17 直到达到255)。

标签: matlab image-processing opencv histogram


【解决方案1】:

histSize 控制每个维度中的 bin 数量。如果您想要这些一维直方图中的 16 个 bin,请使用:

int histSize[] = { 16 };

或者我猜只是 int histSize = 16;会工作的。

将 bin 边界设置为 0 到 255,如下所示:

  float intensity_range = { 0, 256 }; //upper bound is exclusive
  const float * histRange[] = { intensity_range }; 

OpenCV 的 calcHist 提供了很少有人需要的可配置性。 Check out the method description and example code here.

【讨论】:

  • 代码确实可以编译。但我想将直方图中的值量化为 16 个 bin。
  • ut_vector=hist(G_vector,0:17:255) 所做的基本上是计算属于某个 bin 的像素值的数量。例如,如果值是 [15 20 30 45] 那么15 属于 0-15 箱,20 属于 16-31 等等
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-25
  • 1970-01-01
  • 1970-01-01
  • 2016-07-12
  • 2015-07-27
相关资源
最近更新 更多