This repository was archived by the owner on Oct 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMFSColor.cpp
More file actions
192 lines (169 loc) · 4.82 KB
/
MFSColor.cpp
File metadata and controls
192 lines (169 loc) · 4.82 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
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
#include "MFSColor.h"
#include <boost/algorithm/string.hpp>
//#include <stdio.h>
//#include <math.h>
using namespace cv;
/***********************************************************************
* (DE)CONSTRUCTOR(S)
**********************************************************************/
//MFSColor::MFSColor(unsigned int w, unsigned int h, unsigned int k, unsigned int k_t, unsigned int t, unsigned char sE, unsigned char sN, float cE, float cD, const char* c, string d): MFSquare(w, h, k, k_t, t, sE, sN, cE, cD, c)
MFSColor::MFSColor(unsigned int w, unsigned int h, unsigned int k, unsigned int k_t, unsigned int t, unsigned char sE, const char *cn, float cs, const char* c, string d): MFSquare(w, h, k, k_t, t, sE, cn, cs, c)
{
initStart(d);
init();
initEnd();
}
/***********************************************************************
* INITALIZATION METHODS
**********************************************************************/
void MFSColor::init()
{
initColors();
}
void MFSColor::initColors()
{
colors = vector<Scalar>();
unsigned char r, g, b;
string s = boost::to_lower_copy(colorsText);
unsigned int i = 0;
unsigned int rangeHere = 0;
while (i < s.length() || rangeHere < range)
{
if (i+MF_COLOR_RGB_HEXA_LENGTH <= s.length())
{
if (s[i] != MF_COLOR_RGB_HEXA_START)
{
cerr << "Warning: not the proper hexa format is used!" << endl;
}
r = (unsigned char) getNumericValue(s[i+1],16)*16 + getNumericValue(s[i+2],16);
g = (unsigned char) getNumericValue(s[i+3],16)*16 + getNumericValue(s[i+4],16);
b = (unsigned char) getNumericValue(s[i+5],16)*16 + getNumericValue(s[i+6],16);
colors.push_back(Scalar( b, g, r));
rangeHere++;
}
else
{
cerr << "Warning: zero values added!" << endl;
colors.push_back(Scalar( 0, 0, 0));
rangeHere++;
}
i += MF_COLOR_RGB_HEXA_LENGTH;
if (i < s.length() && s[i] != MF_COLOR_RGB_SEPARATOR)
{
cerr << "Warning: not the proper separator used!" << endl;
}
i++;
}
}
/***************************************************************
* CHECK ATTRIBUTES
**************************************************************/
void MFSColor::checkAttributes()
{
MFSquare::checkAttributes();
checkColors();
}
void MFSColor::checkColors()
{
string s = colorsText;
if (s.length() != range * MF_COLOR_RGB_HEXA_LENGTH + range - 1)
{
throw MarkerFieldException("Colors' text does not fit with range!");
}
}
/***********************************************************************
* COST CALCULATIONS
**********************************************************************/
void MFSColor::checkNeighbors()
{
Scalar v1, v2;
for (unsigned int y = 0; y < height; y++)
{
unsigned long y_times_width = y * width;
for (unsigned int x = 0; x < width; x++)
{
v1 = getRGBValue(dataField[y_times_width + x]);
if (y < height-1)
{
v2 = getRGBValue(dataField[y_times_width+width + x]);
incCostNeighbor(y_times_width + x, v1[0], y_times_width+width + x, v2[0]);
incCostNeighbor(y_times_width + x, v1[1], y_times_width+width + x, v2[1]);
incCostNeighbor(y_times_width + x, v1[2], y_times_width+width + x, v2[2]);
}
if (x < width-1)
{
v2 = getRGBValue(dataField[y_times_width + x+1]);
incCostNeighbor(y_times_width + x, v1[0], y_times_width + x+1, v2[0]);
incCostNeighbor(y_times_width + x, v1[1], y_times_width + x+1, v2[1]);
incCostNeighbor(y_times_width + x, v1[2], y_times_width + x+1, v2[2]);
}
}
}
}
void MFSColor::recalculateCost()
{
MFSquare::recalculateCost();
checkNeighbors();
}
/***********************************************************************
* IMPLEMENTED PURE VIRTUAL FUNCTIONS FROM MARKER FIELD
**********************************************************************/
const char* MFSColor::getClassName()
{
return "Color Square Marker Field";
}
/***********************************************************************
* IMPLEMENTED PURE VIRTUAL FUNCTIONS FROM MARKER FIELD SQUARE
**********************************************************************/
unsigned char MFSColor::compareTwo(unsigned int first, unsigned int second) {
Scalar c1 = getRGBValue(first);
Scalar c2 = getRGBValue(second);
unsigned char ret = 0;
// BLUE
if (c1[0] > c2[0])
{
ret += MARKERFIELD_COMPARE_GREATER;
}
else if (c1[0] == c2[0])
{
ret += MARKERFIELD_COMPARE_EQUAL;
}
else
{
ret += MARKERFIELD_COMPARE_SMALLER;
}
ret = ret << 2;
// GREEN
if (c1[1] > c2[1])
{
ret += MARKERFIELD_COMPARE_GREATER;
}
else if (c1[1] == c2[1])
{
ret += MARKERFIELD_COMPARE_EQUAL;
}
else
{
ret += MARKERFIELD_COMPARE_SMALLER;
}
ret = ret << 2;
// RED
if (c1[2] > c2[2])
{
ret += MARKERFIELD_COMPARE_GREATER;
}
else if (c1[2] == c2[2])
{
ret += MARKERFIELD_COMPARE_EQUAL;
}
else
{
ret += MARKERFIELD_COMPARE_SMALLER;
}
ret = ret << 2;
return ret;
}
Scalar MFSColor::getRGBValue(unsigned int v)
{
return colors[v];
}