-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
314 lines (277 loc) · 9.74 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QFileDialog>
#include <QSlider>
#include <chrono>
#include <omp.h>
#include <math.h>
void MeanCovMapCalculate(float *data, int width, int height, float *corrIP, int radius);
void f_EPMFilter(const uchar *srcData, uchar *dstData, int nWidth, int nHeight, int nStride, int radius, float delta, float light);
void f_EPMFilterOneChannel(const uchar *srcData, uchar *dstData, int nWidth, int nHeight, int nStride, int radius, float delta, float light);
//参考 https://blog.csdn.net/Trent1985/article/details/80802144#commentsedit
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
initWidgets();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::initWidgets()
{
connect(ui->pushButton, &QPushButton::clicked, this, [=] {
QFileDialog* loadFileDialog = new QFileDialog(this);
loadFileDialog->setWindowTitle(QString("Select An Picture"));
loadFileDialog->setDirectory("E:/Rison/Pictures");
//设置可以选择多个文件,默认为只能选择一个文件QFileDialog::ExistingFiles
//fileDialog->setFileMode(QFileDialog::ExistingFiles);
loadFileDialog->setNameFilter(tr("Images(*.png *.jpg *.jpeg *.bmp)"));
if(loadFileDialog->exec())
{
auto fileNames = loadFileDialog->selectedFiles();
srcImg = QImage(fileNames.first());
//srcImg = QImage("E:/Rison/Pictures/fff.png");
outImg = srcImg.copy();
width = srcImg.width();
height = srcImg.height();
qDebug() << "byte count " << srcImg.byteCount();
ui->input->setPixmap(QPixmap::fromImage(srcImg));
}
loadFileDialog->deleteLater();
});
QSlider *slider = new QSlider(this);
slider->setGeometry(320, 270, 200, 20);
slider->setOrientation(Qt::Horizontal);
slider->setMinimum(15);
slider->setMaximum(50);
connect(slider, &QSlider::valueChanged, this, [=](int value) {
///////////////////////////////////////////////
int radius = qMax(srcImg.width(), srcImg.height())*0.02f;
auto beginClock = std::chrono::system_clock::now();
f_EPMFilter(srcImg.bits(), outImg.bits(), width, height, width, radius, value/10000.0f,1.5f);
auto endClock = std::chrono::system_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(endClock - beginClock);
qDebug("cost time: %.3lf mms", duration.count() / 1000.0f);//输出图像处理花费时间信息
///////////////////////////////////////////////
ui->output->setPixmap(QPixmap::fromImage(outImg));
});
}
//使用积分图用于快速计算均值,进而计算方差
//参考 https://blog.csdn.net/fb_help/article/details/88534427
void MeanCovMapCalculate(float*data, int width, int height, float *corrIP, int radius)
{
//sump需要比原来的多1,因为用(1,1)处的值表示(0,0)的值,(x+1,y+1)表示(0,0)~(x,y)直接的总和
//qDebug() <<"--->"<<__FUNCTION__ <<":"<<__LINE__ <<" size: "<<width<<"x"<<height;
int mapw=width+1;
int maph=height+1;
float *summap = (float*)malloc(sizeof(float) * mapw * maph);
memset(summap, 0, sizeof(float) * mapw * maph);
//qDebug() <<"--->"<<__FUNCTION__ <<":"<<__LINE__;
for (int x=0; x<width; x++)
{
for (int y=0; y<height; y++)
{
int index = mapw*y+x;
// x+1,y+1 x+1,y x,y+1 x,y x,y
//summap[mapw*(x+1)+y+1] = summap[mapw*(x+1)+y] + summap[mapw*x+y+1] - summap[mapw*x+y] + data[width*x+y];
//qDebug() << "--->x"<<(x+1)<<",y"<<(y+1) <<"|"<< summap[mapw*(x+1)+y+1] << summap[mapw*(x+1)+y] << summap[mapw*x+y+1] << summap[mapw*x+y] << data[width*x+y];
summap[index+mapw+1] = summap[index+mapw] + summap[index+1] - summap[index] + data[index-y];
}
}
//qDebug() <<"--->"<<__FUNCTION__ <<":"<<__LINE__;
int left=radius/2;
int right=radius-left;
int top = radius/2;
int bottom = radius-top;
for (int x=0; x<width; ++x) {
for (int y=0; y<height; ++y) {
int x1 = x-left;
if(x1<0){x1=0;}
int x2 = x+right;
if(x2>width){x2=width;}
int y1 = y-top;
if(y1<0){y1=0;}
int y2=y+bottom;
if(y2>height){y2=height;}
int n=(x2-x1+1)*(y2-y1+1);
//qDebug() <<"--->" <<(mapw*y2+x2)<<",total:"<<(mapw * maph);
corrIP[width*y+x] = (summap[mapw*y2+x2]-summap[mapw*y2+x1]-summap[mapw*y1+x2]+summap[mapw*y1+x1])/n;
}
}
//qDebug() <<"--->"<<__FUNCTION__ <<":"<<__LINE__;
free(summap);
//qDebug() <<"--->"<<__FUNCTION__ <<":"<<__LINE__;
}
unsigned char inline CLIP3(int v, int min, int max)
{
if (v < min) { return min; }
else if (v > max) { return max; }
else return v;
}
float inline light1(float valueDividedBy255, float beta)
{
return log(valueDividedBy255*(beta - 1) + 1) / log(beta);
}
//Edge Preserved mean filter
int f_EPMFilter(unsigned char* srcData, int width, int height, int radius, float delta, float light)
{
int size = width*height;
float *data = (float*)malloc(sizeof(float) * size);
float *meanI = (float*)malloc(sizeof(float) * size);
float *corrI = (float*)malloc(sizeof(float) * size);
//使用积分图计算平均值
for (int i = 0; i < size; i++)
{
data[i] = (float)srcData[i] / 255.0f;
}
MeanCovMapCalculate(data, width, height, meanI, radius);
//曲线美白
for (int i = 0; i < size; i++)
{
srcData[i] = (uchar)CLIP3(light1(data[i], light) * 255.0f, 0, 255);
}
//计算方差
for (int i = 0; i < size; i++)
{
data[i] *= data[i];
}
MeanCovMapCalculate(data, width, height, corrI, radius);
for (int i = 0; i < size; i++)
{
corrI[i] = corrI[i] - meanI[i] * meanI[i];
}
//均方差磨皮,暂存到highPass里面,等待处理
for (int i = 0; i < size; i++)
{
float t = meanI[i] + (corrI[i] * (srcData[i] / 255.0f - meanI[i]) / (corrI[i] + delta));
srcData[i] = (uchar)CLIP3(t * 255.0f, 0, 255);
}
free(data);
free(meanI);
free(corrI);
return 0;
}
//3通道处理
void f_EPMFilter(const uchar* srcData, uchar* dstData, int nWidth, int nHeight, int nStride, int radius, float delta, float light)
{
Q_UNUSED(nStride);
if (srcData == nullptr) { return; }
int64_t rBytesCount = sizeof(uchar) * nWidth * nHeight;
uchar* rData = (uchar*)malloc(rBytesCount);
uchar* gData = (uchar*)malloc(rBytesCount);
uchar* bData = (uchar*)malloc(rBytesCount);
const uchar* pSrc = srcData;
uchar* pR = rData;
uchar* pG = gData;
uchar* pB = bData;
for (int y = 0; y < nHeight; y++)
{
for (int x = 0; x < nWidth; x++)
{
*pR = pSrc[0];
*pG = pSrc[1];
*pB = pSrc[2];
pR++;
pG++;
pB++;
pSrc += 4;
}
}
//并行处理
#pragma omp parallel sections
{
#pragma omp section
f_EPMFilter(rData, nWidth, nHeight, radius, delta, light);
#pragma omp section
f_EPMFilter(gData, nWidth, nHeight, radius, delta, light);
#pragma omp section
f_EPMFilter(bData, nWidth, nHeight, radius, delta, light);
}
uchar* pOut = dstData;
pR = rData;
pG = gData;
pB = bData;
for (int j = 0; j < nHeight; j++)
{
for (int i = 0; i < nWidth; i++)
{
pOut[0] = *pR;
pOut[1] = *pG;
pOut[2] = *pB;
pR++;
pG++;
pB++;
pOut += 4;
}
}
free(rData);
free(gData);
free(bData);
}
void inline RGBToYCbCr(unsigned char R, unsigned char G, unsigned char B, int &Y, int &Cb, int &Cr)
{
Y = (unsigned char)(0.257*R + 0.564*G + 0.098*B + 16);
Cb = (unsigned char)(-0.148*R - 0.291*G + 0.439*B + 128);
Cr = (unsigned char)(0.439*R - 0.368*G + 0.071*B + 128);
}
void inline YCbCrToRGB(unsigned char Y, unsigned char Cb, unsigned char Cr, int &R, int &G, int &B)
{
R = 1.164*(Y - 16) + 1.596*(Cr - 128);
G = 1.164*(Y - 16) - 0.392*(Cb - 128) - 0.813*(Cr - 128);
B = 1.164*(Y - 16) + 2.017*(Cb - 128);
}
//只处理Y通道
void f_EPMFilterOneChannel(const uchar *srcData, uchar *outData, int nWidth, int nHeight, int nStride, int radius, float delta, float light)
{
if (srcData == NULL)
{
return;
}
unsigned char* yData = (unsigned char*)malloc(sizeof(unsigned char) * nWidth * nHeight);
unsigned char* cbData = (unsigned char*)malloc(sizeof(unsigned char) * nWidth * nHeight);
unsigned char* crData = (unsigned char*)malloc(sizeof(unsigned char) * nWidth * nHeight);
const uchar* pSrc = srcData;
int Y, CB, CR;
unsigned char* pY = yData;
unsigned char* pCb = cbData;
unsigned char* pCr = crData;
for (int j = 0; j < nHeight; j++)
{
for (int i = 0; i < nWidth; i++)
{
RGBToYCbCr(pSrc[0], pSrc[1], pSrc[2], Y, CB, CR);
*pY = Y;
*pCb = CB;
*pCr = CR;
pY++;
pCb++;
pCr++;
pSrc += 4;
}
}
f_EPMFilter(yData, nWidth, nHeight, radius, delta, light);
uchar* pOut = outData;
pY = yData;
pCb = cbData;
pCr = crData;
int R, G, B;
for (int j = 0; j < nHeight; j++)
{
for (int i = 0; i < nWidth; i++)
{
YCbCrToRGB(*pY, *pCb, *pCr, R, G, B);
pOut[0] = R;
pOut[1] = G;
pOut[2] = B;
pY++;
pCb++;
pCr++;
pOut += 4;
}
}
free(yData); free(cbData); free(crData);
}