-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHistogram_Equalization_Matching.cpp
180 lines (160 loc) · 5.75 KB
/
Histogram_Equalization_Matching.cpp
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
void Histogram::Equalize(KImageColor& src, KImageColor& out){
int nRow = src.Row();
int nCol = src.Col();
out.Create(nRow, nCol);
Histogram normalized;
normalized.collect(src).toCumulativeProb();
for(int i = 0; i<nRow; i++){
for(int j = 0; j<nCol; j++){
out._ppA[i][j].r = (unsigned char)(normalized.chist.R[src._ppA[i][j].r] * 255 + 0.5);
out._ppA[i][j].g = (unsigned char)(normalized.chist.G[src._ppA[i][j].g] * 255 + 0.5);
out._ppA[i][j].b = (unsigned char)(normalized.chist.B[src._ppA[i][j].b] * 255 + 0.5);
}
}
}
Histogram& Histogram::Match(KImageColor& icTarget, KImageColor& icSource, KImageColor& out){
Histogram targetHist, sourceHist;
targetHist.collect(icTarget).toCumulativeProb();
sourceHist.collect(icSource).toCumulativeProb();
colorHist& t = targetHist.chist;
colorHist& s = sourceHist.chist;
auto argminfinder = [&](double t, std::vector<double>& s, unsigned char* map){
double dMin = INFINITY, dDiff;
for(int i=0; i<=255; i++)
if((dDiff = _DIFF(t, s[i])) < dMin)
{
dMin = dDiff;
if(map)
*map = i;
}
};
unsigned char mapped_r[255] = {0,};
unsigned char mapped_g[255] = {0,};
unsigned char mapped_b[255] = {0,};
for(int i = 0; i<=255; i++){
argminfinder(t.R[i], s.R, &mapped_r[i]);
argminfinder(t.G[i], s.G, &mapped_g[i]);
argminfinder(t.B[i], s.B, &mapped_b[i]);
}
int nRow = icTarget.Row();
int nCol = icTarget.Col();
out.Create(nRow, nCol);
for(int i = 0; i<nRow;i++){
for(int j = 0; j<nCol; j++){
out._ppA[i][j].r = mapped_r[icTarget._ppA[i][j].r];
out._ppA[i][j].g = mapped_g[icTarget._ppA[i][j].g];
out._ppA[i][j].b = mapped_b[icTarget._ppA[i][j].b];
}
}
return *this;
}
Histogram& Histogram::collect(const KImageColor& Img){
vector<double> r(256, 0);
vector<double> g(256, 0);
vector<double> b(256, 0);
int nRow = Img.Row();
int nCol = Img.Col();
for(int i = 0; i<nRow; i++){
for(int j = 0; j<nCol; j++){
r[Img._ppA[i][j].r]++;
g[Img._ppA[i][j].g]++;
b[Img._ppA[i][j].b]++;
}
}
atrb = "RGB";
size = Img.Size();
chist.R.swap(r); chist.G.swap(g); chist.B.swap(b);
return *this;
}
Histogram& Histogram::collect(KImageGray& Img){
ghist.I.reserve(256); std::fill(ghist.I.begin(), ghist.I.end(), 0);
int nRow = Img.Row();
int nCol = Img.Col();
for(int i = 0; i<nRow; i++){
for(int j = 0; j<nCol; j++){
ghist.I[Img._ppA[i][j]]++;
}
}
atrb = "GRAY";
size = Img.Size();
return *this;
}
Histogram& Histogram::toCumulativeProb(){
if(this->atrb == "RGB"){
for(int i = 1; i<=255; i++){
chist.R[i] += chist.R[i-1];
chist.G[i] += chist.G[i-1];
chist.B[i] += chist.B[i-1];
}
for(int i = 0; i<=255; i++){
chist.R[i] /= size;
chist.G[i] /= size;
chist.B[i] /= size;
}
}
else if(this->atrb == "GRAY"){
for(int i = 1; i<=255; i++){
ghist.I[i] += ghist.I[i-1];
}
for(int i = 0; i<=255; i++){
ghist.I[i] /= size;
}
}
return *this;
}
//===========================================//
void MainFrame::on_pushHistogramEqualization_clicked()
{
KImageColor icMain;
if(_q_pFormFocused != 0 && _q_pFormFocused->ImageColor().Address())
icMain = _q_pFormFocused->ImageColor();
else
return;
Histogram Equalizer;
KImageColor icEqualized;
Equalizer.Equalize(icMain, icEqualized); //Histogram Equliaze
ImgForm<KImageColor>::Create(*this, "Histogram Equalized", icEqualized);
}
void MainFrame::on_pushHistogramMatching_clicked()
{
QFileDialog::Options q_Options = QFileDialog::DontResolveSymlinks |
QFileDialog::DontUseNativeDialog;
QString q_stFile = QFileDialog::getOpenFileName(this,
tr("Select a Target Image"), "./data",
"Image Files(*.ppm *.pgm *.tif)", 0, q_Options);
ImageForm* source;
if(q_stFile.length() == 0)
return;
source = ImgForm<QString>::Create(*this, "Target Image", q_stFile);
if(source->Atrb() != "RGB"){ //Check if not Color Image
this->CloseImageForm(source); //Close recently opened ImageForm
if(ui->listWidget->isVisible() == false)
on_buttonShowList_clicked();
ui->listWidget->addItem(QString("Select only Image color."));
return;
}
source->show();
KImageColor icSource = source->ImageColor();
q_stFile = QFileDialog::getOpenFileName(this, tr("Select a Source Image"), "./data",
"Image Files(*.bmp *.ppm *.pgm *.tif)", 0, q_Options);
if(q_stFile.length()==0){
this->CloseImageForm(source); //Close source image
return;
}
ImageForm* target;
target = ImgForm<QString>::Create(*this, "Source Image", q_stFile);
if(target->Atrb() != "RGB"){ //Check if not Color Image
this->CloseImageForm(source);
this->CloseImageForm(target); //Close recenty opened ImageForm
if(ui->listWidget->isVisible() == false)
on_buttonShowList_clicked();
ui->listWidget->addItem(QString("Select only Image color."));
return;
}
target->show();
KImageColor icTarget = target->ImageColor();
Histogram Matching;
KImageColor icMatched;
Matching.Match(icTarget, icSource, icMatched); //Histogram Matching
ImgForm<KImageColor>::Create(*this, "Histogram Matched", icMatched);
}