-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtramage.cpp
232 lines (170 loc) · 6.07 KB
/
tramage.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
#include <iostream>
#include <vector>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
/*
##############################################################################
*/
cv::Mat tramage_floyd_steinberg( cv::Mat input ){
int rowSize = input.rows;
int columnSize = input.cols;
cv::Mat output( rowSize, columnSize, CV_8UC1 );
output.setTo(255);
cv::Mat input2;
input.convertTo(input2, CV_32FC1);
for(int row = 0; row < rowSize; row++) {
for(int col = 0; col < columnSize; col++) {
float pixel = input2.at<float>(row, col);
//calcul de la couleur la plus proche
float new_pixel = pixel > 127.0 ? 255.0 : 0;
output.at<uchar>(row, col) = new_pixel;
float error = pixel - new_pixel;
if(col + 1 < columnSize) input2.at<float>(row, col + 1) += error * 7.0 / 16.0;
if(row + 1 < rowSize) {
if(col - 1 >= 0) input2.at<float>(row + 1, col - 1) += error * 3.0 / 16.0;
input2.at<float>(row + 1, col) += error * 5.0 / 16.0;
if(col + 1 < columnSize) input2.at<float>(row + 1, col + 1) += error * 1.0 / 16.0;
}
}
}
return output;
}
void test_tramage_floyd_steinberg(String filename){
Mat f = imread(filename, IMREAD_GRAYSCALE);
if (f.empty()) {
cout << "Impossible de charger l'image " << filename << endl;
return;
}
namedWindow("Image initial");
imshow("Image initial", f);
Mat final_image = tramage_floyd_steinberg(f);
namedWindow("Image finale");
imshow("Image finale", final_image);
waitKey(0);
}
/*
####################################################################
*/
cv::Mat tramage_floyd_steinberg_color(cv::Mat input) {
int rowSize = input.rows;
int columnSize = input.cols;
cv::Mat output(rowSize, columnSize, input.type());
output.setTo(255);
for (int row = 0; row < rowSize; row++) {
for (int col = 0; col < columnSize; col++) {
for (int channel = 0; channel < input.channels(); channel++) {
uchar pixel = input.at<Vec3b>(row, col)[channel];
// Calcul de la couleur la plus proche
uchar new_pixel = pixel > 127 ? 255 : 0;
output.at<Vec3b>(row, col)[channel] = new_pixel;
int error = pixel - new_pixel;
if (col + 1 < columnSize) input.at<Vec3b>(row, col + 1)[channel] += error * 5 / 16;
if (row + 1 < rowSize) {
if (col - 1 >= 0) input.at<Vec3b>(row + 1, col - 1)[channel] += error * 3 / 16;
input.at<Vec3b>(row + 1, col)[channel] += error * 3 / 16; // Ajustement ici
if (col + 1 < columnSize) input.at<Vec3b>(row + 1, col + 1)[channel] += error * 1 / 16;
}
}
}
}
return output;
}
void test_tramage_floyd_steinberg_color(String filename){
Mat f = imread(filename);
if (f.empty()) {
cout << "Impossible de charger l'image " << filename << endl;
return;
}
namedWindow("Image initial");
imshow("Image initial", f);
Mat final_image = tramage_floyd_steinberg_color(f);
namedWindow("Image finale");
imshow("Image finale", final_image);
waitKey(0);
}
/*
####################################################################
*/
float distance_color_l2(Vec3f bgr1, Vec3f bgr2) {
return sqrt(pow(bgr1[0] - bgr2[0], 2) + pow(bgr1[1] - bgr2[1], 2) + pow(bgr1[2] - bgr2[2], 2));
}
int best_color(Vec3f bgr, vector<Vec3f> colors) {
int index = 0;
float minDist = distance_color_l2(bgr, colors[0]);
for (int i = 1; i < colors.size(); i++) {
float dist = distance_color_l2(bgr, colors[i]);
if (dist < minDist) {
minDist = dist;
index = i;
}
}
return index;
}
Vec3f error_color(Vec3f bgr1, Vec3f bgr2) {
return Vec3f(bgr1[0] - bgr2[0], bgr1[1] - bgr2[1], bgr1[2] - bgr2[2]);
}
cv::Mat tramage_floyd_steinberg_final(cv::Mat input, std::vector<cv::Vec3f> colors) {
cv::Mat fs;
input.convertTo(fs, CV_32FC3, 1 / 255.0);
int rowSize = fs.rows;
int columnSize = fs.cols;
for (int row = 0; row < rowSize; row++) {
for (int col = 0; col < columnSize; col++) {
Vec3f c = fs.at<Vec3f>(row, col);
int i = best_color(c, colors);
Vec3f e = error_color(c, colors[i]);
fs.at<Vec3f>(row, col) = colors[i];
if (col + 1 < columnSize) fs.at<Vec3f>(row, col + 1) += e * (7.0 / 16.0);
if (row + 1 < rowSize) {
if (col - 1 >= 0) fs.at<Vec3f>(row + 1, col - 1) += e * (1.0 / 16.0);
fs.at<Vec3f>(row + 1, col) += e * (5.0 / 16.0);
if (col + 1 < columnSize) fs.at<Vec3f>(row + 1, col + 1) += e * (3.0 / 16.0);
}
}
}
cv::Mat output;
fs.convertTo(output, CV_8UC3, 255.0);
return output;
}
void test_tramage_floyd_steinberg_final(String filename){
Mat f = imread(filename);
vector<cv::Vec3f> COLORS;
Vec3f a(0.0f, 0.0f, 0.0f);
Vec3f b(1.0f, 0.0f, 1.0f);
Vec3f c(0.0f, 1.0f, 1.0f);
Vec3f d(1.0f, 1.0f, 0.0f);
Vec3f e(1.0f, 1.0f, 1.0f);
Vec3f r(0.0f, 0.0f, 1.0f);
Vec3f g(0.0f, 1.0f, 0.0f);
Vec3f bl(1.0f, 0.0f, 0.0f);
std::vector<Vec3f> vect;
COLORS.push_back(a);
COLORS.push_back(b);
COLORS.push_back(c);
COLORS.push_back(d);
COLORS.push_back(e);
namedWindow("Image initial");
imshow("Image initial", f);
Mat final_image = tramage_floyd_steinberg_final(f, COLORS);
namedWindow("Image tramee CMYK");
imshow("Image tramee CMYK", final_image);
waitKey(0);
}
/*
####################################################################
*/
int main(int argc, char* argv[]) {
if (argc != 2) {
cout << "Usage: " << argv[0] << " <nom-fichier-image>" << endl;
return 1;
}
Mat f = imread(argv[1]);
if (f.empty()) {
cout << "Impossible de charger l'image " << argv[1] << endl;
return 1;
}
test_tramage_floyd_steinberg_final(argv[1]);
return 0;
}