-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
196 lines (167 loc) · 6.79 KB
/
main.cpp
File metadata and controls
196 lines (167 loc) · 6.79 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
#include <opencv2/opencv.hpp>
#include <iostream>
#include <random>
#if CV_MAJOR_VERSION == 4 && CV_MINOR_VERSION == 12 && CV_SUBMINOR_VERSION == 0
#ifdef _DEBUG
#pragma comment(lib, "opencv_world4120d.lib")
#else
#pragma comment(lib, "opencv_world4120.lib")
#endif
#else
#error "OpenCV version must be 4.12.0"
#endif
class VisualCryptography {
private:
cv::Mat original;
int width, height;
std::mt19937 rng;
static const size_t MAX_WIDTH = 400;
static const size_t MAX_HEIGHT = 400;
public:
VisualCryptography(const cv::Mat& input_image) {
try {
if (input_image.empty()) {
throw std::runtime_error("Input image is empty");
}
// Resize if image is too large
cv::Mat resized;
if (input_image.cols > MAX_WIDTH || input_image.rows > MAX_HEIGHT) {
double scale = std::min(
static_cast<double>(MAX_WIDTH) / input_image.cols,
static_cast<double>(MAX_HEIGHT) / input_image.rows
);
cv::resize(input_image, resized, cv::Size(), scale, scale);
}
else {
resized = input_image.clone();
}
// Convert to binary
if (resized.channels() == 3) {
cv::Mat gray;
cv::cvtColor(resized, gray, cv::COLOR_BGR2GRAY);
cv::threshold(gray, original, 128, 255, cv::THRESH_BINARY);
}
else {
cv::threshold(resized, original, 128, 255, cv::THRESH_BINARY);
}
width = original.cols;
height = original.rows;
rng.seed(std::random_device()());
std::cout << "Image processed. Size: " << width << "x" << height << std::endl;
}
catch (const std::exception& e) {
throw std::runtime_error("Error in constructor: " + std::string(e.what()));
}
}
std::pair<cv::Mat, cv::Mat> generateShares() {
try {
cv::Mat share1 = cv::Mat::zeros(height * 2, width * 2, CV_8UC1);
cv::Mat share2 = cv::Mat::zeros(height * 2, width * 2, CV_8UC1);
const uchar patterns[2][2][2] = {
{{255, 0}, {0, 255}}, // Pattern 1
{{0, 255}, {255, 0}} // Pattern 2
};
std::uniform_int_distribution<int> dist(0, 1);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
uchar pixel = original.at<uchar>(i, j);
int pattern_idx = dist(rng);
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
int row = i * 2 + x;
int col = j * 2 + y;
if (pixel == 255) { // White pixel
share1.at<uchar>(row, col) = patterns[pattern_idx][x][y];
share2.at<uchar>(row, col) = patterns[pattern_idx][x][y];
}
else { // Black pixel
share1.at<uchar>(row, col) = patterns[pattern_idx][x][y];
share2.at<uchar>(row, col) = patterns[pattern_idx][x][y] == 255 ? 0 : 255;
}
}
}
}
}
return std::make_pair(share1, share2);
}
catch (const std::exception& e) {
throw std::runtime_error("Error generating shares: " + std::string(e.what()));
}
}
cv::Mat overlayShares(const cv::Mat& share1, const cv::Mat& share2) {
try {
if (share1.size() != share2.size()) {
throw std::runtime_error("Shares have different sizes");
}
cv::Mat result = cv::Mat::zeros(share1.size(), CV_8UC1);
cv::bitwise_and(share1, share2, result);
return result;
}
catch (const std::exception& e) {
throw std::runtime_error("Error in overlay: " + std::string(e.what()));
}
}
};
// Function to create test image
cv::Mat createTestImage(int width = 100, int height = 100) {
cv::Mat image = cv::Mat::ones(height, width, CV_8UC1) * 255;
cv::putText(image, "TEST", cv::Point(10, height / 2),
cv::FONT_HERSHEY_SIMPLEX, 1.0, cv::Scalar(0), 2);
return image;
}
int main() {
try {
std::cout << "Visual Cryptography Demo\n" << std::endl;
// Create test image
std::cout << "Creating test image..." << std::endl;
cv::Mat testImage = createTestImage();
if (testImage.empty()) {
throw std::runtime_error("Failed to create test image");
}
// Initialize visual cryptography
std::cout << "Initializing visual cryptography..." << std::endl;
VisualCryptography vc(testImage);
// Generate shares
std::cout << "Generating shares..." << std::endl;
auto shares = vc.generateShares();
cv::Mat& share1 = shares.first;
cv::Mat& share2 = shares.second;
// Save shares
std::cout << "Saving shares..." << std::endl;
cv::imwrite("share1.png", share1);
cv::imwrite("share2.png", share2);
// Create overlay
std::cout << "Creating overlay..." << std::endl;
cv::Mat result = vc.overlayShares(share1, share2);
cv::imwrite("result.png", result);
// Display images
cv::namedWindow("Original", cv::WINDOW_NORMAL);
cv::namedWindow("Share 1", cv::WINDOW_NORMAL);
cv::namedWindow("Share 2", cv::WINDOW_NORMAL);
cv::namedWindow("Result", cv::WINDOW_NORMAL);
cv::resizeWindow("Original", 400, 400);
cv::resizeWindow("Share 1", 400, 400);
cv::resizeWindow("Share 2", 400, 400);
cv::resizeWindow("Result", 400, 400);
cv::imshow("Original", testImage);
cv::imshow("Share 1", share1);
cv::imshow("Share 2", share2);
cv::imshow("Result", result);
std::cout << "\nImages have been generated and saved:" << std::endl;
std::cout << "- share1.png" << std::endl;
std::cout << "- share2.png" << std::endl;
std::cout << "- result.png" << std::endl;
std::cout << "\nPress any key to exit..." << std::endl;
cv::waitKey(0);
cv::destroyAllWindows();
}
catch (const cv::Exception& e) {
std::cerr << "OpenCV error: " << e.what() << std::endl;
return -1;
}
catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return -1;
}
return 0;
}