-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCalcHistogram.h
More file actions
115 lines (95 loc) · 2.44 KB
/
CalcHistogram.h
File metadata and controls
115 lines (95 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#ifndef CalcHistogram_H
#define CalcHistogram_H
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace std;
using namespace cv;
class Histogram1D
{
private:
int histSize[1]; // 项的数量
float hranges[2]; // 统计像素的最大值和最小值
const float* ranges[1];
int channels[1]; // 仅计算一个通道
public:
Histogram1D()
{
// 准备1D直方图的参数
histSize[0] = 64;
hranges[0] = 0.0f;
hranges[1] = 256.0f;
ranges[0] = hranges;
channels[0] = 0;
}
MatND getHistogram(const Mat &image)
{
MatND hist;
// 计算直方图
calcHist(&image,// 要计算图像的
1, // 只计算一幅图像的直方图
channels, // 通道数量
Mat(), // 不使用掩码
hist, // 存放直方图
1, // 1D直方图
histSize, // 统计的灰度的个数
ranges); // 灰度值的范围
return hist;
}
Mat getHistogramImage(const Mat &image)
{
MatND hist = getHistogram(image);
// 最大值,最小值
double maxVal = 0.0f;
double minVal = 0.0f;
minMaxLoc(hist, &minVal, &maxVal);
//显示直方图的图像
Mat histImg(histSize[0], histSize[0], CV_8U, Scalar(255));
// 设置最高点为nbins的90%
int hpt = static_cast<int>(0.9 * histSize[0]);
//每个条目绘制一条垂直线
for (int h = 0; h < histSize[0]; h++)
{
float binVal = hist.at<float>(h);
int intensity = static_cast<int>(binVal * hpt / maxVal);
// 两点之间绘制一条直线
line(histImg, Point(h, histSize[0]), Point(h, histSize[0] - intensity), Scalar::all(0));
}
return histImg;
}
void mat2GrayHist(Mat &image, int grayHist[64]);
};
class CalcHistogram
{
private:
int histSize[3]; //直方图项的数量
float hranges[2]; //h通道像素的最小和最大值
float sranges[2];
float vranges[2];
const float *ranges[3]; //各通道的范围
int channels[3]; //三个通道
int dims;
public:
CalcHistogram(int hbins = 4, int sbins = 4, int vbins = 4)
{
histSize[0] = hbins;
histSize[1] = sbins;
histSize[2] = vbins;
hranges[0] = 0; hranges[1] = 256;
sranges[0] = 0; sranges[1] = 256;
vranges[0] = 0; vranges[1] = 256;
ranges[0] = hranges;
ranges[1] = sranges;
ranges[2] = vranges;
channels[0] = 0;
channels[1] = 1;
channels[2] = 2;
dims = 3;
}
Mat getHistogram(const Mat &image);
void getHistogramImage(const Mat &image);
void mat2RGBHist(Mat &image, int hsvHist[64]);
};
float calcCosSimi(int h0[64], int h1[64]);
#endif // CalcHistogram_H