-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilter.cpp
More file actions
291 lines (241 loc) · 7.86 KB
/
Copy pathFilter.cpp
File metadata and controls
291 lines (241 loc) · 7.86 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
#include "Filter.h"
#include "Image.h"
#include <cmath>
#include <iostream>
// Clamps a channel value to the range [0, 255].
// Used after convolution, as the result may go out of bounds.
int clampChannel(double value)
{
if (value < 0.0)
{
return 0;
}
if (value > 255.0)
{
return 255;
}
return static_cast<int>(std::lround(value));
}
// Contrast matrix
Filter::Matrix ContrastFilter::contrastMatrix = {
{0, 0, 0},
{0, 1.5, 0},
{0, 0, 0},
};
// Blur matrix
Filter::Matrix BlurFilter::blurMatrix = {
{1, 1, 1},
{1, 1, 1},
{1, 1, 1},
};
// Sharpen matrix
Filter::Matrix SharpenFilter::sharpenMatrix = {
{0, -1, 0},
{-1, 5, -1},
{0, -1, 0},
};
// Contour matrix
Filter::Matrix ContourFilter::contourMatrix = {
{0, -1, 0},
{-1, 4, -1},
{0, -1, 0},
};
const MyString& Filter::GetName() const
{
return name;
}
// Base apply implementation
Image* Filter::apply(Image* image) const
{
return image;
}
MatrixFilter::MatrixFilter(const MyString& name, const Matrix& matrix, double normKff)
: Filter(name, matrix, normKff)
{
}
// Constructor for filters without a matrix (Negative, Threshold, ContrastNorm).
// Fills the matrix with zeros, but it is not used.
Filter::Filter(const MyString& name) : name(name), normKff(1.0)
{
for (int i = 0; i < MATRIX_SIZE; ++i)
for (int j = 0; j < MATRIX_SIZE; ++j)
matrix[i][j] = 0.0;
}
// Constructor for matrix filters - copies the given matrix
Filter::Filter(const MyString& name, const Matrix& matrix, double normKff) : name(name), normKff(normKff)
{
for (int i = 0; i < MATRIX_SIZE; ++i)
for (int j = 0; j < MATRIX_SIZE; ++j)
this->matrix[i][j] = matrix[i][j];
}
// Matrix filter application
// 1. Create a new image result
// 2. For each pixel (x, y) iterate through a 3x3 window around it
// 3. Edges are handled by clamping to the edge pixel
// 4. For each channel (R, G, B) compute weighted sum with matrix normalization
// 5. Clamp the result via clampChannel() to [0, 255]
// 6. Copy result back to image
Image* MatrixFilter::apply(Image* image) const
{
if (image == nullptr)
{
return nullptr;
}
const int width = image->GetWidth();
const int height = image->GetHeight();
Image result(width, height); // temporary image for the result
const int offset = MATRIX_SIZE / 2;
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
double sumR = 0.0;
double sumG = 0.0;
double sumB = 0.0;
// Iterate through 3x3 window around (x, y)
for (int ky = 0; ky < MATRIX_SIZE; ++ky)
{
for (int kx = 0; kx < MATRIX_SIZE; ++kx)
{
int sampleX = x + kx - offset;
int sampleY = y + ky - offset;
// Edge handling: if out of bounds - take the edge pixel
if (sampleX < 0)
{
sampleX = 0;
}
else if (sampleX >= width)
{
sampleX = width - 1;
}
if (sampleY < 0)
{
sampleY = 0;
}
else if (sampleY >= height)
{
sampleY = height - 1;
}
const Pixel& pixel = image->GetPixel(sampleX, sampleY);
const double weight = matrix[ky][kx] * normKff; // weight with normalization
sumR += pixel.r * weight;
sumG += pixel.g * weight;
sumB += pixel.b * weight;
}
}
result.GetPixel(x, y) = Pixel(
clampChannel(sumR),
clampChannel(sumG),
clampChannel(sumB));
}
}
// Copy the result back to the original image
image->GetPixels() = result.GetPixels();
return image;
}
ContrastFilter::ContrastFilter() : MatrixFilter("Contrast", contrastMatrix)
{
}
BlurFilter::BlurFilter() : MatrixFilter("Blur", blurMatrix, 1. / 9)
{
}
SharpenFilter::SharpenFilter() : MatrixFilter("Sharpen", sharpenMatrix)
{
}
ContourFilter::ContourFilter() : MatrixFilter("Contour", contourMatrix)
{
}
// Negative: new_val = 255 - old_val for each channel
NegativeFilter::NegativeFilter() : Filter("Negative") {}
// Apply negative: each channel is inverted (255 - value)
Image* NegativeFilter::apply(Image* image) const
{
if (image == nullptr) return nullptr;
const int width = image->GetWidth();
const int height = image->GetHeight();
Image result(width, height);
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
const Pixel& p = image->GetPixel(x, y);
result.GetPixel(x, y) = Pixel(
255 - p.r,
255 - p.g,
255 - p.b
);
}
}
image->GetPixels() = result.GetPixels();
return image;
}
// Thresholding: if pixel brightness >= threshold - 1 (white), otherwise 0 (black).
// For color images first convert to grayscale using ITU-R BT.601 formula
ThresholdFilter::ThresholdFilter(int threshold) : Filter("Threshold"), threshold_(threshold) {}
Image* ThresholdFilter::apply(Image* image) const
{
if (image == nullptr) return nullptr;
int imagePType = image->GetPType();
// If the image is already binary - do nothing
if (imagePType == 1 || imagePType == 4) return image;
const int width = image->GetWidth();
const int height = image->GetHeight();
Image result(width, height);
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
const Pixel& p = image->GetPixel(x, y);
short gray;
if (imagePType == 2 || imagePType == 5) {
gray = p.g; // grayscale image - take any channel
}
else {
// Color → convert to grayscale using ITU-R BT.601 standard
gray = 0.299 * p.r + 0.587 * p.g + 0.114 * p.b;
}
int val = ((gray >= threshold_) ? 1 : 0); // threshold comparison
result.GetPixel(x, y) = Pixel(val);
}
}
image->setPType(4); // result - binary P4
image->GetPixels() = result.GetPixels();
return image;
}
// Contrast normalization
ContrastNormalizationFilter::ContrastNormalizationFilter() : Filter("ContrastNorm") {}
Image* ContrastNormalizationFilter::apply(Image* image) const
{
if (image == nullptr) return nullptr;
const int width = image->GetWidth();
const int height = image->GetHeight();
// Step 1: find global minimum and maximum across all three channels
int globalMin = 255, globalMax = 0;
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
const Pixel& p = image->GetPixel(x, y);
if (p.r < globalMin) globalMin = p.r;
if (p.g < globalMin) globalMin = p.g;
if (p.b < globalMin) globalMin = p.b;
if (p.r > globalMax) globalMax = p.r;
if (p.g > globalMax) globalMax = p.g;
if (p.b > globalMax) globalMax = p.b;
}
}
// Step 2: if the image is uniform - skip
if (globalMin == globalMax) return image;
// Step 3: scaling factor
double scale = 255.0 / (globalMax - globalMin);
Image result(width, height);
// Step 4: apply normalization to each pixel
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
const Pixel& p = image->GetPixel(x, y);
result.GetPixel(x, y) = Pixel(
clampChannel((p.r - globalMin) * scale),
clampChannel((p.g - globalMin) * scale),
clampChannel((p.b - globalMin) * scale)
);
}
}
image->GetPixels() = result.GetPixels();
return image;
}