2 #include "ColorSpaces.h" 48 base = Mat(classes.size(), CV_8UC3);
55 cvtColor(base, base, cv::ColorConversionCodes::COLOR_BGR2GRAY);
56 cvtColor(base, base, cv::ColorConversionCodes::COLOR_GRAY2RGB);
60 DGM_ASSERT_MSG(base.channels() == 3,
"Base image has %d channel(s), but must have 3.", base.channels());
61 DGM_ASSERT_MSG(classes.channels() == 1,
"Class Map has %d channel(s), but must have 1.", classes.channels());
63 for (
int y = 0; y < base.rows; y++) {
64 byte * pBase = base.ptr<byte>(y);
65 const byte * pClass = classes.ptr<byte>(y);
66 for (
int x = 0; x < base.cols; x++) {
69 for (
int c = 0; c < 3; c++)
70 if (mapping && !((flag &
MARK_OVER) ==
MARK_OVER)) pBase[3 * x + c] =
static_cast<byte
>((pBase[3 * x + c] +
m_vPalette.at(pClass[x] % n).first.val[c]) / 2);
71 else pBase[3 * x + c] =
static_cast<byte
>(
m_vPalette.at(pClass[x] % n).first.val[c]);
78 DGM_ASSERT(!potential.empty());
80 if (potential.dims == 2) {
81 if (potential.cols == 1)
return drawVector(potential, flag);
88 const byte nStates = confusionMat.rows;
96 confusionMat.copyTo(tmp);
97 for (byte y = 0; y < nStates; y++) {
98 float *pTmp = tmp.ptr<
float>(y);
100 for (byte x = 0; x < nStates; x++) sum += pTmp[x];
101 for (byte x = 0; x < nStates; x++) pTmp[x] = (sum > 0.0f) ? 100.0f * pTmp[x] / sum : 0.0f;
107 cv::Size resSize = cMat.size();
110 resSize.height +=
ds;
113 Rect roi = Rect(Point(0, 0), cMat.size());
114 cMat.copyTo(tmp(roi));
119 for (byte y = 0; y < nStates; y++) {
120 const float *pCM = confusionMat.ptr<
float>(y);
122 for (byte x = 0; x < nStates; x++) sum += pCM[x];
123 float val = (sum > 0.0f) ? 100.0f * pCM[y] / sum : 0.0f;
128 for (byte x = 0; x < nStates; x++) {
130 for (byte y = 0; y < nStates; y++) sum += confusionMat.at<
float>(y, x);
131 float val = (sum > 0.0f) ? 100.0f * confusionMat.at<
float>(x, x) / sum : 0.0f;
137 for (byte s = 0; s < nStates; s++) sum += confusionMat.at<
float>(s, s);
143 resSize.height += 25;
146 roi = Rect(Point(25, 25), tmp.size());
147 tmp.copyTo(res(roi));
153 drawRectangle(res, Point(25 + 1 +
ds, 1), Point(25 + (nStates + 1) *
ds - 1, 25 - 1), frgColor,
"Predicted state", 0.45,
TP_BOTTOM);
154 drawRectangle(res, Point(25 + 1 + (nStates + 1) *
ds, 1), Point(25 + (nStates + 2) *
ds - 1, 25 - 1), frgColor);
158 tmp = Mat(25 - 1, nStates *
ds - 1, CV_8UC3); tmp.setTo(
bkgIntencity);
160 flip(tmp.t(), tmp, 0);
161 roi = Rect(Point(1, 25 + 1 +
ds), tmp.size());
162 tmp.copyTo(res(roi));
164 drawRectangle(res, Point(1, 25 + 1 + (nStates + 1) *
ds), Point(25 - 1, 25 + (nStates + 2) *
ds - 1), frgColor);
174 const byte nStates = potential.rows;
182 minMaxLoc(potential, NULL, &max);
186 for (byte s = 0; s < nStates; s++) {
187 float potVal = potential.at<
float>(s, 0);
189 double sat = (std::isnan(potVal)) ? 1.0 : potVal / max;
190 Scalar color_Tgt = (std::isnan(potVal) || bw) ? Scalar(0) :
m_vPalette.at(s % n).first;
192 double intensity = 0;
193 for (
int i = 0; i < 3; i++) {
194 color_Cur.val[i] = sat * color_Tgt.val[i] + (1.0 - sat) *
frgIntensity;
195 intensity += color_Cur.val[i] / 3;
200 color_Cur = (intensity < 128) ? CV_RGB(255, 255, 255) : CV_RGB(0, 0, 0);
202 if (
m_vPalette.at(s % n).second.empty()) sprintf(str,
"c%d", s);
203 else sprintf(str,
"%s",
m_vPalette.at(s % n).second.c_str());
204 textSize = getTextSize(str, cv::HersheyFonts::FONT_HERSHEY_SIMPLEX, 0.45, 1, NULL);
205 putText(res, str, Point(
ds * s + (MAX(
ds - textSize.width, 6)) / 2, 15), FONT_HERSHEY_SIMPLEX, 0.45, color_Cur, 1, cv::LineTypes::LINE_AA);
212 const byte nStates = potential.rows;
220 minMaxLoc(potential, NULL, &max);
222 Mat res(
ds * (nStates + 1) + 1,
ds * (nStates + 1) + 1, CV_8UC3); res.setTo(
bkgIntencity);
226 for (byte x = 1; x < nStates + 1; x++) {
227 if (
m_vPalette.at((x - 1) % n).second.empty()) sprintf(str,
"W%d", x - 1);
228 else sprintf(str,
"%s",
m_vPalette.at((x - 1) % n).second.c_str());
229 if (x == 1) sprintf(str,
"Bkgrd.");
233 for (byte y = 1; y < nStates + 1; y++) {
234 if (
m_vPalette.at((y - 1) % n).second.empty()) sprintf(str,
"W%d", y - 1);
235 else sprintf(str,
"%s",
m_vPalette.at((y - 1) % n).second.c_str());
236 if (y == 1) sprintf(str,
"Bkgrd.");
239 const float *pPot = potential.ptr<
float>(y - 1);
240 for (byte x = 1; x < nStates + 1; x++) {
241 float potVal = pPot[x - 1];
243 double sat = (std::isnan(potVal)) ? 1.0 : potVal / max;
244 Scalar color_Tgt = (std::isnan(potVal) || bw) ? Scalar(0) :
m_vPalette.at(MAX(0, y - 1) % n).first;
246 double intensity = 0;
247 for (
int i = 0; i < 3; i++) {
248 color_Cur.val[i] = sat * color_Tgt.val[i] + (1.0 - sat) *
frgIntensity;
249 intensity += color_Cur.val[i] / 3;
254 rectangle(res, Point(
ds,
ds), Point(
ds * (nStates + 1),
ds * (nStates + 1)), CV_RGB(0, 0, 0));
261 DGM_WARNING(
"This function is not implemented yet!");
268 Point pt1(1 + x *
ds, 1 + y *
ds);
269 Point pt2((x + 1) *
ds - 1, (y + 1) *
ds - 1);
280 if (std::isnan(val)) sprintf(str,
"N A N");
281 else if (val == 0) sprintf(str,
"O");
282 else if (val < 0.01f) sprintf(str,
"0.00");
283 else sprintf(str,
"%3.2f", val);
285 if (std::isnan(val)) sprintf(str,
"N A N");
286 else if (val == 0) sprintf(str,
"O");
287 else if ((val < 0.01f) || (val > 9999.99f)) sprintf(str,
"%1.1E", val);
288 else sprintf(str,
"%4.2f", val);
293 if (std::isnan(val)) {
294 line(img, pt1, pt2, CV_RGB(127, 127, 127), 1, cv::LineTypes::LINE_AA);
295 line(img, Point(pt2.x, pt1.y), Point(pt1.x, pt2.y), CV_RGB(127, 127, 127), 1, cv::LineTypes::LINE_AA);
301 rectangle(img, pt1, pt2, color, -1);
304 Size textSize = getTextSize(str, cv::HersheyFonts::FONT_HERSHEY_SIMPLEX, fontScale, 1, NULL);
305 Size rectSize = cv::Size(abs(pt2.x - pt1.x), abs(pt2.y - pt1.y));
307 Point org(MAX(rectSize.width- textSize.width, 6) / 2, (rectSize.height + textSize.height) / 2);
309 if (
textProp &
TP_RIGHT) org.x = MAX(rectSize.width - textSize.width - 3, 3);
312 org += Point(MIN(pt1.x, pt2.x), MIN(pt1.y, pt2.y));
314 double intensity = 0;
315 for (
int i = 0; i < 3; i++) intensity += color.val[i] / 3;
316 cv::Scalar fontColor = (intensity < 128) ? CV_RGB(255, 255, 255) : CV_RGB(0, 0, 0);
318 putText(img, str, org, FONT_HERSHEY_SIMPLEX, fontScale, fontColor, 1, cv::LineTypes::LINE_AA);
326 const int margin = 2;
327 const int nWords = dictionary.rows;
328 const int blockSize =
static_cast<int>(sqrt(dictionary.cols));
330 int width =
static_cast<int>(sqrt(nWords));
331 int height = nWords / width;
332 if (width * height < nWords) width++;
334 Mat res(height * (blockSize + margin) + margin, width * (blockSize + margin) + margin, CV_8UC1, cv::Scalar(0));
336 for (
int w = 0; w < nWords; w++) {
337 Mat word = dictionary.row(w);
338 word = 127.5 + m * 127.5 * word.reshape(0, blockSize);
343 int y0 = y * (blockSize + margin) + margin;
344 int x0 = x * (blockSize + margin) + margin;
346 word.convertTo(res(cv::Rect(x0, y0, blockSize, blockSize)), res.type());
Mat drawVoxel(const Mat &potential, byte flag) const
void drawSquare(Mat &img, byte x, byte y, const Scalar &color, T val, double fontScale=1.0, byte textProp=0) const
Default Pallete with 3 colors.
void drawRectangle(Mat &img, Point pt1, Point pt2, const Scalar &color, float val, double fontScale=1.0, byte textProp=TP_CENTER) const
Default Pallete with 12 colors.
Mark per-class accuracies in the confusion matrix.
The class with index 0 will not be visualized.
Default Pallete with 24 colors.
Default Pallete with 72 colors.
static const byte bkgIntencity
std::vector< std::pair< Scalar, std::string > > vec_nColor_t
Default Pallete with 72 colors.
Mat drawConfusionMatrix(const Mat &confusionMat, byte flag=0) const
Visualizes a confusion matrix.
Default Pallete with 36 colors.
Default Pallete with 24 colors.
Default Pallete with 36 colors.
Default Pallete with 12 colors.
CMarker(default_pallete palette=DEF_PALETTE_12)
Constructor with a default palette.
Mat drawDictionary(const Mat &dictionary, double m)
Visualizes a sparse coding dictionary.
static const byte frgIntensity
Default Pallete with 3 colors.
void markClasses(Mat &base, const Mat &classes, byte flag=0) const
Visualizes the classes.
default_pallete
Default palettes.
Mat drawVector(const Mat &potential, byte flag) const
cv::Scalar hsv2rgb(cv::Scalar hsv)
Transforms color from HSV to RGB space.
Default Pallete with 24 colors.
Mark in "black and white" palette.
Mat drawMatrix(const Mat &potential, byte flag) const
Default Pallete with 6 colors.
cv::Scalar hsv2bgr(cv::Scalar hsv)
Transforms color from HSV to BGR space.
Default Pallete with 6 colors.
vec_nColor_t m_vPalette
Pointer to the container with the palette.
Mat drawPotentials(const Mat &potential, byte flag=0) const
Visualizes the potentials.