-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConverter.cpp
149 lines (115 loc) · 3.53 KB
/
Converter.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
#include "Converter.h"
int Converter::RGB2GrayScale(const Mat& sourceImage, Mat& destinationImage)
{
if (sourceImage.empty()) {
return 0;
}
// lay hang, cot cua anh goc
int rows = sourceImage.rows;
int cols = sourceImage.cols;
// lay chieu dai cua 1 dong anh goc
auto rowStepSrc = sourceImage.step[0];
// lay chieu rong cua 1 pixel anh goc
auto colStepSrc = sourceImage.step[1];
// lay con tro data sourceImage
uchar* pDataSrc = (uchar*)sourceImage.data;
// tao anh xam
destinationImage = Mat(rows, cols, CV_8UC1);
// lay chieu dai cua 1 dong anh xam
auto rowStepDest = destinationImage.step[0];
// lay chieu rong cua 1 pixel anh xam
auto colStepDest = destinationImage.step[1];
// lay con tro data destinationImage
uchar* pDataDest = (uchar*)destinationImage.data;
for (int y = 0; y < rows; y++, pDataSrc += rowStepSrc, pDataDest += rowStepDest) {
uchar* pRowSrc = pDataSrc;
uchar* pRowDest = pDataDest;
for (int x = 0; x < cols; x++, pRowSrc += colStepSrc, pRowDest += colStepDest) {
uchar R = pRowSrc[0];
uchar G = pRowSrc[1];
uchar B = pRowSrc[2];
// cong thuc :https://docs.opencv.org/2.4/modules/imgproc/doc/miscellaneous_transformations.html#void%20cvtColor%28InputArray%20src,%20OutputArray%20dst,%20int%20code,%20int%20dstCn%29
uchar pixel = 0.299f * R + 0.587f * G + 0.114f * B;
*pRowDest = pixel;
}
}
return 1;
}
int Converter::RGB2HSV(const Mat& sourceImage, Mat& destinationImage)
{
if (sourceImage.empty()) {
return 0;
}
// lay hang, cot cua anh goc
int rows = sourceImage.rows;
int cols = sourceImage.cols;
// lay chieu dai cua 1 dong anh goc
auto rowStepSrc = sourceImage.step[0];
// lay chieu rong cua 1 pixel anh goc
auto colStepSrc = sourceImage.step[1];
// lay con tro data sourceImage
uchar* pDataSrc = (uchar*)sourceImage.data;
// tao anh dich
destinationImage = Mat(rows, cols, CV_8UC3);
// lay chieu dai cua 1 dong anh dich
auto rowStepDest = destinationImage.step[0];
// lay chieu rong cua 1 pixel anh dich
auto colStepDest = destinationImage.step[1];
// lay con tro data destinationImage
uchar* pDataDest = (uchar*)destinationImage.data;
for (int y = 0; y < rows; y++, pDataSrc += rowStepSrc, pDataDest += rowStepDest) {
uchar* pRowSrc = pDataSrc;
uchar* pRowDest = pDataDest;
for (int x = 0; x < cols; x++, pRowSrc += colStepSrc, pRowDest += colStepDest) {
// cong thuc :https://docs.opencv.org/2.4/modules/imgproc/doc/miscellaneous_transformations.html#void%20cvtColor%28InputArray%20src,%20OutputArray%20dst,%20int%20code,%20int%20dstCn%29
// chuyen ti le 0-1
float R = pRowSrc[0] / 255.f;
float G = pRowSrc[1] / 255.f;
float B = pRowSrc[2] / 255.f;
float H = 0;
float S = 0;
float V = Util::max(Util::max(R, G), B);
float minRGB = Util::min(Util::min(R, G), B);
float VSubMin = V - minRGB;
if (VSubMin > 0) {
S = VSubMin / V;
if (V == R) {
H = 60 * (G - B) / VSubMin;
}
else if (V == G) {
H = 120 + 60 * (B - R) / VSubMin;
}
else if (V == B) {
H = 240 + 60 * (R - G) / VSubMin;
}
if (H < 0)
{
H += 360;
}
}
pRowDest[0] = H / 2.f;
pRowDest[1] = 255.f * S;
pRowDest[2] = 255.f * V;
}
}
return 1;
}
int Converter::Convert(Mat& sourceImage, Mat& destinationImage, int type)
{
if (type == 0) {
//chuyen rgb sang grayscale
return this->RGB2GrayScale(sourceImage, destinationImage);
}
else if (type == 1) {
//chuyen rgb sang hsv
return this->RGB2HSV(sourceImage, destinationImage);
}
else
return 0;
}
Converter::Converter()
{
}
Converter::~Converter()
{
}