-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathExtract.cpp
195 lines (168 loc) · 7.05 KB
/
Extract.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
#pragma once
#include "stdafx.h"
#include "Headers.h"
#define m 38
#define n 107
#define D 106
//////////////////////////////////////////////////////////////////////////////////////////
//// 추출
//////////////////////////////////////////////////////////////////////////////////////////
void ExtractWatermark(Mat& marked_img)
{
Extract(marked_img);
}
/* DWT-DCT-CRT 코드 */
float Extract_CRT(Mat blocks)
{
int Z = blocks.at<float>(0, 0);
if (Z == 40 || Z == 44) {
Z += 60;
}
int p = Z % m;
int q = Z % n;
int d = abs(p - q);
int b = p + q;
// 기준 값을 대상으로 QR의 흰 부분과 검은 부분 구성
if (b >= (D + 35) / 2) // watermark bit 1
{
return 255;
}
else // watermark bit 0
{
return 0;
}
}
void Extract(Mat& Marked_Image)
{
//imshow("Marked_Image", Marked_Image);
Mat yuv_arr[3];
cvtColor(Marked_Image, Marked_Image, COLOR_RGB2YCrCb); // RGB to YCrCb
split(Marked_Image, yuv_arr); // 채널 분리
Mat Marked_Y_channel = Mat(Marked_Image.cols, Marked_Image.rows, Marked_Image.type());
int QRcodeSize;
yuv_arr[0].copyTo(Marked_Y_channel); // Y 채널 분리
yuv_arr[0].convertTo(Marked_Y_channel, CV_32F); //uchar -> float
// 추출한 QRcode 계수들을 저장할 행렬
//Mat HH_recoverd_QRcode_Pixel = Mat(32, 32, CV_8UC1);
//Mat HL_recoverd_QRcode_Pixel = Mat(32, 32, CV_8UC1);
Mat LH_recoverd_QRcode_Pixel = Mat(32, 32, CV_8UC1);
Mat WT_result;
yuv_arr[0].convertTo(Marked_Y_channel, CV_32F); //uchar -> float
WT(Marked_Y_channel, WT_result, 1); // 분리한 Y 채널을 대상으로 1단계 DWT 진행
//imshow("Extracted_Image_WT", WT_result);
// 부대역의 계수를 저장할 행렬 변수
//Mat HH_subband = Mat(WT_result.cols / 2, WT_result.rows / 2, WT_result.type());
// Mat HL_subband = Mat(WT_result.cols / 2, WT_result.rows / 2, WT_result.type());
Mat LH_subband = Mat(WT_result.cols / 2, WT_result.rows / 2, WT_result.type());
//HH_subband = WT_result(Rect(WT_result.cols / 2, WT_result.rows / 2, WT_result.cols / 2, WT_result.rows / 2)); // HH
//HL_subband = WT_result(Rect(WT_result.cols / 2, 0, WT_result.cols / 2, WT_result.rows / 2));
LH_subband = WT_result(Rect(0, WT_result.rows / 2, WT_result.cols / 2, WT_result.rows / 2));
//LL_subband = WT_result(Rect(0, 0, WT_result.rows / 2, WT_result.cols / 2)); // LL
// DCT를 진행할 8x8 크기의 블럭들
Size blockSize(8, 8);
//vector<Mat> HH_blocks; // 각 부대역의 블럭들
//vector<Mat> HL_blocks;
vector<Mat> LH_blocks;
// 256x256 크기의 부대역을 1024개의 8x8 블럭 사이즈로 분할
for (int y = 0; y < 256; y += blockSize.height)
{
for (int x = 0; x < 256; x += blockSize.width)
{
Rect rect = Rect(x, y, blockSize.width, blockSize.height);
//HH_blocks.push_back(Mat(HH_subband, rect));
//HL_blocks.push_back(Mat(HL_subband, rect));
LH_blocks.push_back(Mat(LH_subband, rect));
}
}
int x = 0, y = 0;
// cout << "EXTRACTED" << endl;
int enter = 0;
// 1024개의 8*8 블록에 dct 적용 후 값 출력
for (int i = 0; i < 1024; i++)
{
//dct(HH_blocks[i], HH_blocks[i]);
//dct(HL_blocks[i], HL_blocks[i]);
dct(LH_blocks[i], LH_blocks[i]);
enter++;
if (enter == 32) {
enter = 0;
}
}
// 각 부대역의 1024개의 블럭들을 대상으로 삽입된 워터마크 추출 진행
for (int i = 0; i < 1024; i++)
{
//HH_recoverd_QRcode_Pixel.at<uchar>((int)(y), (int)(x)) = Extract_CRT(HH_blocks[i]);
//HL_recoverd_QRcode_Pixel.at<uchar>((int)(y), (int)(x)) = Extract_CRT(HL_blocks[i]);
LH_recoverd_QRcode_Pixel.at<uchar>((int)(y), (int)(x++)) = Extract_CRT(LH_blocks[i]);
if (x == 32)
{
y++;
x = 0;
}
}
// QRcode를 생성할 행렬 변수 설정
//QRcodeSize = HL_recoverd_QRcode_Pixel.rows;
QRcodeSize = LH_recoverd_QRcode_Pixel.rows;
//Mat QR_HH(QRcodeSize + 2, QRcodeSize + 2, HH_recoverd_QRcode_Pixel.type(), Scalar(255));
//Mat QR_HL(QRcodeSize + 2, QRcodeSize + 2, HL_recoverd_QRcode_Pixel.type(), Scalar(255));
Mat QR_LH(QRcodeSize + 2, QRcodeSize + 2, LH_recoverd_QRcode_Pixel.type(), Scalar(255));
// 결정된 QRcode의 픽셀 값을 위치에 맞게 저장
for (int i = 0; i < 32; i++)
{
for (int j = 0; j < 32; j++)
{
//QR_HH.at<uchar>(i + 1, j + 1) = HH_recoverd_QRcode_Pixel.at<uchar>(i, j);
//QR_HL.at<uchar>(i + 1, j + 1) = HL_recoverd_QRcode_Pixel.at<uchar>(i, j);
QR_LH.at<uchar>(i + 1, j + 1) = LH_recoverd_QRcode_Pixel.at<uchar>(i, j);
}
}
// 32x32 크기의 QR의 크기 100x100으로 확장
//Mat BIG_QR_HH(100, 100, HH_recoverd_QRcode_Pixel.type(), Scalar(255));
//Mat BIG_QR_HL(100, 100, HL_recoverd_QRcode_Pixel.type(), Scalar(255));
Mat BIG_QR_LH(100, 100, LH_recoverd_QRcode_Pixel.type(), Scalar(255));
int nn = 0;
for (int i = 0; i < 32; i++)
{
for (int j = 0; j < 32; j++)
{
//BIG_QR_HH.at<uchar>(nn, 3 * j) = QR_HH.at<uchar>(i, j);
//BIG_QR_HH.at<uchar>(nn, 3 * j + 1) = QR_HH.at<uchar>(i, j);
//BIG_QR_HH.at<uchar>(nn, 3 * j + 2) = QR_HH.at<uchar>(i, j);
//BIG_QR_HH.at<uchar>(nn + 1, 3 * j) = QR_HH.at<uchar>(i, j);
//BIG_QR_HH.at<uchar>(nn + 1, 3 * j + 1) = QR_HH.at<uchar>(i, j);
//BIG_QR_HH.at<uchar>(nn + 1, 3 * j + 2) = QR_HH.at<uchar>(i, j);
//BIG_QR_HH.at<uchar>(nn+ 2, 3 * j) = QR_HH.at<uchar>(i, j);
//BIG_QR_HH.at<uchar>(nn + 2, 3 * j + 1) = QR_HH.at<uchar>(i, j);
//BIG_QR_HH.at<uchar>(nn + 2, 3 * j + 2) = QR_HH.at<uchar>(i, j);
//BIG_QR_HL.at<uchar>(nn, 3 * j) = QR_HL.at<uchar>(i, j);
//BIG_QR_HL.at<uchar>(nn, 3 * j + 1) = QR_HL.at<uchar>(i, j);
//BIG_QR_HL.at<uchar>(nn, 3 * j + 2) = QR_HL.at<uchar>(i, j);
//BIG_QR_HL.at<uchar>(nn + 1, 3 * j) = QR_HL.at<uchar>(i, j);
//BIG_QR_HL.at<uchar>(nn + 1, 3 * j + 1) = QR_HL.at<uchar>(i, j);
//BIG_QR_HL.at<uchar>(nn + 1, 3 * j + 2) = QR_HL.at<uchar>(i, j);
//BIG_QR_HL.at<uchar>(nn + 2, 3 * j) = QR_HL.at<uchar>(i, j);
//BIG_QR_HL.at<uchar>(nn + 2, 3 * j + 1) = QR_HL.at<uchar>(i, j);
//BIG_QR_HL.at<uchar>(nn + 2, 3 * j + 2) = QR_HL.at<uchar>(i, j);
BIG_QR_LH.at<uchar>(nn, 3 * j) = QR_LH.at<uchar>(i, j);
BIG_QR_LH.at<uchar>(nn, 3 * j + 1) = QR_LH.at<uchar>(i, j);
BIG_QR_LH.at<uchar>(nn, 3 * j + 2) = QR_LH.at<uchar>(i, j);
BIG_QR_LH.at<uchar>(nn + 1, 3 * j) = QR_LH.at<uchar>(i, j);
BIG_QR_LH.at<uchar>(nn + 1, 3 * j + 1) = QR_LH.at<uchar>(i, j);
BIG_QR_LH.at<uchar>(nn + 1, 3 * j + 2) = QR_LH.at<uchar>(i, j);
BIG_QR_LH.at<uchar>(nn + 2, 3 * j) = QR_LH.at<uchar>(i, j);
BIG_QR_LH.at<uchar>(nn + 2, 3 * j + 1) = QR_LH.at<uchar>(i, j);
BIG_QR_LH.at<uchar>(nn + 2, 3 * j + 2) = QR_LH.at<uchar>(i, j);
}
nn += 3;
}
//imshow("HH_Extracted_QRcode", BIG_QR_HH);
imshow("LH_Extracted_QRcode", BIG_QR_LH);
//imshow("HL_Extracted_QRcode", BIG_QR_HL);
//imwrite("HH_Extracted_QRcode.png", BIG_QR_HH);
imwrite("LH_Extracted_QRcode.png", BIG_QR_LH);
//imwrite("HL_Extracted_QRcode.png", BIG_QR_HL);
cout << "**********DWT-DCT-CRT**********" << endl;
cvtColor(Marked_Image, Marked_Image, COLOR_YCrCb2RGB); // YCrCb to RGB
getPSNR(Marked_Image); // 원본 이미지와 삽입 이미지의 PSNR 값 계산을 위함
getNCC(); // 삽입된 워터마크와 추출된 워터마크 간 NCC 값 계산
}