随手翻了翻之前推薦的那本OpenCV2的教程,突然發現了之前看圖像進行中的像素直方圖的實作教程,原本就打算找時間自己實作的,沒想到opencv是有這個功能的
而且這個功能可以輔助很多的圖像處理和識别的算法過程,是值得看一看的
OpenCV對于像素直方圖的統計有這麼一個函數:
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 ar-
ray channels are numerated from 0 to arrays[0].channels()-1 , the second ar-
ray 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 L0 of the 0-th histogram bin and the upper (exclusive)
boundary UhistSize[i]−1 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:
L0 , U0 = L1 , U1 = L2 , ..., UhistSize[i]−2 = LhistSize[i]−1 , UhistSize[i]−1 . The array
elements, that are not between L0 and UhistSize[i]−1 , 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 <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
class Histogram
{
private:
int histSize[1];//number of bins
float hranges[2];//min and max pixel value
const float* ranges[1];
int channels[1];//only 1 channel used here
public:
Histogram()
{
//Prepare arguments for 1D histogram
histSize[0] = 256;
hranges[0] = 0.0;
hranges[1] = 255.0;
ranges[0] = hranges;
channels[0] = 0;//by default , we look at channel 0
}
cv::MatND getHistogram(const cv::Mat &image)
{
cv::MatND hist;
//Compute histogram
cv::calcHist(&image,
1,
channels,
cv::Mat(),
hist,
1,
histSize,
ranges
);
return hist;
}
//Compute the 1D histogram and returns an image of it
cv::Mat getHistogramImage(const cv::Mat &image)
{
//compute histogram first
cv::MatND hist = getHistogram(image);
//Get min and max bin values
double maxVal = 0;
double minVal = 0;
cv::minMaxLoc(hist,&minVal,&maxVal);
//Image on which to display histogram
cv::Mat histImg(histSize[0],histSize[0],CV_8U,cv::Scalar(255));
//set highest point at 90% of nbins
int hpt = static_cast<int>(0.9*histSize[0]);
//Draw a vertical line for each bin
for( int h = 0;h<histSize[0];h++)
{
float binVal = hist.at<float>(h);
int intensity = static_cast< int >(binVal * hpt
/ maxVal);
//This function draw a line between 2 points
cv::line(histImg,cv::Point(h,histSize[0]),
cv::Point(h,histSize[0] - intensity),
cv::Scalar::all(0));
}
return histImg;
}
};
int main()
{
//Read input image
cv::Mat image = cv::imread("1.png",0);//open in b&w
//the histogram object
Histogram h;
//Compute the histogram
/*cv::MatND histo = h.getHistogram(image);
for(int i = 0;i<256;i++)
{
cout<<"Value "<<i<<"="<<histo.at<float>(i) <<endl;
}*/
cv::namedWindow("Histogram");
cv::imshow("Histogram",h.getHistogramImage(image));
cv::waitKey(0);
return 0;
}
需要注意的是imread這個函數後面的那個參數,不清楚的可以查閱官方的手冊~
截圖:

上面的代碼隻是讀取的灰階圖像回報的1D的直方圖,那麼如果我是3通道的RGB/BGR圖像呢?隻需要做相應的修改然後函數便會傳回3*256的一個Mat值
private:
int histSize[3];//number of bins
float hranges[2];//min and max pixel value
const float* ranges[3];
int channels[3];//only 1 channel used here
public:
Histogram()
{
//Prepare arguments for 1D histogram
histSize[0] = 256;histSize[1] = 256;histSize[2] = 256;
hranges[0] = 0.0;
hranges[1] = 255.0;
ranges[0] = hranges;ranges[1] =hranges;
ranges[2] =hranges;
channels[0] = 0;channels[1] = 1;channels[2] = 2;
}
cv::MatND getHistogram(const cv::Mat &image)
{
cv::MatND hist;
//Compute histogram
cv::calcHist(&image,
1,
channels,
cv::Mat(),
hist,
3,
histSize,
ranges
);
return hist;
}
但是在3通道的直方圖計算過程中,有可能會覺得計算量過大了,同樣可以使用稀疏矩陣(sparse matrix),calcHist同樣支援
cv::SparseMat getSparseHistogram(const cv::Mat &image)
{
cv::SparseMat hist(3,histSize,CV_32F);
//Compute histogram
cv::calcHist(&image,
1,
channels,
cv::Mat(),
hist,
3,
histSize,
ranges
);
return hist;
}
接下來一段時間,就要回歸水魚的比賽了,OpenCV就要放一放
打算晚上寫一下北大提供的PoseToPose函數的分析指導,由于自己的學識有限,很難分析全面,真的希望有人可以來指正我的錯誤~
張巍骞
2012-4-10