-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsharper.cpp
More file actions
309 lines (266 loc) · 10.2 KB
/
sharper.cpp
File metadata and controls
309 lines (266 loc) · 10.2 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
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
#include "sharper.h"
#include "ui_sharper.h"
Sharper::Sharper(QWidget *parent) :
QWidget(parent),
ui(new Ui::Sharper)
{
ui->setupUi(this);
connect(ui->doubleSpinBox_5, SIGNAL(valueChanged(double)),
this, SLOT(nChanged(double)));
connect(ui->contBut, SIGNAL(clicked()),
this, SLOT(contrastSlot()));
connect(ui->contrastArea, SIGNAL(valueChanged(int)),
this, SLOT(contAreaChanged(int)));
connect(ui->uMaskBut, SIGNAL(clicked()),
this, SLOT(uncertainSlot()));
connect(ui->uSpin, SIGNAL(valueChanged(int)),
this, SLOT(kgChanged(int)));
connect(ui->slider, SIGNAL(valueChanged(int)),
this, SLOT(sliderChanged(int)));
connect(ui->kgSpinbox, SIGNAL(valueChanged(int)),
this, SLOT(realKgChanged(int)));
connect(ui->simpleSharpButton, SIGNAL(clicked()),
this, SLOT(simpleSharp()));
connect(ui->sRGButton, SIGNAL(clicked()),
this, SLOT(simpleSharpRGB()));
connect(ui->gausSpin, SIGNAL(valueChanged(int)),
this, SLOT(setGausPower(int)));
connect(ui->sigmaSpin, SIGNAL(valueChanged(double)),
this, SLOT(setGausSigma(double)));
kg=1;
contArea = 2;
realKg=1;
gausPower = 1;
gausSigma = 1;
}
Sharper::~Sharper()
{
delete ui;
}
void Sharper::setSource(MainWindow *source)
{
window = source;
}
void Sharper::setImage(QImage picture)
{
image = picture;
result = image;
}
void Sharper::kgChanged(int spin)
{
kg = spin;
}
void Sharper::uncertainSlot()
{
uncertainMask(&image,2,1);
window->receiveResult(result);
}
void Sharper::simpleSharp()
{
uncertainMask(&image,1, 1);
window->receiveResult(result);
}
void Sharper::simpleSharpRGB()
{
uncertainMask(&image,1, 2);
window->receiveResult(result);
}
void Sharper::setGausPower(int spin)
{
gausPower=spin;
}
void Sharper::setGausSigma(double spin)
{
gausSigma = spin;
}
QList<int> Sharper::vicinity(QImage *picture, int power, int curx, int cury, int chanel)
{ //функция возвращает окрестность заданного пикселя
QList<int> proxArray;
for (int y = cury - power; y <= cury+ power; y++)
for (int x = curx - power; x <= curx+ power; x++)
{ //проверяем окрестные пиксели
if ((0 <= x && x <= picture->width()-1) && (0 <= y && y <= picture->height()-1))
{ //запоминаем их цвета
QColor pixel = picture->pixel(x, y);
if (chanel == 1)
proxArray.append(pixel.red());
if (chanel == 2)
proxArray.append(pixel.green());
if (chanel == 3)
proxArray.append(pixel.blue());
if (chanel ==4)
proxArray.append(pixel.lightness());
}
else { //если мы за пределами изображения, то запоминаем чёрный цвет
proxArray.append(0);
}
}
return proxArray;
}
int Sharper::listAverage(QList<int> theArray)
{ //функция считает среднее арифметическое значение списка
//qDebug() << "listaverage";
int aver = 0;
for (int i = 0; i <= theArray.size()-1; i++)
{
aver = aver + theArray[i];
}
aver = aver / theArray.size(); // среднее значение равно сумма значений/кол-во значений
return aver;
}
double Sharper::listDisp(QList<int> theArray, int mu)
{ //функция считает дисперсию значений списка
//qDebug() << "listdisp";
double aver = 0; //counting color value
for (int i = 0; i <= theArray.size()-1; i++)
{
aver = aver + ((theArray[i]-mu)*(theArray[i]-mu));
}
aver = aver / theArray.size();
return sqrt(aver);
}
QList<qreal> Sharper::genGauss(int power, qreal sigma)
{
QList<qreal>ohNo;
for (int i = 1; i<=power*2+1; i++)
{
for (int j = 1; j<=power*2+1; j++)
{
ohNo.append( (1.0/sqrt(2.0*3.14159*sigma*sigma)) * qExp( -(qPow((j-power - 1.0), 2) + qPow((i-power - 1.0), 2))/(2.0*sigma*sigma) ));
}
}
qreal sum = 0;
for (int i = 0; i<ohNo.size(); i++)
sum = sum+ohNo[i];
sum =1/sum;
for (int i = 0; i<ohNo.size(); i++)
ohNo[i]=ohNo[i]*sum;
return ohNo;
}
int Sharper::applyKernel(QList<int> area, QList<qreal> kernel)
{ //функция, накладывающая маски
int sum = 0;
for (int i = 0; i < area.size(); i++)
{ //умножаем элемент окрестности на соответствующий элемент маски и
sum = sum + area[i]*kernel[i]; //считаем сумму всех таких произведений
}
return sum ;// 16; //возвращаем Z
}
QColor Sharper::uncertainColor(QImage *picture, int mode, int tempKg, QColor sourcePixel, QList<qreal>gaussian, int x, int y, int hsl)
{
QColor pixel;
int value = 0;
if (hsl == 1)
{
QList<int> local = vicinity(picture, gausPower, x, y, 4); //получаем окрестность
int gauss = applyKernel(local, gaussian); //применяем фильтр гаусса к окрестности
if (mode == 2) tempKg=countKg(vicinity(picture, kg, x, y, 4));
int value = gauss + tempKg*(sourcePixel.lightness()-gauss); //считаем новую яркость
if (value > 255) value = 255;
if (value < 0) value = 0;
pixel.setHsl(sourcePixel.hslHue(),sourcePixel.hslSaturation(),value); //задаем новую яркость*/
return pixel;
}
QList<int> local = vicinity(picture, gausPower, x, y, 1); //получаем окрестность
int gauss = applyKernel(local, gaussian); //применяем фильтр гаусса к окрестности
if (mode == 2) tempKg=countKg(vicinity(picture, kg, x, y, 1));
value = gauss + tempKg*(sourcePixel.red()-gauss); //считаем новую яркость
if (value > 255) value = 255;
if (value < 0) value = 0;
pixel.setRed(value);
local = vicinity(picture, gausPower, x, y, 2); //получаем окрестность
gauss = applyKernel(local, gaussian); //применяем фильтр гаусса к окрестности
if (mode == 2) tempKg=countKg(vicinity(picture, kg, x, y, 2));
value = gauss + tempKg*(sourcePixel.green()-gauss); //считаем новую яркость
if (value > 255) value = 255;
if (value < 0) value = 0;
pixel.setGreen(value);
local = vicinity(picture, gausPower, x, y, 3); //получаем окрестность
gauss = applyKernel(local, gaussian); //применяем фильтр гаусса к окрестности
if (mode == 2) tempKg=countKg(vicinity(picture, kg, x, y, 3));
value = gauss + tempKg*(sourcePixel.blue()-gauss); //считаем новую яркость
if (value > 255) value = 255;
if (value < 0) value = 0;
pixel.setBlue(value);
return pixel;
}
void Sharper::uncertainMask(QImage *picture, int mode, int hsl)
{ //нечеткое маскирование
int tempKg = realKg;
QList<qreal> gaussian;
gaussian = genGauss(gausPower,gausSigma);
window->setMaxProgress(picture->width()-1);
for (int x = 0; x<picture->width();x++)
{ //обрабатываем всю картинку
window->setProgress(x);
for (int y = 0; y<picture->height();y++)
{
QColor pixel = uncertainColor(picture, mode, tempKg, picture->pixel(x,y),gaussian,x,y,hsl);
// pixel.setHsl(pixel.hslHue(),pixel.hslSaturation(),value); //задаем новую яркость
result.setPixel(x,y,pixel.rgb());
}
}
}
double Sharper::countKg(QList<int> proxy)
{ //адаптивный коэффициент
qreal disp = listDisp(proxy,listAverage(proxy));
if (abs(disp)<0.1) return kn;
return kn / listDisp(proxy,listAverage(proxy));
}
void Sharper::sliderChanged(int value)
{//слот слайдера для коэффициента фильтра
kn = value;
}
void Sharper::localContrast(QImage *picture)
{ //фильтрация с учётом локальной контрастности
window->setMaxProgress(picture->width()-1);
for (int x = 0; x<picture->width();x++)
{
window->setProgress(x);
for (int y = 0; y<picture->height();y++)
{
QList<int> local = vicinity(picture, contArea, x, y, 4); //получаем локальную окрестность
QColor pixel = picture->pixel(x, y); //делаем пиксель
int value = contrastPixel(pixel.lightness(), listAverage(local)) ; //считаем фильтр
if (value > 255) value = 255;
if (value < 0) value = 0;
//qDebug() << gauss << " + " << kg << " * (" << pixel.lightness() << " - " << gauss << ") = " << value;
pixel.setHsl(pixel.hslHue(),pixel.hslSaturation(),value);
result.setPixel(x,y,pixel.rgb()); //записываем
}
}
}
int Sharper::contrastPixel(int source, int proxBright)
{ //считаем выходное значение одного пикселя
// qDebug() << "was " << source << proxBright;
double kostil = source - proxBright;
double Cz = qAbs(kostil)/(source+proxBright);
// qDebug () << "cz" << Cz;
if (source <= proxBright)
{
// qDebug() << "now " << 1.0 - qPow(Cz, N) ;
return proxBright * ( (1.0 - qPow(Cz, N)) / (1.0 + qPow(Cz, N)) );
} //всё по формуле считаем в этой функции
else
{
//qDebug() << "now " << (1.0 + qPow(Cz, N)) / (1.0 - qPow(Cz, N));
return proxBright * ( (1.0 + qPow(Cz, N)) / (1.0 - qPow(Cz, N)) );
}
}
void Sharper::nChanged(double spin)
{ //слот смнился N
N = spin;
}
void Sharper::contrastSlot()
{ //вызвать функцию фильтрации
localContrast(&image);
window->receiveResult(result);
}
void Sharper::contAreaChanged(int spin)
{ //сменить размер окрестности
contArea = spin;
}
void Sharper::realKgChanged(int spin)
{
realKg=spin;
}