-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorName.cpp
More file actions
163 lines (128 loc) · 3.76 KB
/
Copy pathColorName.cpp
File metadata and controls
163 lines (128 loc) · 3.76 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
#include "ColorName.h"
#include <fstream>
/*!
Convert image to color names.
@param colorNames [in] vector<int> color names.
@param src [in] Mat, source image, CV_8UC3.
@param dst [out] Mat, result image, CV_8UC1, value 1~11.
@return int, 0x0000 = Successfully.
*/
int ConvertImage2ColorNames(vector<int>& colorNames, InputArray src, OutputArray dst)
{
Mat source = src.getMat();
if (source.empty() || source.type() != CV_8UC3) return 0x0001; /*!< Source image, not supported type. */
dst.create(source.size(), CV_8UC1);
Mat destination = dst.getMat();
auto srcStart = source.begin<Vec3b>();
auto srcEnd = source.end<Vec3b>();
auto dstStart = destination.begin<uchar>();
while (srcStart != srcEnd)
{
Vec3b bgr = *srcStart;
int index = bgr[2] / 8 + bgr[1] / 8 * 32 + bgr[0] / 8 * 1024;
*dstStart = colorNames[index];
++srcStart;
++dstStart;
}
return 0x0000;
}
/*!
Convert image to color.
@param colorNames [in] vector<int> color names.
@param src [in] Mat, source image, CV_8UC3.
@param dst [out] Mat, result image, CV_8UC3.
@return int, 0x0000 = Successfully.
*/
int ConvertImage2Color(vector<int>& colorNames, InputArray src, OutputArray dst)
{
Mat source = src.getMat();
if (source.empty() || source.type() != CV_8UC3) return 0x0001; /*!< Source image, not supported type. */
dst.create(source.size(), CV_8UC3);
Mat destination = dst.getMat();
const uchar COLOR_VALUES[11][3] = {{0, 0, 0},{255, 0, 0},{64, 102, 128},{128, 128, 128},{0, 255, 0},{0, 204, 255},{255, 128, 255},{255, 0, 255},{0, 0, 255},{255, 255, 255},{0, 255, 255}};
auto srcStart = source.begin<Vec3b>();
auto srcEnd = source.end<Vec3b>();
auto dstStart = destination.begin<Vec3b>();
while (srcStart != srcEnd)
{
Vec3b bgr = *srcStart;
int index = bgr[2] / 8 + bgr[1] / 8 * 32 + bgr[0] / 8 * 1024;
int color = colorNames[index];
*dstStart = Vec3b(COLOR_VALUES[color - 1]);
++srcStart;
++dstStart;
}
return 0x0000;
}
/*!
Read color indexs from specified file.
@param filePath [in] string, file path.
@param colorNames [out] vector<int>, color indexs.
@return int, 0x0000 = read successfully.
*/
int ReadConfigs(string filePath, vector<int>& colorNames)
{
fstream f = fstream(filePath, ios::in);
if (!f.is_open()) return 0x0001; /*!< File is not opened. */
const int COLOR_ROW_COUNTS = 32768;
const int COLOR_COL_COUNTS = 11;
colorNames.resize(COLOR_ROW_COUNTS);
int readCounts = 0;
while (!f.eof())
{
int color = 0;
double value, maxValue = 0;
// abandon rgb color values.
f >> value;
f >> value;
f >> value;
if (f.eof()) break;
for (int i = 0; i < COLOR_COL_COUNTS; i++)
{
f >> value;
if (value > maxValue)
{
maxValue = value;
color = i + 1;
}
}
colorNames[readCounts] = color;
readCounts++;
}
if (readCounts != COLOR_ROW_COUNTS) return 0x0002; /*!< Not enough configs. */
return 0x0000; /*!< Successfully. */
}
/*!
Read color probability indexs from specified file.
@param filePath [in] string, file path.
@param colorNames [out] vector<int>, color probability indexs.
@return [int] 0x0000 = read successfully.
*/
int ReadConfigs(string filePath, vector<vector<double>>& colorNames)
{
fstream f = fstream(filePath, ios::in);
if (!f.is_open()) return 0x0001; /*!< File is not opened. */
const int COLOR_ROW_COUNTS = 32768;
const int COLOR_COL_COUNTS = 11;
colorNames.resize(COLOR_ROW_COUNTS);
int readCounts = 0;
while (!f.eof())
{
vector<double> color;
double value;
// abandon rgb color values.
f >> value;
f >> value;
f >> value;
if (f.eof()) break;
for (int i = 0; i < COLOR_COL_COUNTS; i++)
{
f >> value;
color.push_back(value);
}
colorNames[readCounts] = color;
readCounts++;
}
if (readCounts != COLOR_ROW_COUNTS) return 0x0002; /*!< Not enough configs. */
return 0x0000; /*!< Successfully. */
}