-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathopencv.cpp
More file actions
265 lines (234 loc) · 9.02 KB
/
opencv.cpp
File metadata and controls
265 lines (234 loc) · 9.02 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
////////////////////////////////////////ECE 406 Final Project///////////////////////////////////////////////////
// CPU Version by Renfei Wang and Shaowei Su //
// This program will unscramble the image //
// Please input three arguments //
// <image filename> <csv filename> <Box size> //
// the BoxSize is 2,4 or 8 according to the csv file //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stdint.h>
#include <sys/time.h>
using namespace cv;
int M; // number of rows in image
int N; // number of columns in image
int numBox;
int boxSize;
int box_col; // equals to the box_row
///////////////////This function is to calculate the xor of every row in the checkbox/////////////////
void checkbox_binary_row(uint64_t *csvMat,int boxSize,int box_col,int *result_matrix){
for(int x=0;x<box_col;x++){
for(int i=0;i<box_col;i=i+1){
uint64_t temp1 = csvMat[i*2+1+x*box_col*2];
for(int k=0;k<boxSize;k++){
int result=0;
uint64_t temp2 = temp1>>8;
uint64_t temp3 = temp2<<8;
result = temp1 - temp3;
temp1 = csvMat[i*2+1+x*box_col*2]>>8*(k+1);
result_matrix[(boxSize-1-k)*box_col+i+boxSize*x*box_col]=result;
}
}
}
}
///////////This function is to calculate the xor of every column in the checkbox/////////////////
void checkbox_binary_column(uint64_t *csvMat,int boxSize,int box_col,int *result_matrix){
for(int x=0;x<box_col;x++){
for(int i=0;i<box_col;i=i+1){
uint64_t temp1 = csvMat[i*2+x*box_col*2];
for(int k=0;k<boxSize;k++){
int result=0;
uint64_t temp2 = temp1>>8;
uint64_t temp3 = temp2<<8;
result = temp1 - temp3;
temp1 = csvMat[i*2+x*box_col*2]>>8*(k+1);
result_matrix[i*box_col*boxSize+x+(boxSize-1-k)*box_col]=result;
}
}
}
}
//////////This function will get the xor of each row or column///////////////////////
void get_xor(int *result_xor,int *result_matrix,int box_col, int M){
for(int j=0;j<M;j++){
for(int i=0+j*box_col;i<box_col-1+j*box_col;i++){
result_matrix[i+1]=result_matrix[i]^result_matrix[i+1];
}
result_xor[j] = result_matrix[box_col-1+j*box_col];//get the last one, which is the final result of the XOR
}
}
/////////////This is to calculate the xor of row in the scramble image///////////////
int *rowXOR(uchar *p, int M){
int i, j;
int *row_xor;
row_xor = (int*) malloc(M*sizeof(int));
if(row_xor == NULL){ printf("Fail to melloc \n\n"); exit(EXIT_FAILURE); }
for(i=0;i<M;i++){
row_xor[i] = p[i*M] ;
}
for(i=0;i<M;i++){
for(j=1;j<M;j++){
row_xor[i] = row_xor[i] ^ p[i*M+j];
}
}
return row_xor;
}
/////////////This is to calculate the xor of column in the scramble image///////////////
int *colXOR(uchar *p, int M){
int i, j;
int *col_xor;
col_xor = (int*) malloc(M*sizeof(int));
if(col_xor == NULL){ printf("Fail to melloc \n\n"); exit(EXIT_FAILURE); }
for(i=0;i<M;i++){
col_xor[i] = p[i] ;
}
for(i=0;i<M;i++){
for(j=1;j<M;j++){
col_xor[i] = col_xor[i] ^ p[j*M+i];
}
}
return col_xor;
}
int main(int argc, char *argv[]){
int i, j;
int *row_xor, *col_xor;
if( argc != 4) {
printf("Usage: input format: <image filename><csv filename><Box size>\n");
printf("box size should be 2, 4 or 8\n");
exit(EXIT_FAILURE);
}
/////////////////////image load/////////////////////////////////////////
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
if(! image.data ) {
fprintf(stderr, "Could not open the image.\n");
exit(EXIT_FAILURE);
}
printf("Loaded image '%s', size = %dx%d (dims = %d).\n", argv[1], image.rows, image.cols, image.dims);
// Set up global variables based on image size:
M = image.rows;
N = image.cols;
boxSize = atoi(argv[3]);
numBox = pow(M / boxSize, 2);
box_col= M/boxSize;// how many box in one col
///////////////////unscramble image XOR result////////////////////////////
row_xor = (int*) malloc(M*sizeof(int));
if(row_xor == NULL){ printf("Fail to melloc \n\n"); exit(EXIT_FAILURE); }
col_xor = (int*) malloc(N*sizeof(int));
if(col_xor == NULL){ printf("Fail to melloc \n\n"); exit(EXIT_FAILURE); }
uchar *p = image.data;
char buffer[1024] ;
char *record,*line;
i = 0;
j = 0;
uint64_t csvmat_read[numBox][2];
uint64_t csvMat[numBox*2];
/////////////////csv file load/////////////////////////////////////////
FILE *fstream = fopen(argv[2],"r");
if(fstream == NULL)
{
printf("\n file opening failed ");
exit(EXIT_FAILURE);
}
while((line=fgets(buffer,sizeof(buffer),fstream))!=NULL)
{
j=0;
record = strtok(line,",");
while(record != NULL)
{
csvmat_read[i][j] = strtoull(record,0,0) ;
record = strtok(NULL,",");
j++;
}
++i ;
}
for(int i=0;i<numBox;i=i+1){
csvMat[2*i]=csvmat_read[i][0];
csvMat[2*i+1]=csvmat_read[i][1];
}
////////////some varibles and memories malloc///////////////////
int *result_matrix;
int *result_xor;
result_matrix= (int*) malloc(M*box_col*sizeof(int));// this is to store the decimal which is transformed from the 8 digits
if(result_matrix == NULL){ printf("Fail to melloc result_matrix\n\n"); exit(EXIT_FAILURE); }
//printf("%d\n",M*box_col);
result_xor= (int*) malloc(M*sizeof(int));
if(result_xor == NULL){ printf("Fail to melloc result_xor\n\n"); exit(EXIT_FAILURE); }
uchar *temp_image;
temp_image=(uchar*) malloc(M*N*sizeof(uchar));
if(temp_image == NULL){ printf("Fail to melloc p\n\n"); exit(EXIT_FAILURE); }
Mat temp = Mat(M, N, CV_8UC1, temp_image);
struct timeval tt;
double ST, ET; // Local Start and num_times for this thread
double TE; // Local Time Elapsed for this thread
gettimeofday(&tt, NULL);// get the time before the calculation
ST = tt.tv_sec*1000.00 + (tt.tv_usec/1000.0);
/////////////////load checkbox XOR and XOR every line////////////////////////////////////
/////////////////load checkbox for the row, which is the csvmat[][1]/////////////////////
row_xor = rowXOR(p, M);
col_xor = colXOR(p, M);
checkbox_binary_row(csvMat,boxSize,box_col,result_matrix);
get_xor(result_xor,result_matrix,box_col,M);
int flag1=0;
int flag2=0;
int swap[256];
for(int i=0;i<256;i++){
swap[i]=0;
}
for(int j=0;j<N;j++){
for(int i=0;i<M;i++){//swap from this line
if(result_xor[j]==row_xor[i] && swap[i]==0){// if find the targets, then swap
swap[i]=1;
Mat M1 = temp.row(j);
image.row(i).copyTo(M1);
flag1++;
//printf("has swaped column %d and %d and the result_xor is %d the row_xor is %d\n",j,i,result_xor[j],row_xor[i]);
break;
}
}
}
/////////////////load checkbox XOR and XOR every line////////////////////////////////////
/////////////////load checkbox for the column, which is the csvmat[][1]/////////////////////
for(int i=0;i<256;i++){
swap[i]=0;
}
checkbox_binary_column(csvMat,boxSize,box_col,result_matrix);
get_xor(result_xor,result_matrix,box_col,M);
for(int j=0;j<N;j++){
for(int i=0;i<M;i++){//swap from this line
if(result_xor[j]==col_xor[i] && swap[i]==0){// if find the targets, then swap
swap[i]=1;
flag2++;
Mat M2 = image.col(j);
temp.col(i).copyTo(M2);
//printf("has swaped row %d and %dand the result_xor is %d the row_xor is %d\n",j,i,result_xor[j],col_xor[i]);
break;
}
}
}
//printf("%d, %d \n",flag1,flag2);
///////////////////////////////////////////////////////////////////////////////////////
gettimeofday(&tt, NULL);// get the time after the calculation
ET = tt.tv_sec*1000.00 + (tt.tv_usec/1000.0);
TE = (ET-ST); // calculate the total calculating time
printf(" unscramble the image in %f ms\n\n", TE); // display the total calculating time
///////////////////////////////////////////////////////////////////////////////////////////////////
// Display the output image:
Mat result = Mat(M, N, CV_8UC1, image.data);
// and save it to disk:
string output_filename = "unscramble.png";
if (!imwrite(output_filename, result)) {
fprintf(stderr, "couldn't write output to disk!\n");
exit(EXIT_FAILURE);
}
printf("Saved image '%s', size = %dx%d (dims = %d).\n", output_filename.c_str(), result.rows, result.cols, result.dims);
free(row_xor);
free(col_xor);
free(result_matrix);
free(result_xor);
free(temp_image);
exit(EXIT_SUCCESS);
}