/
BinaryMat.cpp
85 lines (72 loc) · 1.51 KB
/
BinaryMat.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
#include "BinaryMat.h"
namespace aa
{
BinaryMat::BinaryMat(const cv::Mat* mat)
{
setMat(mat);
}
BinaryMat::~BinaryMat()
{
delete [] bytes;
}
void BinaryMat::setMat(const cv::Mat* mat)
{
setVariables(mat->rows, mat->cols);
bytesFromMat(mat);
}
bool BinaryMat::matFromBytes(cv::Mat* mat)
{
if (mat->rows != r || mat->cols != c)
{
return false;
}
int bitIndex = 0;
int byteIndex = 0;
for(int i = 0; i < r; i++)
{
uchar* rowData = (uchar*)mat->ptr(i); // points to the beginning of the i'th row
for(int j = 0; j < c; j++)
{
uchar pixel = bytes[byteIndex].get(bitIndex);
rowData[j] = bytes[byteIndex].get(bitIndex);
bitIndex = (bitIndex + 1) % BYTE_SIZE;
byteIndex = bitIndex == 0 ? byteIndex + 1 : byteIndex;
}
}
return true;
}
int BinaryMat::rows()
{
return r;
}
int BinaryMat::cols()
{
return c;
}
void BinaryMat::setVariables(int rows, int cols)
{
r = rows;
c = cols;
bytes = new Byte[getBytesSize()];
}
int BinaryMat::getBytesSize()
{
return (int)(ceil(1.0 * r * c / BYTE_SIZE));
}
void BinaryMat::bytesFromMat(const cv::Mat* mat)
{
int bitIndex = 0;
int byteIndex = 0;
for(int i = 0; i < mat->rows; i++)
{
uchar* rowData = (uchar*)mat->ptr(i); // points to the beginning of the i'th row
for(int j = 0; j < mat->cols; j++)
{
uchar pixel = rowData[j];
bytes[byteIndex].set(bitIndex, rowData[j]);
bitIndex = (bitIndex + 1) % BYTE_SIZE;
byteIndex = bitIndex == 0 ? byteIndex + 1 : byteIndex;
}
}
}
}