-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrayLevelImage2D.cpp
More file actions
210 lines (188 loc) · 4.86 KB
/
GrayLevelImage2D.cpp
File metadata and controls
210 lines (188 loc) · 4.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
#include <iostream>
#include <vector>
#include <exception>
#include <fstream>
#include <sstream>
#include "GrayLevelImage2D.hpp"
using namespace std;
// pas besoin d'ecrire les constructeurs par copie et d'affectation car ils sont generes automatiquement
// mise en place du type
typedef unsigned char GrayLevel; // le type pour les niveaux de gris.
// declare method of class GrayLevelImage2d.hpp
GrayLevelImage2D::GrayLevelImage2D()
{
m_width = 0;
m_height = 0;
}
GrayLevelImage2D::GrayLevelImage2D(int w, int h, GrayLevel g)
{
m_width = w;
m_height = h;
// resize rempoli automatiquement de 0
m_data.resize(w * h, g);
}
void GrayLevelImage2D::fill(GrayLevel g)
{
m_data.assign(m_width * m_height, g);
}
int GrayLevelImage2D::w() const
{
return m_width;
}
int GrayLevelImage2D::h() const
{
return m_height;
}
GrayLevel GrayLevelImage2D::at(int i, int j) const
{
return m_data[i + j * m_width];
}
GrayLevel &GrayLevelImage2D::at(int i, int j)
{
return m_data[i + j * m_width];
}
// index function
int GrayLevelImage2D::index(int i, int j) const
{
return i + j * m_width;
}
GrayLevelImage2D::Iterator::Iterator(GrayLevelImage2D &image, int x, int y)
: Container::iterator(image.m_data.begin() + image.index(x, y))
{
}
GrayLevelImage2D::Iterator GrayLevelImage2D::begin()
{
return Iterator(*this, 0, 0);
}
GrayLevelImage2D::Iterator GrayLevelImage2D::end()
{
return Iterator(*this, 0, m_height);
// ou return Iterator(*this, m_width, m_height-1);
}
GrayLevelImage2D::Iterator GrayLevelImage2D::start(int x, int y)
{
return Iterator(*this, x, y);
}
std::pair<int, int> GrayLevelImage2D::position(Iterator it) const
{
int x = it - m_data.begin();
int y = x / m_width;
x = x % m_width;
return std::make_pair(x, y);
}
string readline(std::istream &input)
{
string str;
do
{
getline(input, str);
} while (str != "" && str[0] == '#');
return str;
}
bool GrayLevelImage2D::importPGM(std::istream &input)
{
if (!input.good())
return false;
std::string format = readline(input);
std::string line = readline(input);
std::string delim = " ";
m_width = std::stoi(line.substr(0, line.find(delim)));
line.erase(0, line.find(delim) + delim.length());
m_height = std::stoi(line);
std::cout << m_width << " " << m_height << " " << format << std::endl;
std::cout << readline(input) << std::endl; // grayscale range
fill(0);
if (format == "P5")
{
input >> std::noskipws;
unsigned char v;
for (Iterator it = begin(), itE = end(); it != itE; ++it)
{
input >> v;
*it = v;
}
}
else
{
input >> std::skipws;
int v;
for (Iterator it = begin(), itE = end(); it != itE; ++it)
{
input >> v;
*it = v;
}
}
return true;
}
bool GrayLevelImage2D::exportPGM(ostream &output, bool ascii)
{
// write header
output << "P5" << endl;
output << m_width << " " << m_height << endl;
output << "255" << endl;
// write data
if (ascii)
{
for (int j = 0; j < m_height; ++j)
{
for (int i = 0; i < m_width; ++i)
{
output << (int)at(i, j) << " ";
}
output << endl;
}
}
else
{
for (Iterator it = begin(), itE = end(); it != itE; ++it)
{
output << *it;
}
}
return true;
}
void GrayLevelImage2D::medianFilter(int k)
{
GrayLevelImage2D copy(*this);
for (int j = 0; j < m_height; ++j)
{
for (int i = 0; i < m_width; ++i)
{
std::vector<GrayLevel> values;
for (int y = -k; y <= k; ++y)
{
for (int x = -k; x <= k; ++x)
{
if (i + x >= 0 && i + x < m_width && j + y >= 0 && j + y < m_height)
{
values.push_back(copy.at(i + x, j + y));
}
}
}
std::sort(values.begin(), values.end());
at(i, j) = values[values.size() / 2];
}
}
}
void GrayLevelImage2D::convolation(double coefficient)
{
GrayLevelImage2D copy(*this);
for (int j = 0; j < m_width; ++j)
{
for (int i = 0; i < m_height; ++i)
{
//le if permettent de verifier si on est sur la bordure de l'image ou pas
double newVal = at(i, j) * (1 + coefficient);
if (i > 0)
newVal -= at(i - 1, j) * (coefficient / 4);
if (i < m_width - 1)
newVal -= at(i + 1, j) * (coefficient / 4);
if (j > 0)
newVal -= at(i, j - 1) * (coefficient / 4);
if (j < m_height - 1)
newVal -= at(i, j + 1) * (coefficient / 4);
copy.at(i, j) = newVal;
}
}
*this = copy;
}