-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvolutionOperation.cpp
More file actions
136 lines (113 loc) · 3.83 KB
/
ConvolutionOperation.cpp
File metadata and controls
136 lines (113 loc) · 3.83 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
#include "ConvolutionOperation.h"
ConvolutionOperation::ConvolutionOperation(){
}
ConvolutionOperation::~ConvolutionOperation(){
}
double ConvolutionOperation::convolutionCenter(const int & row, const int & col, Image * image, Matrix<double> & mask){
int delta = (mask.getRows() - 1) / 2;
int end = mask.getRows() - delta ;
int start = (-1) * ((mask.getRows() - 1) / 2);
double sum = 0.0;
for(int i = start; i < end; ++i){
for(int j = start; j < end; ++j){
sum += mask.getAt(i+delta, j+delta) * image->getPixel(row+i, col+j);
}
}
return sum;
}
// double ConvolutionOperation::convolutionBorder(const int & row, const int & col, Image * image, Matrix<double> & mask){
// int delta = (mask.getRows() - 1) / 2;
// int end = mask.getRows() - delta ;
// int start = (-1) * ((mask.getRows() - 1) / 2);
// int rowIdx = 0;
// int colIdx = 0;
// int imgRows = image->getRows();
// int imgCols = image->getCols();
// double sum = 0.0;
// for(int i = start; i < end; ++i){
// for(int j = start; j < end; ++j){
// rowIdx = row+i;
// colIdx = col+j;
// if (rowIdx < 0) rowIdx += imgRows;
// if (colIdx < 0) colIdx += imgCols;
// if (rowIdx >= imgRows) rowIdx -= imgRows;
// if (colIdx >= imgCols) colIdx -= imgCols;
// sum += mask.getAt(i+delta, j+delta) * image->getPixel(rowIdx, colIdx);
// }
// }
// return sum;
// }
double ConvolutionOperation::convolutionBorder(const int & row, const int & col, Image * image, Matrix<double> & mask){
int delta = (mask.getRows() - 1) / 2;
int end = mask.getRows() - delta ;
int start = (-1) * ((mask.getRows() - 1) / 2);
int rowIdx = 0;
int colIdx = 0;
int imgMaxRow = image->getRows() - 1;
int imgMaxCol = image->getCols() - 1;
double sum = 0.0;
for(int i = start; i < end; ++i){
for(int j = start; j < end; ++j){
rowIdx = row+i;
colIdx = col+j;
if (rowIdx < 0) rowIdx *= (-1);
if (colIdx < 0) colIdx *= (-1);
if (rowIdx > imgMaxRow) rowIdx = (2 * imgMaxRow) - rowIdx;
if (colIdx > imgMaxCol) colIdx = (2 * imgMaxCol) - colIdx;
sum += mask.getAt(i+delta, j+delta) * image->getPixel(rowIdx, colIdx);
}
}
return sum;
}
Image ConvolutionOperation::convolution(Image * image, Matrix<double> & mask)
{
Image * imgFiltered = new Image((*image));
int maskSize = mask.getRows();
int newPixelValue = 0;
int startRow = (maskSize - 1) / 2;
int startCol = startRow;
int endRow = image->getRows() - (startRow + 1);
int endCol = image->getCols() - (startCol + 1);
// Convolution for image central area (without borders)
for(int i = startRow; i < endRow; ++i){
for(int j = startCol; j < endCol; ++j){
newPixelValue = (int) (convolutionCenter(i, j, image, mask));
imgFiltered->setPixel(i,j, newPixelValue);
}
}
// Convolution for rows out borders (up and bottom)
int idxLowerRow = endRow;
endRow = startRow;
endCol = image->getCols();
startRow = 0;
startCol = 0;
for(int i = startRow; i < endRow; ++i){
idxLowerRow += i;
for(int j = startCol; j < endCol; ++j){
// Upper row
newPixelValue = (int) (convolutionBorder(i, j, image, mask));
imgFiltered->setPixel(i,j, newPixelValue);
// Lower row
newPixelValue = (int) (convolutionBorder(idxLowerRow, j, image, mask));
imgFiltered->setPixel(idxLowerRow,j, newPixelValue);
}
}
// Convolution for columns out borders (left and right)
startRow = endRow;
startCol = 0;
endRow = image->getRows() - startRow;
endCol = startRow;
int idxRightCol = image->getCols() - startRow;
for(int j = startCol; j < endCol; ++j){
idxRightCol += j;
for(int i = startRow; i < endRow; ++i){
// Left col
newPixelValue = (int) (convolutionBorder(i, j, image, mask));
imgFiltered->setPixel(i,j, newPixelValue);
// Right col
newPixelValue = (int) (convolutionBorder(i, idxRightCol, image, mask));
imgFiltered->setPixel(i,idxRightCol, newPixelValue);
}
}
return (*imgFiltered);
}