/
Encoder.cpp
186 lines (155 loc) · 5.9 KB
/
Encoder.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
/*
* Encoder.cpp
*
* Created on: 12 Oct 2015
* Author: sohamghosh
*/
#include "Encoder.h"
#include "SBHE.h"
#include "Wavelet.h"
#include <cmath>
#include <fstream>
using namespace cv;
Encoder::Encoder(Mat img) {
this->img = img;
this->img.convertTo(this->img, CV_32FC1);
this->keyPhi = getPhi(Options::Mk);
this->nonkeyPhi = getPhi(Options::Mw);
assert(img.rows % (Options::blockSize * Options::M) == 0); // divisibility assertions
assert(img.cols % (Options::blockSize * Options::M) == 0);
}
cv::Mat Encoder::getPhi(double measurementRate){ // measurement rate = Mr/B^2
double B_sq = Options::blockSize * Options::blockSize;
return getPhi(int(measurementRate * B_sq), B_sq);
}
cv::Mat Encoder::getPhi(int m, int n){
SBHE h = SBHE(m, n, 61);
return h.getSBHEmat();
}
Mat Encoder::getNthBlock(int n, Mat gob){ // this is 0 indexed
assert(n >= 0 && n < Options::M * Options::M);
int rowStart = int(n / Options::M) * Options::blockSize;
int colStart = int(n % Options::M) * Options::blockSize;
// std::cout << rowStart << ", " << colStart << "\n";
return gob.colRange(colStart, colStart+Options::blockSize).rowRange(rowStart, rowStart + Options::blockSize).clone().reshape(1, pow(Options::blockSize, 2));
}
/*
* GOB: Group Of Blocks
* - Each GOB has M x M blocks
*/
Mat Encoder::getGOB(int n){
assert(n >= 0 && n < (this->img.cols * this->img.rows/(pow(Options::blockSize, 2) * pow(Options::M, 2))));
int rowStart, colStart;
int nc = this->f.cols / (Options::blockSize * Options::M);
colStart = (n % nc) * Options::blockSize * Options::M;
rowStart = (n / nc) * Options::blockSize * Options::M;
Mat x = Mat(this->f.colRange(colStart, colStart+Options::blockSize * Options::M).rowRange(rowStart, rowStart+Options::blockSize * Options::M));
Mat GOB = x.clone();
return GOB;
}
/*
* Get CS measurements
* y = phi * x
*/
Mat Encoder::encodeBlock(Mat x, Mat phi){ // (MxN) x (Nx1)
Mat res = Mat(phi.rows, 1, CV_32FC1);
Mat zero = Mat::zeros(phi.rows, 1, CV_32FC1);
gemm(phi, x, 1.0, noArray(), 0.0, res);
return res;
}
Mat Encoder::encodeKeyBlock(Mat x){
return encodeBlock(x, this->keyPhi);
}
Mat Encoder::encodeNonKeyBlock(Mat x){
return encodeBlock(x, this->nonkeyPhi);
}
void Encoder::encodeImage(){
int numGOBs = this->img.cols * this->img.rows/(pow(Options::blockSize * Options::M, 2));
Mat y;
this->f = Wavelet(this->img, Wavelet::DWT).getResult(); // wavelet transform (CDF 9/7)
f = f.reshape(1, this->img.cols * this->img.rows); // resize to N^2 x 1 vector
f = SBHE::scrambleInputSignal(f, Options::A).reshape(1, img.rows);
for (int i = 0; i < numGOBs; i++){
Mat gob = getGOB(i);
for (int j = 0; j < pow(Options::M, 2); j++){
Mat block = getNthBlock(j, gob); // given offset within GOB, get block (Nx1)
if (j == 0){ // first block of every GOB is a key block
y = encodeKeyBlock(block);
} else {
y = encodeNonKeyBlock(block);
}
encoded.push_back(y);
}
}
}
cv::Mat Encoder::getKeyPhi(){
return this->keyPhi;
}
cv::Mat Encoder::getnonkeyPhi(){
return this->nonkeyPhi;
}
std::vector<cv::Mat> Encoder::getEncodedValues(){
return this->encoded;
}
void Encoder::dumpEncoding(char * fileName){
std::cout << "Dumping encoded values to " << fileName << "\n";
int b_sq = Options::blockSize * Options::blockSize;
int numBlocks = img.rows * img.cols / (Options::blockSize * Options::blockSize);
int numKeyBlocks = 1.0/(Options::M * Options::M) * numBlocks;
int numNonKeyBlocks = numBlocks - numKeyBlocks;
size_t num_items = (int(Options::Mk * b_sq) * numKeyBlocks + int(Options::Mw * b_sq) * numNonKeyBlocks); // number of key blocks * measurements from key blocks + number of non key blocks * measurements from non key blocks
cv::Mat saveMat = Mat::zeros(1, num_items, CV_32FC1);
std::ofstream file;
file.open(std::string("bit_") + std::string(fileName), std::ofstream::out | std::ofstream::binary);
bool negativeMap[num_items];
int c = 0;
for (int i = 0; i < encoded.size(); i++){
for (int j = 0; j < encoded[i].rows; j++, c++){
saveMat.at<float>(0, c) = 1000 * abs(encoded[i].at<float>(j, 0));
bool isNegative = encoded[i].at<float>(j, 0) < 0.0;
negativeMap[c] = isNegative;
}
}
saveMat.convertTo(saveMat, CV_16U);
file.write((char *)negativeMap, num_items/8); // num_items bits --> bytes
cv::imwrite(std::string(fileName), saveMat);
}
void Encoder::loadEncoding(char * fileName){
std::cout << "Loading encoded values from " << fileName << "\n";
// Do some calculations to get sizes of vectors/mat required
int b_sq = Options::blockSize * Options::blockSize;
int numBlocks = img.rows * img.cols / (Options::blockSize * Options::blockSize);
int numKeyBlocks = 1.0/(Options::M * Options::M) * numBlocks;
int numNonKeyBlocks = numBlocks - numKeyBlocks;
size_t num_items = (int(Options::Mk * b_sq) * numKeyBlocks + int(Options::Mw * b_sq) * numNonKeyBlocks); // number of key blocks * measurements from key blocks + number of non key blocks * measurements from non key blocks
encoded.clear();
cv::Mat loadMat = cv::imread(std::string(fileName), CV_LOAD_IMAGE_ANYDEPTH); // Load values
std::ifstream file;
file.open(std::string("bit_") + std::string(fileName), std::ifstream::in | std::ifstream::binary);
bool negativeMap[num_items];
file.read((char *) negativeMap, num_items/8);
// load bit map of negative/positive
cv::Mat thisBlock;
int c = 0;
loadMat.convertTo(loadMat, CV_32FC1);
loadMat = loadMat / 1000;
int multiplier = 1;
for (int i = 0; i < numBlocks; i++){
if (i % (Options::M * Options::M) == 0){
thisBlock = Mat::zeros(Options::Mk * Options::blockSize * Options::blockSize, 1, CV_32FC1);
} else {
thisBlock = Mat::zeros(Options::Mw * Options::blockSize * Options::blockSize, 1, CV_32FC1);
}
for (int j = 0; j < thisBlock.rows; j++, c++){
if (negativeMap[c]) {
multiplier = -1;
} else {
multiplier = 1;
}
thisBlock.at<float>(j, 0) = multiplier * loadMat.at<float>(0, c);
}
encoded.push_back(thisBlock);
}
}
Encoder::~Encoder() {
}