-
Notifications
You must be signed in to change notification settings - Fork 1
/
matrix_2d.cpp
400 lines (342 loc) · 10.3 KB
/
matrix_2d.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include "matrix_2d.h"
#include "QFile"
#include "QDebug"
#include <math.h>
#include <iostream>
#include <chrono>
template<typename T>
Matrix2D<T>::Matrix2D(const uint width_, const uint height_)
{
mat.resize(width_);
for ( uint i=0; i < mat.size(); ++i ) {
mat[i].resize(height_);
}
width = width_;
height = height_;
//Initialize all values to 0.f
Fill(0.f);
}
template<typename T>
Matrix2D<T> Matrix2D<T>::operator+(const Matrix2D &m)
{
// if matrices do not have the same size, return original matrix
if (width != m.width || height != m.height){
return (*this);
}
Matrix2D<T> new_mat(width, height);
for (int x = 0; x < width; ++x){
for (int y = 0; y < height; ++y){
new_mat.mat[x][y] = this->mat[x][y] + m.mat[x][y];
}
}
return new_mat;
}
template<typename T>
Matrix2D<T> Matrix2D<T>::operator-(const Matrix2D &m)
{
// if matrices do not have the same size, return original matrix
if (width != m.width || height != m.height){
return (*this);
}
Matrix2D<T> new_mat(width, height);
for (int x = 0; x < width; ++x){
for (int y = 0; y < height; ++y){
new_mat.mat[x][y] = this->mat[x][y] - m.mat[x][y];
}
}
return new_mat;
}
template<typename T>
Matrix2D<T> Matrix2D<T>::operator*(const T value)
{
for (int x = 0; x < width; ++x){
for (int y = 0; y < height; ++y){
this->mat[x][y] = value*this->mat[x][y];
}
}
return *this;
}
template<typename T>
bool Matrix2D<T>::Compare(const Matrix2D &m2)
{
for (int x = 0; x < width; ++x){
for (int y = 0; y < height; ++y){
if ( this->mat[x][y] - m2.mat[x][y] > 1.f) {
return false;
}
}
}
return true;
}
template<typename T>
Matrix2D<T>::~Matrix2D()
{
// We do not use dynamic memory allocation within this class,
// thus destruction will do nothing here. Make it virtual
// so that future subclasses can perform their own object destruction
}
template<typename T>
uint Matrix2D<T>::GetWidth()
{
return width;
}
template<typename T>
uint Matrix2D<T>::GetHeight()
{
return height;
}
template<typename T>
T Matrix2D<T>::ValueAt(const int x, const int y)
{
return mat[x][y];
}
template<typename T>
void Matrix2D<T>::SetCellValue(const int x, const int y, const T value)
{
mat[x][y] = value;
}
template<typename T>
void Matrix2D<T>::Fill(const T value)
{
for (uint i = 0; i < mat.size(); ++i) {
std::fill( mat[i].begin(), mat[i].end(), value);
}
}
template<typename T>
std::unique_ptr<QImage> Matrix2D<T>::ConvertToQImage()
{
std::unique_ptr<QImage> image = std::make_unique<QImage>(this->width, this->height, QImage::Format_RGB32);
for(int x = 0; x < image->width(); ++x){
for(int y = 0; y < image->height(); ++y){
QRgb color = 0xff000000;
if(this->ValueAt(x, y) > 255.f){
color = 0xffffffff;
}else if(this->ValueAt(x, y) < 0.f){
color = 0xff000000;
}else{
color = qRgb(static_cast<int>(this->ValueAt(x, y)), static_cast<int>(this->ValueAt(x, y)), static_cast<int>(this->ValueAt(x, y)));
}
image->setPixel(x, y, color);
}
}
//Ownership is passed to the callee
return std::move(image);
}
template<typename T>
void Matrix2D<T>::FilterMax(const int filter_size)
{
int half_size = filter_size/2;
Matrix2D<T> temp_matrix(width, height);
for ( int x = 0; x < width; ++x ) {
for ( int y = 0; y < height; ++y ) {
T max_value = -1.f;
for(int k = -half_size; k <= half_size; ++k){
for(int l = -half_size; l <= half_size; ++l){
int col = x + k;
int row = y + l;
if(col < 0 || col >= width){
break;
}
if(row < 0 || row >= height){
continue;
}
if(mat[col][row] > max_value){
max_value = mat[col][row];
}
}
}
temp_matrix.mat[x][y] = max_value;
}
}
for(int x = 0; x < width; ++x){
for(int y = 0; y < height; ++y){
mat[x][y] = temp_matrix.mat[x][y];
}
}
}
template<typename T>
void Matrix2D<T>::FilterMin(const int filter_size)
{
int half_size = filter_size/2;
Matrix2D<T> temp_matrix(width, height);
for(int x = 0; x < width; ++x){
for(int y = 0; y < height; ++y){
T min_value = 255.f;
//Find minimum value inside the filter window
for(int k = -half_size; k <= half_size; ++k){
for(int l = -half_size; l <= half_size; ++l){
//Mirror pixels at the border of the image
int col = x + k;
int row = y + l;
if(col < 0 || col >= width){
break;
}
if(row < 0 || row >= height){
continue;
}
if(mat[col][row] < min_value){
min_value = mat[col][row];
}
}
}
temp_matrix.mat[x][y] = min_value;
}
}
for(int x = 0; x < width; ++x){
for(int y = 0; y < height; ++y){
mat[x][y] = temp_matrix.mat[x][y];
}
}
}
template<typename T>
void Matrix2D<T>::FilterMean(const int filter_size)
{
//Mean Filter is separable, thus the convolution will be performed in two passes: row and column-wise
int half_size = static_cast<int>(floor(filter_size/2));
Matrix2D<T> temp_matrix(width, height);
#if 1
//Row-wise convolution
for(int x = 0; x < width; ++x ) {
for(int y = 0; y < height; ++y){
T new_value = 0.f;
int pixels_count = 0;
for(int k = -half_size; k <= half_size; ++k){
int col = x + k;
//symmetric padding
if (col < 0 ) {
col = -col;
}
if ( col >= width) {
col -= width;
}
if ( col >= 0 && col < width ) {
pixels_count++;
new_value += mat[col][y];
}
}
temp_matrix.mat[x][y] = new_value/pixels_count;
}
}
Matrix2D<T> temp_matrix2(width, height);
//Column-wise convolution
for(int x = 0; x < width; ++x ) {
for(int y = 0; y < height; ++y){
T new_value = 0.f;
int pixels_count = 0;
for(int k = -half_size; k <= half_size; ++k){
int row = y + k;
//symmetric padding
if (row < 0 ) {
row = -row;
}
if ( row >= height) {
row -= height;
}
if ( row >= 0 && row < height ) {
pixels_count++;
new_value += temp_matrix.mat[x][row];
}
}
temp_matrix2.mat[x][y] = new_value/pixels_count;
}
}
#else
//Naive convolution
for(int i = 0; i < rows; ++i){
for(int j = 0; j < columns; ++j){
T new_value = 0.f;
int pixels_count = 0;
for(int k = -half_size; k <= half_size; ++k){
for(int l = -half_size; l <= half_size; ++l){
//Mirror pixels at the border of the image
int row = i + k;
int column = j + l;
if(row < 0){
row = -row;
}
if(column < 0){
column = -column;
}
if(row >= rows){
row -= rows;
}
if(column >= columns){
column -= columns;
}
if ( (row >= 0 && row < rows) && (column >= 0 && column < columns) ){
pixels_count++;
new_value += mat[row][column];
}else{
qDebug()<<"out of scope"<<row<<column;
}
}
}
if(pixels_count != filter_size*filter_size){
qDebug()<<"wrong elements in convolution";
}
// qDebug()<<"pixels count"<<pixels_count<<"filter size"<<filter_size;
temp_matrix.mat[i][j] = new_value/pixels_count;
}
}
#endif
for(int x = 0; x < width; ++x){
for(int y = 0; y < height; ++y){
mat[x][y] = temp_matrix.mat[x][y];
}
}
}
template<typename T>
void Matrix2D<T>::ScaleToInterval(const T start, const T end)
{
const T min = GetMinValue();
const T max = GetMaxValue();
for ( int x = 0; x < width; ++x ) {
for ( int y = 0; y < height; ++y ) {
mat[x][y] = start + (end - start) * ( mat[x][y] - min ) / ( max - min );
}
}
}
template<typename T>
T Matrix2D<T>::GetMinValue()
{
T min = mat[0][0];
for ( int x = 0; x < width; ++x ) {
for ( int y = 0; y < height; ++y ) {
if (mat[x][y] < min) {
min = mat[x][y];
}
}
}
return min;
}
template<typename T>
T Matrix2D<T>::GetMaxValue()
{
T max = mat[0][0];
for ( int x = 0; x < width; ++x ) {
for ( int y = 0; y < height; ++y ) {
if (mat[x][y] > max) {
max = mat[x][y];
}
}
}
return max;
}
template<typename T>
void Matrix2D<T>::SaveToFile(const QString filename)
{
qDebug()<<"saving to"<<filename;
QFile file( filename );
if ( file.open(QIODevice::ReadWrite) )
{
QTextStream stream( &file );
for( int x = 0; x < width; ++x ) {
stream << "[ ";
for( int y = 0; y < height; ++y ) {
stream << QString::number(mat[x][y], 'f', 2) <<", ";
}
stream << "]\n";
}
}
file.close();
}