24 #ifndef COM_DAFER45_TBTK_PLOT_CANVAS
25 #define COM_DAFER45_TBTK_PLOT_CANVAS
31 #include <opencv2/core/core.hpp>
32 #include <opencv2/highgui/highgui.hpp>
33 #include <opencv2/imgproc/imgproc.hpp>
47 void setWidth(
unsigned int width);
50 void setHeight(
unsigned int height);
61 void setBoundsX(
double minX,
double maxX);
64 void setBoundsY(
double minY,
double maxY);
67 void setBounds(
double minX,
double maxX,
double minY,
double maxY);
70 double getMinX()
const;
73 double getMaxX()
const;
76 double getMinY()
const;
79 double getMaxY()
const;
82 void setLabelX(
const std::string &labelX);
85 void setLabelY(
const std::string &labelY);
88 void setShowColorBox(
bool showColorBox);
91 bool getShowColorBox()
const;
94 void setBoundsColor(
double minColor,
double maxColor);
97 void setCanvas(cv::Mat &canvas);
100 const cv::Mat& getCanvas()
const;
120 const std::vector<unsigned char> &color,
129 const std::vector<unsigned char> &color
133 void save(std::string filename)
const;
139 double width, height;
142 unsigned int paddingLeft, paddingRight, paddingBottom, paddingTop;
145 double minX, maxX, minY, maxY;
148 std::string labelX, labelY;
154 double minColor, maxColor;
157 static constexpr
unsigned int COLOR_BOX_WINDOW_WIDTH = 100;
164 cv::Point getCVPoint(
double x,
double y)
const;
170 inline void PlotCanvas::setWidth(
unsigned int width){
174 inline void PlotCanvas::setHeight(
unsigned int height){
175 this->height = height;
178 inline void PlotCanvas::setPadding(
181 double paddingBottom,
184 this->paddingLeft = paddingLeft;
185 this->paddingRight = paddingRight;
186 this->paddingBottom = paddingBottom;
187 this->paddingTop = paddingTop;
190 inline void PlotCanvas::setBoundsX(
196 "Canvas::setBoundsX()",
197 "minX has to be smaller than maxX.",
202 this->minX = minX - 1e-10;
203 this->maxX = maxX + 1e-10;
211 inline void PlotCanvas::setBoundsY(
217 "Canvas::setBoundsY()",
218 "minY has to be smaller than maxY.",
223 this->minY = minY - 1e-10;
224 this->maxY = maxY + 1e-10;
232 inline void PlotCanvas::setBounds(
238 setBoundsX(minX, maxX);
239 setBoundsY(minY, maxY);
242 inline double PlotCanvas::getMinX()
const{
246 inline double PlotCanvas::getMaxX()
const{
250 inline double PlotCanvas::getMinY()
const{
254 inline double PlotCanvas::getMaxY()
const{
258 inline void PlotCanvas::setLabelX(
const std::string &labelX){
259 this->labelX = labelX;
262 inline void PlotCanvas::setLabelY(
const std::string &labelY){
263 this->labelY = labelY;
266 inline void PlotCanvas::setShowColorBox(
bool showColorBox){
267 this->showColorBox = showColorBox;
270 inline bool PlotCanvas::getShowColorBox()
const{
274 inline void PlotCanvas::setBoundsColor(
double minColor,
double maxColor){
275 this->minColor = minColor;
276 this->maxColor = maxColor;
279 inline void PlotCanvas::setCanvas(cv::Mat &canvas){
280 this->canvas = canvas;
283 inline const cv::Mat& PlotCanvas::getCanvas()
const{
287 inline cv::Point PlotCanvas::getCVPoint(
double x,
double y)
const{
288 double width = maxX - minX;
289 double height = maxY - minY;
291 paddingLeft + (1 - (paddingLeft + paddingRight + showColorBox*COLOR_BOX_WINDOW_WIDTH)/(
double)canvas.cols)*canvas.cols*(x - minX)/(
double)width,
292 canvas.rows - 1 - (paddingBottom + (1 - (paddingBottom + paddingTop)/(
double)canvas.rows)*canvas.rows*(y - minY)/(
double)height)
296 inline void PlotCanvas::clear(){
297 canvas = cv::Mat::zeros(height, width, CV_8UC3);
301 cv::Point(width-1, height-1),
302 cv::Scalar(255, 255, 255),
303 #
if CV_MAJOR_VERSION > 3
313 inline void PlotCanvas::setPixel(
320 canvas.at<cv::Vec3b>(y, x)[0] = blue;
321 canvas.at<cv::Vec3b>(y, x)[1] = green;
322 canvas.at<cv::Vec3b>(y, x)[2] = red;
325 inline void PlotCanvas::drawLine(
330 const std::vector<unsigned char> &color,
342 if(x0 < minX && x1 < minX)
344 if(x0 > maxX && x1 > maxX)
348 y0 += (minX - x0)*(y1 - y0)/(x1 - x0);
353 y1 -= (x1 - maxX)*(y1 - y0)/(x1 - x0);
356 if(y0 < minY && y1 < minY)
358 if(y0 > maxY && y1 > maxY)
362 x0 += (minY - y0)*(x1 - x0)/(y1 - y0);
367 x1 -= (y1 - maxY)*(x1 - x0)/(y1 - y0);
376 cv::Scalar(color[2], color[1], color[0]),
378 #
if CV_MAJOR_VERSION > 3
386 inline void PlotCanvas::drawCircle(
390 const std::vector<unsigned char> &color
407 cv::Scalar(color[2], color[1], color[0]),
409 #
if CV_MAJOR_VERSION > 3
417 inline void PlotCanvas::save(std::string filename)
const{
418 imwrite(filename, canvas);