-
Notifications
You must be signed in to change notification settings - Fork 0
/
noise.cpp
64 lines (51 loc) · 1.64 KB
/
noise.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
#include "noise.h"
QImage* addAWGN_GRAY(QImage* image, QSize size, double std) {
QImage* imageNoise = new QImage(size, QImage::Format_RGB32);
for (int i = 0; i < size.width(); i++) {
for (int j = 0; j < size.height(); j++) {
QRgb p = image->pixel(i, j);
int gray = qGray(p);
double gray_noise = gray+AWGN_generator(std);
if (gray_noise > 255) {
gray_noise = 255;
} else if (gray_noise < 0) {
gray_noise = 0;
}
QRgb n = qRgb(gray_noise, gray_noise, gray_noise);
imageNoise->setPixel(i, j, n);
}
}
return imageNoise;
}
QImage* addAWGN(QImage* image, QSize size, double std) {
QImage* imageNoise = new QImage(size, QImage::Format_RGB32);
for (int i = 0; i < size.width(); i++) {
for (int j = 0; j < size.height(); j++) {
QRgb p = image->pixel(i, j);
int r = qRed(p);
int g = qGreen(p);
int b = qBlue(p);
double rn = r+AWGN_generator(std);
double gn = g+AWGN_generator(std);
double bn = b+AWGN_generator(std);
if (rn > 255) {
rn = 255;
} else if (rn < 0) {
rn = 0;
}
if (gn > 255) {
gn = 255;
} else if (gn < 0) {
gn = 0;
}
if (bn > 255) {
bn = 255;
} else if (bn < 0) {
bn = 0;
}
QRgb n = qRgb(rn, gn, bn);
imageNoise->setPixel(i, j, n);
}
}
return imageNoise;
}