歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> OpenCV2 計算直方圖

OpenCV2 計算直方圖

日期:2017/3/1 10:14:15   编辑:Linux編程

我們仍然如前例一樣,構建一個類來處理得到直方圖。

我們來看看基本的類定義

#ifndef HISTOGRAM_H_
#define HISTOGRAM_H_
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <vector>

class Histogram {
private:
int histSize[1];
float hrangee[2];
const float* ranges[1];
int channels[1];

protected:
cv::Mat getHistogram(const cv::Mat&);

public:
Histogram();
cv::Mat getHistogramImage(const cv::Mat&, int channel = 0);
};

#endif /* HISTOGRAM_H_ */

類的實現

#include "Histogram.h"

Histogram::Histogram() {
histSize[0] = 256;
hrangee[0] = 0.0;
hrangee[1] = 255.0;
ranges[0] = hrangee;
channels[0] = 0;
}


cv::Mat Histogram::getHistogram(const cv::Mat& image){
cv::MatND hist;
cv::calcHist(&image, 1, channels, cv::Mat(), hist, 1, histSize, ranges);
return hist;
}


cv::Mat Histogram::getHistogramImage(const cv::Mat& image, int channel){
std::vector<cv::Mat> planes;
cv::split(image,planes);
cv::Scalar color;
if(planes.size() == 1){
channel = 0;
color = cv::Scalar(0,0,0);
}else{
color = cv::Scalar(channel==0?255:0, channel==1?255:0, channel==2?255:0);
}
cv::MatND hist = getHistogram(planes[channel]);
double maxVal = 0;
double minVal = 0;
cv::minMaxLoc(hist, &minVal, &maxVal, 0, 0);
cv::Mat histImg(histSize[0], histSize[0], CV_8UC3, cv::Scalar(255,255,255));
int hpt = static_cast<int>(0.9*histSize[0]);
for(int h=0; h<histSize[0]-1; h++){
float binVal = hist.at<float>(h);
float binVal2 = hist.at<float>(h+1);
int intensity = static_cast<int>(binVal*hpt/maxVal);
int intensity2 = static_cast<int>(binVal2*hpt/maxVal);
cv::line(histImg, cv::Point(h,histSize[0]-intensity),
cv::Point(h,histSize[0]-intensity2), color);
}
return histImg;
}

這裡面要用的幾個函數在這裡列出:

C++: void minMaxLoc(const SparseMat& src, double* minVal, double* maxVal, int* minIdx=0, int* maxIdx=0)

其作用是尋找一個矩陣中最大和最小值,並得到它們的位置

C++: void split(const Mat& mtx, vector<Mat>& mv)

把一個多通道矩陣分成幾個單通道矩陣,在這裡主要用於計算各通道的直方圖

C++: void calcHist(const Mat* arrays, int narrays, const int* channels, InputArray mask, OutputArray hist, int dims, const int* histSize, const float** ranges, bool uniform=true, bool accumulate=false )

計算直方圖,各參數含義可在參考文檔中查到

Parameters:

  • arrays – Source arrays. They all should have the same depth, CV_8U or CV_32F , and the same size. Each of them can have an arbitrary number of channels.
  • narrays – Number of source arrays.
  • channels – List of the dims channels used to compute the histogram. The first array channels are numerated from 0 to arrays[0].channels()-1 , the second array channels are counted from arrays[0].channels() to arrays[0].channels() + arrays[1].channels()-1, and so on.
  • mask – Optional mask. If the matrix is not empty, it must be an 8-bit array of the same size as arrays[i] . The non-zero mask elements mark the array elements counted in the histogram.
  • hist – Output histogram, which is a dense or sparse dims -dimensional array.
  • dims – Histogram dimensionality that must be positive and not greater than CV_MAX_DIMS (equal to 32 in the current OpenCV version).
  • histSize – Array of histogram sizes in each dimension.
  • ranges – Array of the dims arrays of the histogram bin boundaries in each dimension. When the histogram is uniform ( uniform =true), then for each dimension i it is enough to specify the lower (inclusive) boundary of the 0-th histogram bin and the upper (exclusive) boundary for the last histogram bin histSize[i]-1 . That is, in case of a uniform histogram each of ranges[i] is an array of 2 elements. When the histogram is not uniform ( uniform=false ), then each of ranges[i] contains histSize[i]+1 elements: . The array elements, that are not between and , are not counted in the histogram.
  • uniform – Flag indicatinfg whether the histogram is uniform or not (see above).
  • accumulate – Accumulation flag. If it is set, the histogram is not cleared in the beginning when it is allocated. This feature enables you to compute a single histogram from several sets of arrays, or to update the histogram in time.

我們來看看主程序

#include "Histogram.h"

int main(){
cv::Mat image = cv::imread("E:/Image/Lena.jpg");
Histogram h;

cv::namedWindow("Red");
cv::namedWindow("Blue");
cv::namedWindow("Green");
cv::namedWindow("Original");
cv::imshow("Original",image);
cv::imshow("Red",h.getHistogramImage(image,2));
cv::imshow("Green",h.getHistogramImage(image,1));
cv::imshow("Blue",h.getHistogramImage(image));

cv::waitKey(0);
return 0;
}

運行效果如下:

Copyright © Linux教程網 All Rights Reserved