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

How do I compute the brightness histogram aggregated by column in OpenCV C++

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

如何计算这样的直方图?如果有任何帮助,我将不胜感激!

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);

out 是一个 cv::Mat,它只有一行。