-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfleximage.cpp
More file actions
318 lines (258 loc) · 8.14 KB
/
fleximage.cpp
File metadata and controls
318 lines (258 loc) · 8.14 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
310
311
312
313
314
315
316
317
318
#include <QtGui>
#include <algorithm>
#include <iostream>
#include <functional>
#include "fleximage.h"
#include "utils.h"
FlexImage::FlexImage(QString input): QImage(input){
//updateHistogramm();
}
FlexImage::FlexImage() : QImage(){
//updateHistogramm();
}
FlexImage::FlexImage(int x, int y) : QImage(x,y,QImage::Format_RGB888){
//updateHistogramm();
}
int FlexImage::getGray(int x, int y){
QRgb rgb = this->pixel(x,y);
return qGray(rgb);
}
int FlexImage::getRed(int x, int y){
QRgb rgb = this->pixel(x,y);
return qRed(rgb);
}
int FlexImage::getGreen(int x, int y){
QRgb rgb = this->pixel(x,y);
return qGreen(rgb);
}
int FlexImage::getBlue(int x, int y){
QRgb rgb = this->pixel(x,y);
return qBlue(rgb);
}
std::array<int,256> FlexImage::getScale(int (FlexImage::*colorfunc)(int x,int y)){
std::array<int,256> data{{}};
this->forEachPixel([&](int x ,int y) -> void{
int col = Utils::clamp((this->*colorfunc)(x,y));
data[col]++;
});
return data;
}
std::array<double,256> FlexImage::getRelativeScale(std::array<int,256> (FlexImage::*scalefunc)(void)){
int size = this->width() * this->height();
std::array<int,256> scale = (this->*scalefunc)();
std::array<double,256> tmp{{}};
for(int i = 0; i < 256 ; ++i){
tmp[i] = (double) scale[i];
}
for(int i = 0; i < 256 ; ++i){
tmp[i] /= size;
}
return tmp;
}
std::array<int,256> FlexImage::getGrayScale(){
return getScale(this->getGray);
}
std::array<int,256> FlexImage::getRedScale(){
return getScale(this->getRed);
}
std::array<int,256> FlexImage::getGreenScale(){
return getScale(this->getGreen);
}
std::array<int,256> FlexImage::getBlueScale(){
return getScale(this->getBlue);
}
std::array<double,256> FlexImage::getRelativeGrayScale(){
return getRelativeScale(this->getGrayScale);
}
std::array<double,256> FlexImage::getRelativeRedScale(){
return getRelativeScale(this->getRedScale);
}
std::array<double,256> FlexImage::getRelativeGreenScale(){
return getRelativeScale(this->getGreenScale);
}
std::array<double,256> FlexImage::getRelativeBlueScale(){
return getRelativeScale(this->getBlueScale);
}
double FlexImage::calculateGrayFConst(){
return calculateFConst(this->getRelativeGrayScale);
}
double FlexImage::calculateRedFConst(){
return calculateFConst(this->getRelativeRedScale);
}
double FlexImage::calculateGreenFConst(){
return calculateFConst(this->getRelativeGreenScale);
}
double FlexImage::calculateBlueFConst(){
return calculateFConst(this->getRelativeBlueScale);
}
double FlexImage::calculateGrayVarianz(){
return calculateVarianz(this->getRelativeGrayScale,this->calculateGrayFConst());
}
double FlexImage::calculateRedVarianz(){
return calculateVarianz(this->getRelativeRedScale,this->calculateRedFConst());
}
double FlexImage::calculateGreenVarianz(){
return calculateVarianz(this->getRelativeGreenScale,this->calculateGreenFConst());
}
double FlexImage::calculateBlueVarianz(){
return calculateVarianz(this->getRelativeBlueScale,this->calculateBlueFConst());
}
double FlexImage::calculateFConst(std::array<double,256> (FlexImage::*scalefunc)(void)){
std::array<double,256> scale = (this->*scalefunc)();
double fConst = 0;
for (int i = 0; i < 256; i++) {
fConst += scale[i] * i;
}
return fConst;
}
double FlexImage::calculateVarianz(std::array<double,256> (FlexImage::*scalefunc)(void),double fconst){
std::array<double,256> scale = (this->*scalefunc)();
double varianz = 0;
for (int i = 0; i < 256; i++) {
varianz += scale[i] * ((i - fconst) * (i - fconst));
}
return varianz;
}
void FlexImage::toGray(){
this->forEachPixel([&](int x,int y)-> void{
QRgb rgb = this->pixel(x, y);
int g = qGray(rgb);
QRgb grey = qRgb(g, g, g);
this->setPixel(x, y, grey);
});
}
/*FlexImage FlexImage::filterBlackWhite(int threshold){
if(threshold < 0) threshold = 0;
if(threshold > 255) threshold = 255;
for (int x = 0; x < this->width(); x++) {
for (int y = 0; y < this->height(); y++) {
if(qGray(this->pixel(x,y))<threshold){
this->setPixel(x,y,qRgb(0,0,0));
}else{
this->setPixel(x,y,qRgb(255,255,255));
}
}
}
return *this;
}
FlexImage FlexImage::filterWith(int **filter, int M, int N){
double s = 0;
for(int i=0 ; i<M ; ++i){
for(int j=0 ; j<N ; ++j){
s+= abs(filter[i][j]);
}
}
s= 1.0/s;
int width = this->width();
int height = this->height();
int K = M/2;
int L = N/2;
FlexImage copy = *this;
//rechnen
for(int v=0 ; v<height-1 ; ++v){
for(int u=0 ; u<width-1 ; ++u){
//Randfall
if(u<K || u>(width-K) || v<L || v>(height-L)){
this->setPixel(u,v,qRgb(128,128,128));
}else{
int sum = 0;
for(int j=-L ; j<=L ; ++j){
for(int i=-K ; i<=K ; ++i){
QRgb rgb = copy.pixel(u+i, v+j);
int g = qGray(rgb);
int c = filter[j+L][i+K];
sum = sum + c * g;
}
}
int q = (int) round(s * sum);
if(q<0) q = 0;
if(q>255) q = 255;
QRgb grey = qRgb(q, q, q);
this->setPixel(u,v,grey);
}
}
}
return *this;
}
FlexImage FlexImage::filterWith(double **filter, int M, int N){
double s = 0;
for(int i=0 ; i<M ; ++i){
for(int j=0 ; j<N ; ++j){
s+= abs(filter[i][j]);
}
}
s= 1.0/s;
int width = this->width();
int height = this->height();
int K = M/2;
int L = N/2;
FlexImage copy = *this;
//rechnen
for(int v=0 ; v<height-1 ; ++v){
for(int u=0 ; u<width-1 ; ++u){
//Randfall
if(u<K || u>=(width-K) || v<L || v>=(height-L)){
this->setPixel(u,v,qRgb(128,128,128));
}else{
int sum = 0;
for(int j=-L ; j<=L ; ++j){
for(int i=-K ; i<=K ; ++i){
QRgb rgb = copy.pixel(u+i, v+j);
int g = qGray(rgb);
double c = filter[j+L][i+K];
sum = sum + c * g;
}
}
int q = (int) round(s * sum);
if(q<0) q = 0;
if(q>255) q = 255;
QRgb grey = qRgb(q, q, q);
this->setPixel(u,v,grey);
}
}
}
return *this;
}
FlexImage FlexImage::filterWith(Filter* f){
return filterWith(f->getMatrix(),f->getRows(),f->getColums());
}*/
void FlexImage::print(){
this->forEachPixel([&](int x, int y)-> void{
std::cout<<"Pixel"<<x<<","<<y<<": "<<qGray(this->pixel(x,y))<<std::endl;
});
}
void FlexImage::drawLine(const QPoint& from, const QPoint& to, QRgb color, int width){
QPainter painter(this);
QPen pen(color);
pen.setWidth(width);
painter.setPen(pen);
painter.drawLine(from,to);
}
void FlexImage::setBrightness(int amount){
//positive Werte => heller, negative Werte => dunkler
this->forEachPixel([&](int x, int y)-> void{
QRgb result = this->forEachRGB(x,y,[&](int col)->int{ return Utils::clamp(col+amount); });
this->setPixel(x,y,result);
});
}
void FlexImage::setContrast(double contrast){
//Werte < 1 => Kontrast wird schwächer, Werte > 1 => Kontrast wird Stärker
this->forEachPixel([&](int x, int y)-> void{
QRgb result = this->forEachRGB(x,y,[&](int col)->int{ return Utils::clamp(col*contrast); });
this->setPixel(x,y,result);
});
}
FlexImage::~FlexImage(){
}
QRgb FlexImage::forEachRGB(int x, int y, std::function<int (int)> func){
QRgb val = this->pixel(x,y);
QRgb result = qRgb(func(qRed(val)),func(qGreen(val)),func(qBlue(val)));
return result;
}
void FlexImage::forEachPixel(std::function<void(int,int)> func){
for(int i = 0; i<this->width(); ++i){
for(int j = 0; j<this->height(); ++j){
func(i,j);
}
}
}