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

烈酒焚心 提交于 2019-12-09 23:58:09

问题


I want to segment car plate to get separate characters. I found some article, where such segmentation performed using brightness histograms (as i understand - sum of all non-zero pixels).

How can i calculate such histogram? I would really appreciate for any help!


回答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;

}

Or use reduce as suggested by Berak, either calling

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

or

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

out is a cv::Mat, and it will have a single row.



来源:https://stackoverflow.com/questions/28601499/how-do-i-compute-the-brightness-histogram-aggregated-by-column-in-opencv-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!