-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode.cpp
More file actions
180 lines (154 loc) · 5.49 KB
/
Copy pathdecode.cpp
File metadata and controls
180 lines (154 loc) · 5.49 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
//
// main.cpp
// IP_partD_decoder
//
// Created by 楊廷禹 on 2019/1/4.
// Copyright © 2019 Yang Ting Yu. All rights reserved.
//
#include <iostream>
#include <opencv.hpp>
using namespace cv;
using namespace std;
unsigned long Hash(string);
void getRandomPointAndChannelArray(int , int , Point2i [], int[], RNG );
void decode(int, int, Mat &, Mat, Point2i[], int[]);
int main(int argc, const char * argv[]) {
// if(argc != 5){
// cerr<<"Usage: " << argv[0] << " <colour carrier path>" <<" <encoded message image path>"<<" <path for storing randomed message>"<<" <path for storing decoded message>"<< endl;
// return -1;
// }
Mat srcColor, srcEncoded;
Mat messageRandom;
String srcColorPath = "colour_carrier.png";
String srcEncodedPath = "outputFromEncoder.png";
String randomMessageWritePath = "messageRandom.png";
String dstWritePath = "messageDecoded.png";
String srcColorWindow = "colour_carrier";
String srcEncodedWindow = "output";
String randomMessageWindow = "Message Randomed";
String dstWindow = "Message";
//read carrier and message image
srcColor = imread(srcColorPath, -1);
srcEncoded = imread(srcEncodedPath, -1);
if (srcColor.empty() || srcEncoded.empty()) {
cerr<<"Image loading error!"<<endl;
}
Mat dst = Mat(srcEncoded.size(),CV_8U);
namedWindow(srcColorWindow, CV_WINDOW_AUTOSIZE);
namedWindow(srcEncodedWindow, CV_WINDOW_AUTOSIZE);
imshow(srcColorWindow, srcColor);
imshow(srcEncodedWindow, srcEncoded);
string password;
cout<<"Enter 8 char password: ";
cin>>password;
RNG rng(Hash(password));
Point2i randomPoint[srcEncoded.rows * srcEncoded.cols];
//to store channel selection order
int selectChannelOrder[srcEncoded.rows * srcEncoded.cols];
getRandomPointAndChannelArray(srcColor.rows, srcColor.cols, randomPoint, selectChannelOrder, rng);
absdiff(srcEncoded, srcColor, messageRandom);
for (int i = 0; i < messageRandom.rows; i++) {
for(int j = 0; j < messageRandom.cols; j++){
int x = randomPoint[i * srcEncoded.cols + j].x;
int y = randomPoint[i * srcEncoded.cols + j].y;
int selectChannel = selectChannelOrder[i * messageRandom.cols + j];
int value = messageRandom.at<Vec3b>(x, y)[selectChannel];
dst.at<uchar>(i, j) = value;
}
}
int cnt = 0; //For channel order
for (int i = 0; i < messageRandom.rows; i++) {
for (int j = 0; j < messageRandom.cols; j++) {
int selectChannel = selectChannelOrder[cnt];
if(messageRandom.at<Vec3b>(i, j)[selectChannel] == 1){
messageRandom.at<Vec3b>(i, j)[selectChannel] = 0;
}
else{
messageRandom.at<Vec3b>(i, j)[0] = 255;
messageRandom.at<Vec3b>(i, j)[1] = 255;
messageRandom.at<Vec3b>(i, j)[2] = 255;
}
cnt++;
}
}
for (int i = 0; i < dst.rows; i++) {
for (int j = 0; j < dst.cols; j++) {
if(dst.at<uchar>(i, j) == 1){
dst.at<uchar>(i, j) = 0;
}
else{
dst.at<uchar>(i, j) = 255;
}
}
}
namedWindow(dstWindow, CV_WINDOW_AUTOSIZE);
imshow(dstWindow, dst);
namedWindow(randomMessageWindow, CV_WINDOW_AUTOSIZE);
imshow(randomMessageWindow, messageRandom);
imwrite(randomMessageWritePath, messageRandom);
imwrite(dstWritePath, dst);
cout<<"Image stored"<<endl;
waitKey(0);
return 0;
}
unsigned long Hash(string str)
{
unsigned long hash = 5381;
for (int i = 0; i < str.length(); i++) {
hash = ((hash << 5) + hash) + str[i]; /* hash * 33 + c */
}
cout<<"hash: "<<hash<<endl;
return hash;
}
void getRandomPointAndChannelArray(int R, int C, Point2i random[], int channel[], RNG rng)
{
for (int i = 0; i < R; i++) {
for (int j = 0 ; j < C; j++) {
random[i * C + j] = Point2i(i,j);
channel[i * C + j] =rng.operator()(3);
}
}
//swap point by RNG
for (int i = 0; i < R * C; i++) {
int order = rng.operator()(R * C);
swap(random[i],random[order]);
swap(channel[i],channel[order]);
}
}
void decode(int R, int C, Mat& dst, Mat randomMessage, Point2i random[], int channel[])
{
for (int i = 0; i < R; i++) {
for(int j = 0; j < C; j++){
int x = random[i * C + j].x;
int y = random[i * C + j].y;
int selectChannel = channel[i * C + j];
int value = randomMessage.at<Vec3b>(x, y)[selectChannel];
dst.at<uchar>(i, j) = value;
}
}
int cnt = 0; //For channel order
for (int i = 0; i < R; i++) {
for (int j = 0; j < C; j++) {
int selectChannel = channel[cnt];
if(randomMessage.at<Vec3b>(i, j)[selectChannel] == 1){
randomMessage.at<Vec3b>(i, j)[selectChannel] = 0;
}
else{
randomMessage.at<Vec3b>(i, j)[0] = 255;
randomMessage.at<Vec3b>(i, j)[1] = 255;
randomMessage.at<Vec3b>(i, j)[2] = 255;
}
cnt++;
}
}
for (int i = 0; i < dst.rows; i++) {
for (int j = 0; j < dst.cols; j++) {
if(dst.at<uchar>(i, j) == 1){
dst.at<uchar>(i, j) = 0;
}
else{
dst.at<uchar>(i, j) = 255;
}
}
}
}