-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColorTransformer.cpp
350 lines (271 loc) · 8.24 KB
/
ColorTransformer.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "ColorTransformer.h"
int ColorTransformer::ChangeBrighness(const Mat& sourceImage, Mat& destinationImage, short b)
{
if (sourceImage.empty()) {
return 0;
}
// lay hang, cot cua anh goc
int rows = sourceImage.rows;
int cols = sourceImage.cols;
// lay chieu dai cua 1 dong anh goc
auto rowStepSrc = sourceImage.step[0];
// lay chieu rong cua 1 pixel anh goc
auto colStepSrc = sourceImage.step[1];
// lay con tro data sourceImage
uchar* pDataSrc = (uchar*)sourceImage.data;
// tao anh destinationImage
destinationImage = sourceImage.clone();
// lay con tro data destinationImage
uchar* pDataDest = (uchar*)destinationImage.data;
for (int y = 0; y < rows; y++, pDataSrc += rowStepSrc, pDataDest += rowStepSrc) {
uchar* pRowSrc = pDataSrc;
uchar* pRowDest = pDataDest;
for (int x = 0; x < cols; x++, pRowSrc += colStepSrc, pRowDest += colStepSrc) {
// loc qua tung kenh mau
for (int i = 0; i < colStepSrc; ++i)
{
// su dung int tranh tran so
int newPixel = b + pRowSrc[i];
if (newPixel > 255) {
pRowDest[i] = 255;
}
else if (newPixel < 0) {
pRowDest[i] = 0;
}
else {
pRowDest[i] = newPixel;
}
}
}
}
return 1;
}
int ColorTransformer::ChangeContrast(const Mat& sourceImage, Mat& destinationImage, float c)
{
if (sourceImage.empty()) {
return 0;
}
// lay hang, cot cua anh goc
int rows = sourceImage.rows;
int cols = sourceImage.cols;
// lay chieu dai cua 1 dong anh goc
auto rowStepSrc = sourceImage.step[0];
// lay chieu rong cua 1 pixel anh goc
auto colStepSrc = sourceImage.step[1];
// lay con tro data sourceImage
uchar* pDataSrc = (uchar*)sourceImage.data;
// tao anh dich
destinationImage = sourceImage.clone();
// lay con tro data destinationImage
uchar* pDataDest = (uchar*)destinationImage.data;
for (int y = 0; y < rows; y++, pDataSrc += rowStepSrc, pDataDest += rowStepSrc) {
uchar* pRowSrc = pDataSrc;
uchar* pRowDest = pDataDest;
for (int x = 0; x < cols; x++, pRowSrc += colStepSrc, pRowDest += colStepSrc) {
// loc qua tung kenh mau
for (int i = 0; i < colStepSrc; ++i)
{
// su dung int tranh tran so
int newPixel = c * pRowSrc[i];
if (newPixel > 255) {
pRowDest[i] = 255;
}
else if (newPixel < 0) {
pRowDest[i] = 0;
}
else {
pRowDest[i] = newPixel;
}
}
}
}
return 1;
}
int ColorTransformer::CalcHistogram(const Mat& sourceImage, Mat& histMatrix)
{
if (sourceImage.empty()) {
return 0;
}
// lay hang, cot cua anh goc
int rows = sourceImage.rows;
int cols = sourceImage.cols;
// lay chieu dai cua 1 dong anh goc
auto rowStepSrc = sourceImage.step[0];
// lay chieu rong cua 1 pixel anh goc
auto colStepSrc = sourceImage.step[1];
// lay con tro data sourceImage
uchar* pDataSrc = (uchar*)sourceImage.data;
//Khoi tao ma tran histogram toan bo so 0 voi kich thuoc channels x 256, kieu so thuc
histMatrix = Mat::zeros(colStepSrc, 256, CV_32FC1);
// lay chieu dai cua 1 dong ma tran histogram
auto widthStepHistMatrix = histMatrix.step[0];
// lay con tro data ma tran histogram
uchar* pDataHistMatrix = (uchar*)histMatrix.data;
// khoi tao mang con tro chua cac con tro tro toi tung row
float** pChannel = new float* [colStepSrc];
// gan tung con tro tro toi tung dong cua histogram matrix vao con tro pChannel
for (int i = 0; i < colStepSrc; ++i, pDataHistMatrix += widthStepHistMatrix) {
pChannel[i] = (float*)pDataHistMatrix;
}
for (int y = 0; y < rows; y++, pDataSrc += rowStepSrc) {
uchar* pRowSrc = pDataSrc;
for (int x = 0; x < cols; x++, pRowSrc += colStepSrc) {
// loc qua tung kenh mau
for (int i = 0; i < colStepSrc; ++i)
{
// tang so luong mau i len 1
uchar indexColor = pRowSrc[i];
pChannel[i][indexColor] += 1;
}
}
}
return 1;
}
int ColorTransformer::HistogramEqualization(const Mat& sourceImage, Mat& destinationImage)
{
if (sourceImage.empty()) {
return 0;
}
// ma tran histogram
Mat histMatrix;
// tinh histogram cho anh
int result = this->CalcHistogram(sourceImage, histMatrix);
if (!result) {
return 0;
}
// lay so kenh mau
int nChannels = histMatrix.rows;
// tim tong so pixel tren anh
int totalPixel = 0;
// con tro dong so 0
auto pRow = histMatrix.ptr<float>(0);
for (int x = 0; x < 256; ++x, pRow++) {
totalPixel += *pRow;
}
// tao bang phan phoi tich luy va thay doi gia tri
Mat convertMatrix = histMatrix.clone();
// cong thuc: https://www.math.uci.edu/icamp/courses/math77c/demos/hist_eq.pdf
// chay qua tung kenh mau
for (int y = 0; y < nChannels; ++y) {
// tong tich luy
float sumValue = 0;
// con tro dong y
auto pRowCdf = convertMatrix.ptr<float>(y);
for (int x = 0; x < 256; ++x, pRowCdf++) {
// sum tich luy so luong mau i
sumValue += *pRowCdf;
// gia tri moi = P (tong tich luy) * ( L - 1 )
// L = 256
*pRowCdf = floor(sumValue / totalPixel * 255);
}
}
// tao mot ma tran histogram va gan gia tri = 0
Mat equalizeMatrix = histMatrix.clone();
equalizeMatrix.setTo(0);
// anh xa lai ma tran histogram
for (int y = 0; y < nChannels; ++y) {
// loc qua tung kenh mau
auto pRowOrigin = histMatrix.ptr<float>(y);
auto pRowCdf = convertMatrix.ptr<float>(y);
auto pRowEqualize = equalizeMatrix.ptr<float>(y);
// voi moi gia tri mau x, ta tim mau tuong ung sau khi can bang, tiep theo cong vao gia tri dang co
for (int x = 0; x < 256; ++x) {
// mau tuong ung voi mau x
int convertColor = pRowCdf[x];
// so luong mau x co trong anh
int freqColor = pRowOrigin[x];
// cap nhat lai so luong mau bien doi
pRowEqualize[convertColor] += freqColor;
}
}
// thuc hien luu anh vao ma tran destinationImage
this->DrawHistogram(equalizeMatrix, destinationImage);
return 1;
}
int ColorTransformer::DrawHistogram(const Mat& histMatrix, Mat& histImage)
{
if (histMatrix.empty()) {
return 0;
}
int nChannels = histMatrix.rows;
//chuan hoa lai luoc do theo chieu cao co san
// tao mang luu gia tri max cua cac Channel
int* maxCount = new int[nChannels] {};
for (int i = 0; i < nChannels; ++i) {
maxCount[0] = 0;
}
// thuc hien tim max
for (int y = 0; y < nChannels; ++y) {
// con tro dong y
auto pRow = histMatrix.ptr<float>(y);
for (int x = 0; x < 256; ++x, pRow++) {
if (maxCount[y] < *pRow) {
maxCount[y] = *pRow;
}
}
}
// sao chep ma tran histogram
Mat copyHistMatrix = histMatrix.clone();
// buoc chuan hoa theo chieu cao max va heightSize
for (int y = 0; y < nChannels; ++y) {
// con tro dong y
auto pRowCopy = copyHistMatrix.ptr<float>(y);
for (int x = 0; x < 256; ++x, pRowCopy++) {
// gia tri moi = gia tri cu / max * chieu cao luoc do
*pRowCopy = *pRowCopy / maxCount[y] * HEIGHT_SIZE;
}
}
// chieu rong moi gia tri mau
int widthColSize;
// chieu cao cua cua so
int heightWindow = PADDING_SIZE * 2 + HEIGHT_SIZE;
// toa do truc y khoi dau
int yaxixPixel = PADDING_SIZE + HEIGHT_SIZE;
// mau co so
Scalar baseColor[3];
if (nChannels > 1) {
// 3 channels
widthColSize = WIDTH_COL_SIZE_3C;
baseColor[0] = Scalar(0, 0, 255); // red
baseColor[1] = Scalar(0, 255, 0); // green
baseColor[2] = Scalar(255, 0, 0); // blue
}
else {
// gray scale
widthColSize = WIDTH_COL_SIZE_1C;
baseColor[0] = Scalar(128, 128, 128); // gray
}
// chieu rong cua cua so
int widthWindow = (PADDING_SIZE + 256 * widthColSize) * nChannels + PADDING_SIZE;
// khoi tao anh histogram co mau den
histImage = Mat::zeros(heightWindow, widthWindow, CV_8UC3);
// lap qua tung kenh mau ( tung dong ma tran histogram )
for (int y = 0; y < nChannels; ++y) {
// con tro dong y
auto pRowCopy = copyHistMatrix.ptr<float>(y);
// chieu cao cua mau thu 0 trong kenh mau y
float heightPixel = *pRowCopy;
// tao ra 1 diem co toa do mau dau tien cua kenh
Point prePoint = Point(PADDING_SIZE + (PADDING_SIZE + 256 * widthColSize) * y,
yaxixPixel - round(heightPixel));
// di chuyen con tro toi mau tiep theo
pRowCopy++;
for (int x = 1; x < 256; ++x, pRowCopy++) {
// tao ra diem ket thuc cua duong thang
Point nextPoint = prePoint;
nextPoint.x += widthColSize;
nextPoint.y = yaxixPixel - round(*pRowCopy);
// ve duong thang
line(histImage, prePoint, nextPoint, baseColor[y]);
// set lai diem
prePoint = nextPoint;
}
}
return 1;
}
ColorTransformer::ColorTransformer()
{
}
ColorTransformer::~ColorTransformer()
{
}