-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimagesignature.cpp
More file actions
310 lines (242 loc) · 7.53 KB
/
imagesignature.cpp
File metadata and controls
310 lines (242 loc) · 7.53 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*OPENCV LIBRARIES*/
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv/cv.h>
#include <opencv/highgui.h>
/*STANDARD LIBRARIES*/
#include <iostream>
#include <vector>
#include <algorithm>
/*Definitions*/
#define THRESHOLD 0.5
#define DEBUG 0
/*Namespaces*/
using namespace cv;
using namespace std;
/*
* Function to extract edges from an image as a signature
*/
Mat edges_signature(Mat image) {
Mat edges;
/*Canny gets edges from image*/
Canny(image, edges, 20, 20*3);
/*Transposition in order to show the right modifitacions*/
transpose(edges, edges);
return edges;
}
/*
*Function that generates signatures applying DCT transformation
*/
Mat generate_signature(Mat image) {
Mat a, b, dct1, dct2;
Mat result;
int i, j, n, m;
float coef1, coef2;
// Size of the matrix
n = image.cols;
m = image.rows;
result = cv::Mat::zeros(m / 4, n / 4, CV_32FC1);
// for to compare 4 size blocks of the image
for (i = 0; i < (m / 4); ++i) {
for (j = 0; j < (n / 4) - 2; ++j) {
a = image.rowRange(i * 4, (i + 1) * 4).colRange(j * 4 , (j + 1) * 4); // Subimage a size 4x4
b = image.rowRange(i * 4, (i + 1) * 4).colRange((j + 1) * 4 , (j + 2) * 4); // Subimage a size 4x4
dct(a, dct1); //DCT transformation on image a
dct(b, dct2); //DCT transformation on image b
coef1 = (float) dct1.at<float>(0,0);
coef2 = (float) dct2.at<float>(0,0);
if (DEBUG) cout << coef1 << ", " << coef2;
/*Comparing DCT with first DC coefficient*/
if (coef1 > coef2) {
result.at<float>(j, i) = 1.0;
} else {
result.at<float>(j, i) = 0.0;
}
}
}
return result;
}
/*Locates modifications from image and shows them in the original one*/
void show_locations(Mat image, Mat xor_matrix, int type)
{
int x, y, i, j, rows,
matches, cols, size, dif;
Mat act;
Point p1, p2;
float result, reb;
rows = xor_matrix.rows;
cols = xor_matrix.cols;
x = y = 4; // Small size for better look
size = x * y;
namedWindow("Attack detections");
matches = 0;
/*Loop to access the BER matrix*/
for (i = 0; i + x < rows ; ++i) {
for (j = 0; j + y < cols; ++j) {
/*Getting block, and number of errors from BER matrix*/
act = xor_matrix.rowRange(i, i + x).colRange(j, j + y);
dif = countNonZero(act);
result = (float) dif / size;
/*If the errors av is more than the threshold*/
if (result > THRESHOLD) {
++matches;
/*Type 1 = Edges, Type 2 = DCT (Needs Points translation)*/
if (type == 1) {
p1 = Point(i,j);
p2 = Point(i + x, j + y);
} else {
p1 = Point(i * 4,j * 4);
p2 = Point((i + x) * 4, (j + y) * 4);
}
rectangle(image, p1, p2, Scalar(0,0,255)); //Red rectangle on original image
}
}
}
reb = (float) matches / ((rows - x) * (cols - x)) ;
cout << reb << endl;
imshow("Attack detections", image);
cvMoveWindow("Attack detections", 1000, 0);
}
/*
* Function that compares signatures between two images
*/
void compare_signatures(Mat image1, Mat image2, int type)
{
Mat signature1, signature2, differenceM;
Mat YCrCb_img1, YCrCb_img2, clon;
vector<Mat> channels1, channels2;
int diff;
clon = image1.clone();
/*Only changing image scale if the type is DCT*/
if ( type == 2 ) {
image1.convertTo(image1, CV_32FC1);
image2.convertTo(image2, CV_32FC1);
}
/*Creating images YCrCb*/
cvtColor(image1,YCrCb_img1,CV_BGR2YCrCb);
cvtColor(image2,YCrCb_img2,CV_BGR2YCrCb);
/*Separating channels of each YCrCb image*/
cv::split(YCrCb_img1, channels1);
cv::split(YCrCb_img2, channels2);
/*Type 1 = Edges, Type 2 = DCT*/
if (type == 1) {
signature1 = edges_signature(channels1[0]);
signature2 = edges_signature(channels2[0]);
} else {
/*Generating signatures with DCT*/
signature1 = generate_signature(channels1[0]);
signature2 = generate_signature(channels2[0]);
}
/*XOR to get missmatches between images*/
differenceM = signature1 ^ signature2;
if (DEBUG) {
cout << "M1: " << endl << " ";
cout << signature1 << endl;
cout << "M2: " << endl << " ";
cout << signature2 << endl;
cout << "M3: " << endl << " ";
cout << differenceM << endl;
}
/*Quantity of missmatches*/
diff = countNonZero(differenceM);
/*Same image, nothing to do*/
if (diff == 0) {
cout << "Exact same image, nothing to compute" << endl;
return;
}
if (DEBUG) {
namedWindow("Debug");
imshow("Debug",clon);
waitKey(0);
}
/*Show image alterations*/
show_locations(clon, differenceM, type);
}
/*
* Function that prints options to the user
*/
void print_options()
{
cout << "Select type of signature to use:" << endl;
cout << "(1) Egdes signature" << endl;
cout << "(2) DCT signature" << endl;
cout << "Your option: ";
}
/*
* Function that gets input from the user
*/
int get_input()
{
int selection;
print_options();
cin >> selection;
if ( selection == 1 || selection == 2 ) {
return selection;
} else {
cout << endl << "Selected option not valid!" << endl;
cout << "--------------------------------" << endl << endl;
get_input();
}
}
/*
* MAIN
*/
int main(int argc, const char *argv[]) {
Mat image1, image2;
vector<Mat> channels1, channels2;
int type;
Size s;
Mat out;
/* Number of arguments */
if (argc != 4) {
cout << "Usage: ./watermarking image1 image2 type" << endl << " ";
cout << "type: 1 (Egdes signature) 2 (DCT signature)" << endl;
return -1;
}
/*Reading images*/
image1 = imread(argv[1], CV_LOAD_IMAGE_COLOR);
image2 = imread(argv[2], CV_LOAD_IMAGE_COLOR);
/*Error in case of bad input*/
if (!image1.data || !image2.data) {
cout << "Could not open or find image" << endl;
return -1;
}
/*Accepting only square images*/
if ((image1.cols != image1.rows) || (image2.rows != image2.cols)) {
cout << "Images must have NxN size" << endl;
return -1;
}
if (DEBUG) {
namedWindow("Debug");
imshow("Debug", image1);
waitKey(0);
imshow("Debug", image2);
waitKey(0);
}
/*Resizing image2 to image1 size*/
s = cv::Size(image1.cols, image1.rows);
resize(image2, image2, s);
/*Get the input from the user*/
//type = get_input();
type = atoi(argv[3]);
if ( type > 2 ) {
cout << "Usage: ./watermarking image1 image2 type" << endl << " ";
cout << "type: 1 (Egdes signature) 2 (DCT signature)" << endl;
return -1;
}
//Show original image
namedWindow("Original");
imshow("Original", image1);
waitKey(0);
//Show modified image and then attack detection image
namedWindow("Modified");
imshow("Modified", image2);
cvMoveWindow("Original", 0 , 0);
cvMoveWindow("Modified", 0 , 0);
/*Compare signatures and show modifications*/
compare_signatures(image1, image2, type);
/*Key to end execution*/
waitKey(0);
return 0;
}