如何计算OpenCV C ++中按列聚合的亮度直方图

时间:2015-02-19 08:22:30

标签: c++ opencv histogram ocr

我想将车牌分段以获得单独的字符。 我发现了一些文章,其中使用亮度直方图执行此类分割(据我所知 - 所有非零像素的总和)。

enter image description here

我如何计算这样的直方图?我真的很感激你的帮助!

1 个答案:

答案 0 :(得分:1)

std::vector<int> computeColumnHistogram(const cv::Mat& in) {

  std::vector<int> histogram(in.cols,0); //Create a zeroed histogram of the necessary size
  for (int y = 0; y < in.rows; y++) {
    p_row = in.ptr(y); ///Get a pointer to the y-th row of the image
    for (int x = 0; x < in.cols; x++)
      histogram[x] += p_row[x]; ///Update histogram value for this image column
  }

  //Normalize if you want (you'll get the average value per column): 
  //  for (int x = 0; x < in.cols; x++)
  //    histogram[x] /= in.rows;

  return histogram;

}

或者按照Berak的建议使用reduce,或者调用

cv::reduce(in, out, 0, CV_REDUCE_AVG);

cv::reduce(in, out, 0, CV_REDUCE_SUM, CV_32S);

outcv::Mat,它只有一行。