From 5c2dfcd44856f94d0f27a581aac34c0090131d36 Mon Sep 17 00:00:00 2001 From: Vladislav Sovrasov Date: Tue, 1 Aug 2017 15:23:29 +0300 Subject: [PATCH] plot: make the module more customizable --- modules/plot/include/opencv2/plot.hpp | 40 +++++++++++------- modules/plot/src/plot.cpp | 57 ++++++++++++++++++++------ modules/tracking/samples/benchmark.cpp | 2 +- 3 files changed, 71 insertions(+), 28 deletions(-) diff --git a/modules/plot/include/opencv2/plot.hpp b/modules/plot/include/opencv2/plot.hpp index 6673bd135..2b335ae6d 100644 --- a/modules/plot/include/opencv2/plot.hpp +++ b/modules/plot/include/opencv2/plot.hpp @@ -84,23 +84,33 @@ namespace cv CV_WRAP virtual void setPlotGridColor(Scalar _plotGridColor) = 0; CV_WRAP virtual void setPlotTextColor(Scalar _plotTextColor) = 0; CV_WRAP virtual void setPlotSize(int _plotSizeWidth, int _plotSizeHeight) = 0; + CV_WRAP virtual void setShowGrid(bool needShowGrid) = 0; + CV_WRAP virtual void setShowText(bool needShowText) = 0; + CV_WRAP virtual void setGridLinesNumber(int gridLinesNumber) = 0; + /** + * @brief Sets the index of a point which coordinates will be printed on the top left corner of the plot (if ShowText flag is true). + * + * @param pointIdx index of the required point in data array. + */ + CV_WRAP virtual void setPointIdxToPrint(int pointIdx) = 0; CV_WRAP virtual void render(OutputArray _plotResult) = 0; - }; - /** - * @brief Creates Plot2d object - * - * @param data \f$1xN\f$ or \f$Nx1\f$ matrix containing \f$Y\f$ values of points to plot. \f$X\f$ values - * will be equal to indexes of correspondind elements in data matrix. - */ - CV_EXPORTS_W Ptr createPlot2d(InputArray data); - /** - * @brief Creates Plot2d object - * - * @param dataX \f$1xN\f$ or \f$Nx1\f$ matrix \f$X\f$ values of points to plot. - * @param dataY \f$1xN\f$ or \f$Nx1\f$ matrix containing \f$Y\f$ values of points to plot. - */ - CV_EXPORTS_W Ptr createPlot2d(InputArray dataX, InputArray dataY); + /** + * @brief Creates Plot2d object + * + * @param data \f$1xN\f$ or \f$Nx1\f$ matrix containing \f$Y\f$ values of points to plot. \f$X\f$ values + * will be equal to indexes of correspondind elements in data matrix. + */ + CV_WRAP static Ptr create(InputArray data); + + /** + * @brief Creates Plot2d object + * + * @param dataX \f$1xN\f$ or \f$Nx1\f$ matrix \f$X\f$ values of points to plot. + * @param dataY \f$1xN\f$ or \f$Nx1\f$ matrix containing \f$Y\f$ values of points to plot. + */ + CV_WRAP static Ptr create(InputArray dataX, InputArray dataY); + }; //! @} } } diff --git a/modules/plot/src/plot.cpp b/modules/plot/src/plot.cpp index dcfc8c5dd..ff97fb54e 100644 --- a/modules/plot/src/plot.cpp +++ b/modules/plot/src/plot.cpp @@ -168,7 +168,26 @@ namespace cv else plotSizeHeight = 300; } - + void setShowGrid(bool _needShowGrid) + { + needShowGrid = _needShowGrid; + } + void setShowText(bool _needShowText) + { + needShowText = _needShowText; + } + void setGridLinesNumber(int _gridLinesNumber) + { + if(_gridLinesNumber <= 0) + _gridLinesNumber = 1; + gridLinesNumber = _gridLinesNumber; + } + void setPointIdxToPrint(int _cursorPos) + { + if(_cursorPos >= plotDataX.rows || _cursorPos < 0) + _cursorPos = plotDataX.rows - 1; + cursorPos = _cursorPos; + } //render the plotResult to a Mat void render(OutputArray _plotResult) { @@ -189,8 +208,8 @@ namespace cv int ImageXzero = (int)InterpXdataFindZero.at(NumVecElements,0); int ImageYzero = (int)InterpYdataFindZero.at(NumVecElements,0); - double CurrentX = plotDataX.at(NumVecElements-1,0); - double CurrentY = plotDataY.at(NumVecElements-1,0); + double CurrentX = plotDataX.at(cursorPos,0); + double CurrentY = plotDataY.at(cursorPos,0); drawAxis(ImageXzero,ImageYzero, CurrentX, CurrentY, plotAxisColor, plotGridColor); @@ -245,6 +264,10 @@ namespace cv double plotMinY_plusZero; double plotMaxY_plusZero; int plotLineWidth; + bool needShowGrid; + bool needShowText; + int gridLinesNumber; + int cursorPos; //colors of each plot element Scalar plotLineColor; @@ -319,22 +342,30 @@ namespace cv setPlotBackgroundColor(Scalar(0, 0, 0)); setPlotLineColor(Scalar(0, 255, 255)); setPlotTextColor(Scalar(255, 255, 255)); + setShowGrid(true); + setShowText(true); + setGridLinesNumber(10); + setPointIdxToPrint(-1); } void drawAxis(int ImageXzero, int ImageYzero, double CurrentX, double CurrentY, Scalar axisColor, Scalar gridColor) { - drawValuesAsText(0, ImageXzero, ImageYzero, 10, 20); - drawValuesAsText(0, ImageXzero, ImageYzero, -20, 20); - drawValuesAsText(0, ImageXzero, ImageYzero, 10, -10); - drawValuesAsText(0, ImageXzero, ImageYzero, -20, -10); - drawValuesAsText("X = %g",CurrentX, 0, 0, 40, 20); - drawValuesAsText("Y = %g",CurrentY, 0, 20, 40, 20); + if(needShowText) + { + drawValuesAsText(0, ImageXzero, ImageYzero, 10, 20); + drawValuesAsText(0, ImageXzero, ImageYzero, -20, 20); + drawValuesAsText(0, ImageXzero, ImageYzero, 10, -10); + drawValuesAsText(0, ImageXzero, ImageYzero, -20, -10); + drawValuesAsText((format("X_%d = ", cursorPos) + "%g").c_str(), CurrentX, 0, 0, 40, 20); + drawValuesAsText((format("Y_%d = ", cursorPos) + "%g").c_str(), CurrentY, 0, 20, 40, 20); + } //Horizontal X axis and equispaced horizontal lines - int LineSpace = 50; + int LineSpace = cvRound(plotSizeHeight / (float)gridLinesNumber); int TraceSize = 5; drawLine(0, plotSizeWidth, ImageYzero, ImageYzero, axisColor); + if(needShowGrid) for(int i=-plotSizeHeight; i createPlot2d(InputArray _plotData) + Ptr Plot2d::create(InputArray _plotData) { return Ptr (new Plot2dImpl (_plotData)); } - Ptr createPlot2d(InputArray _plotDataX, InputArray _plotDataY) + Ptr Plot2d::create(InputArray _plotDataX, InputArray _plotDataY) { return Ptr (new Plot2dImpl (_plotDataX, _plotDataY)); } diff --git a/modules/tracking/samples/benchmark.cpp b/modules/tracking/samples/benchmark.cpp index 5616829e4..7becc6a03 100644 --- a/modules/tracking/samples/benchmark.cpp +++ b/modules/tracking/samples/benchmark.cpp @@ -165,7 +165,7 @@ struct AlgoWrap void plotLTRC(Mat &img) const { - Ptr p_ = plot::createPlot2d(getLTRC()); + Ptr p_ = plot::Plot2d::create(getLTRC()); p_->render(img); }