1 #include "MarkerHistogram.h" 2 #include "DGM/TrainNodeNaiveBayes.h" 20 Mat res(2 *
margin.height + 256, 2 *
margin.height + 256, CV_8UC3);
22 rectangle(res, Point(
margin.height,
margin.height), Point(256 +
margin.height, 256 +
margin.height), CV_RGB(0, 0, 0), -1);
26 concurrency::parallel_for(0, 256, [&](
int y) {
28 for (
int y = 0; y < 256; y++) {
30 Mat fv(2, 1, CV_8UC1);
31 fv.at<byte>(1, 0) = static_cast<byte>(y);
32 Vec3b *pRes = res.ptr<Vec3b>(
margin.height + 255 - y);
33 for (
int x = 0; x < 256; x++) {
34 fv.at<byte>(0, 0) = static_cast<byte>(x);
37 for (
int s = 0; s < pot.rows; s++) {
38 float val = MIN(100, pot.at<
float>(s, 0));
39 Scalar color = val *
m_vPalette[s % n].first / 100;
40 pRes[
margin.height + x] += Vec3b((byte)color[0], (byte)color[1], (byte)color[2]);
48 else DGM_WARNING(
"The number of features (%d) is not 2", nFeatures);
51 Mat tmp(
margin.height, res.rows, CV_8UC3);
53 sprintf(str,
" feature 1 255");
54 putText(tmp, str, Point(3, tmp.rows - 4), FONT_HERSHEY_SIMPLEX, 0.3, CV_RGB(255, 255, 255), 1, cv::LineTypes::LINE_AA);
55 flip(tmp.t(), tmp, 0);
56 tmp.copyTo(res(Rect(0, 0,
margin.height, tmp.rows)));
57 sprintf(str,
"0 feature 0 255");
58 putText(res, str, Point(3, res.rows - 6), FONT_HERSHEY_SIMPLEX, 0.3, CV_RGB(255, 255, 255), 1, cv::LineTypes::LINE_AA);
61 rectangle(res, Point(
margin.height - 1,
margin.height - 1), Point(256 +
margin.height, 256 +
margin.height), CV_RGB(255, 255, 255));
69 namedWindow(
wndName.c_str(), WINDOW_AUTOSIZE);
70 imshow(
wndName.c_str(), histogramImg);
71 setMouseCallback(
wndName.c_str(), [](
int Event,
int x,
int y,
int flags,
void *param) {
72 if (Event != cv::MouseEventFlags::EVENT_FLAG_LBUTTON)
return;
74 Vec3b color = histogramImg.at<Vec3b>(y, x);
75 histogramImg.release();
76 histogramImg = pUserData->
drawHistogram(CV_RGB(color[2], color[1], color[0]));
77 imshow(
wndName.c_str(), histogramImg);
90 const byte fMaxHeight = 9;
95 fSize.width = nFeatures / fMaxHeight;
96 if (nFeatures % fMaxHeight != 0) fSize.width++;
97 fSize.height = (nFeatures < fMaxHeight) ? nFeatures : fMaxHeight;
100 resSize.width =
margin.width + fSize.width * (256 + 2 *
margin.width);
101 resSize.height =
margin.height + fSize.height * (100 +
margin.height);
105 resSize.width += legende.cols;
107 Mat res(resSize, CV_8UC3);
110 for (word f = 0; f < nFeatures; f++) {
111 int dx = f / fMaxHeight; dx *= (256 + 2 *
margin.width);
112 int dy = f % fMaxHeight; dy *= (100 +
margin.height);
115 Rect roi(Point(dx,
margin.height + dy), featureHistogram.size());
116 featureHistogram.copyTo(res(roi));
117 featureHistogram.release();
121 Rect roi(Point(res.cols - legende.cols,
margin.height), legende.size());
122 legende.copyTo(res(roi));
137 for (byte s = 0; s < nStates; s++) {
139 if (norm(diff) < 1.0)
return s;
149 const int koeff = 1200;
153 Mat tmp(res.size(), res.type()); tmp.setTo(0);
156 for (x = 0; x <= 255; x += 51) {
157 y = ((x > 75) && (x < 180)) ? 25 : 0;
158 line(tmp, Point(
margin.width + x, y), Point(
margin.width + x, 100), CV_RGB(50, 50, 50));
163 for (byte s = 0; s < nStates; s++) {
166 for (x = 0; x < 256; x++) {
167 int len =
static_cast<int>(koeff * pPDF->
getDensity(x));
168 y = 100 - MIN(99, len);
170 if ((activeState == -1) || (activeState == s % n))
173 addWeighted(res, 1.0, tmp,
frgWeight, 0.0, res);
181 cv::Size textSize = getTextSize(str, cv::HersheyFonts::FONT_HERSHEY_SIMPLEX, 0.5, 1, NULL);
182 putText(res, str, Point(
margin.width + (MAX(256 - textSize.width, 108)) / 2, 16), FONT_HERSHEY_SIMPLEX, 0.5, CV_RGB(125, 125, 125), 1, cv::LineTypes::LINE_AA);
185 rectangle(res, Point(
margin.width - 1, 0), Point(
margin.width + 256, 100), CV_RGB(255,255,255));
186 for (x = 0; x <= 255; x += 51) {
187 sprintf(str,
"%d", x);
188 putText(res, str, Point(
margin.width + x - 5, 109), FONT_HERSHEY_SIMPLEX, 0.3, CV_RGB(255, 255, 255), 1, cv::LineTypes::LINE_AA);
199 const int koeff = 1000000;
204 Mat tmp(res.size(), res.type()); tmp.setTo(0);
208 for (byte s = 0; s < nStates; s++) {
211 for (
int y = 0; y < 256; y++) {
212 Vec3b *pTmp = tmp.ptr<Vec3b>(
margin.height + 256 - y);
213 for (
int x = 0; x < 256; x++) {
214 double val = MIN(255, koeff * pPDF2D->
getDensity(Scalar(x, y)));
215 Scalar color = val *
m_vPalette[s % n].first / 255;
217 if ((activeState == -1) || (activeState == s % n))
218 pTmp[
margin.height + x] = Vec3b((byte)color[0], (byte)color[1], (byte)color[2]);
221 addWeighted(res, 1.0, tmp,
frgWeight, 0.0, res);
226 else DGM_WARNING(
"The node trainer (%s) is not Bayes or the number of features (%d) is not 2",
typeid(
m_nodeTrainer).name(), nFeatures);
229 tmp = Mat(
margin.height, res.rows, CV_8UC3);
231 sprintf(str,
" feature 1 255");
232 putText(tmp, str, Point(3, tmp.rows - 4), FONT_HERSHEY_SIMPLEX, 0.3, CV_RGB(255, 255, 255), 1, cv::LineTypes::LINE_AA);
233 flip(tmp.t(), tmp, 0);
234 tmp.copyTo(res(Rect(0, 0,
margin.height, tmp.rows)));
235 sprintf(str,
"0 feature 0 255");
236 putText(res, str, Point(3, res.rows - 6), FONT_HERSHEY_SIMPLEX, 0.3, CV_RGB(255, 255, 255), 1, cv::LineTypes::LINE_AA);
239 rectangle(res, Point(
margin.height - 1,
margin.height - 1), Point(256 +
margin.height, 256 +
margin.height), CV_RGB(255, 255, 255));
247 const byte sMaxHeight = maxHeight / (2 *
margin.height);
252 sSize.width = (nStates + 1) / sMaxHeight;
253 if ((nStates + 1) % sMaxHeight != 0) sSize.width++;
254 sSize.height = ((nStates + 1) < sMaxHeight) ? (nStates + 1) : sMaxHeight;
257 resSize.width = sSize.width * 120;
258 resSize.height = 2 *
margin.height * sSize.height;
261 Mat tmp(res.size(), res.type()); tmp.setTo(0);
263 for (byte s = 0; s < nStates; s++) {
264 int dx = s / sMaxHeight; dx *= 120;
265 int dy = s % sMaxHeight; dy *= 2 *
margin.height;
268 rectangle(tmp, Point(dx, dy), Point(dx +
margin.width, dy +
margin.height),
m_vPalette.at(s % n).first, -1);
271 if (activeState == s) {
272 const Point triangle[3] = {Point(dx, dy), Point(dx +
margin.height / 2, dy +
margin.height / 2), Point(dx, dy +
margin.height)};
273 fillConvexPoly(tmp, triangle, 3, CV_RGB(0, 0, 0));
277 if (
m_vPalette.at(s % n).second.empty()) sprintf(str,
"c%d", s);
278 else sprintf(str,
"%s",
m_vPalette.at(s % n).second.c_str());
279 putText(tmp, str, Point(dx +
margin.width + 5, dy +
margin.height - 3), FONT_HERSHEY_SIMPLEX, 0.5, CV_RGB(255, 255, 255), 1, cv::LineTypes::LINE_AA);
283 int dx = nStates / sMaxHeight; dx *= 120;
284 int dy = nStates % sMaxHeight; dy *= 2 *
margin.height;
285 rectangle(tmp, Point(dx, dy), Point(dx +
margin.width, dy +
margin.height), CV_RGB(255, 255, 255), -1);
286 if (activeState == -1) {
287 const Point triangle[3] = {Point(dx, dy), Point(dx +
margin.height / 2, dy +
margin.height / 2), Point(dx, dy +
margin.height)};
288 fillConvexPoly(tmp, triangle, 3, CV_RGB(0, 0, 0));
290 putText(tmp,
"all", Point(dx +
margin.width + 5, dy +
margin.height - 3), FONT_HERSHEY_SIMPLEX, 0.5, CV_RGB(255, 255, 255), 1, cv::LineTypes::LINE_AA);
292 addWeighted(res, 1.0, tmp,
frgWeight, 0.0, res);
void close(void) const
Closes the histogram window.
vec_string_t m_vFeatureNames
static const double frgWeight
virtual double getDensity(Scalar point)=0
Returns the probability density value for the argument point.
word getNumFeatures(void) const
Returns number of features.
Mat drawHistogram(void) const
Draws a figure with the visualization of feature densitiy distributions.
Mat drawFeatureHistogram(word f, int activeState=-1) const
Draws a single feature histogram.
Mat drawHistogram2D(void) const
Draws a figure with the visualization of 2-dimensional node potentials histogram. ...
static const std::string wndName
Mat getNodePotentials(const Mat &featureVectors, const Mat &weights=Mat(), float Z=0.0f) const
Returns a block of node potentials, based on the block of feature vector.
const CTrainNode & m_nodeTrainer
The node trainer.
void showHistogram(void)
Visualizes the feature densitiy distributions in a separate window with user interaction.
Mat drawClassificationMap2D(float Z) const
Draws a figure with the visualization of 2-dimensional classification map.
Interface class for Probability Density Function (PDF)
Mat drawLegend(int maxHeight, int activeState=-1) const
Draws a legend to the main figure.
Mat drawFeatureHistogram2D(word f, int activeState=-1) const
Draws a 2-dimensional feature histogram.
byte getNumStates(void) const
Returns number of states (classes)
int getActiveState(Scalar color) const
Retrieves a chosen by an user state, from the color.
static const byte bkgIntencity
vec_nColor_t m_vPalette
Pointer to the container with the palette.