Skip to content

Commit 55c896c

Browse files
committed
Merge pull request #1307 from sovrasov:plot_improvement
2 parents e7a4372 + 5c2dfcd commit 55c896c

File tree

3 files changed

+71
-28
lines changed

3 files changed

+71
-28
lines changed

modules/plot/include/opencv2/plot.hpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,33 @@ namespace cv
8484
CV_WRAP virtual void setPlotGridColor(Scalar _plotGridColor) = 0;
8585
CV_WRAP virtual void setPlotTextColor(Scalar _plotTextColor) = 0;
8686
CV_WRAP virtual void setPlotSize(int _plotSizeWidth, int _plotSizeHeight) = 0;
87+
CV_WRAP virtual void setShowGrid(bool needShowGrid) = 0;
88+
CV_WRAP virtual void setShowText(bool needShowText) = 0;
89+
CV_WRAP virtual void setGridLinesNumber(int gridLinesNumber) = 0;
90+
/**
91+
* @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).
92+
*
93+
* @param pointIdx index of the required point in data array.
94+
*/
95+
CV_WRAP virtual void setPointIdxToPrint(int pointIdx) = 0;
8796
CV_WRAP virtual void render(OutputArray _plotResult) = 0;
88-
};
8997

90-
/**
91-
* @brief Creates Plot2d object
92-
*
93-
* @param data \f$1xN\f$ or \f$Nx1\f$ matrix containing \f$Y\f$ values of points to plot. \f$X\f$ values
94-
* will be equal to indexes of correspondind elements in data matrix.
95-
*/
96-
CV_EXPORTS_W Ptr<Plot2d> createPlot2d(InputArray data);
97-
/**
98-
* @brief Creates Plot2d object
99-
*
100-
* @param dataX \f$1xN\f$ or \f$Nx1\f$ matrix \f$X\f$ values of points to plot.
101-
* @param dataY \f$1xN\f$ or \f$Nx1\f$ matrix containing \f$Y\f$ values of points to plot.
102-
*/
103-
CV_EXPORTS_W Ptr<Plot2d> createPlot2d(InputArray dataX, InputArray dataY);
98+
/**
99+
* @brief Creates Plot2d object
100+
*
101+
* @param data \f$1xN\f$ or \f$Nx1\f$ matrix containing \f$Y\f$ values of points to plot. \f$X\f$ values
102+
* will be equal to indexes of correspondind elements in data matrix.
103+
*/
104+
CV_WRAP static Ptr<Plot2d> create(InputArray data);
105+
106+
/**
107+
* @brief Creates Plot2d object
108+
*
109+
* @param dataX \f$1xN\f$ or \f$Nx1\f$ matrix \f$X\f$ values of points to plot.
110+
* @param dataY \f$1xN\f$ or \f$Nx1\f$ matrix containing \f$Y\f$ values of points to plot.
111+
*/
112+
CV_WRAP static Ptr<Plot2d> create(InputArray dataX, InputArray dataY);
113+
};
104114
//! @}
105115
}
106116
}

modules/plot/src/plot.cpp

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,26 @@ namespace cv
168168
else
169169
plotSizeHeight = 300;
170170
}
171-
171+
void setShowGrid(bool _needShowGrid)
172+
{
173+
needShowGrid = _needShowGrid;
174+
}
175+
void setShowText(bool _needShowText)
176+
{
177+
needShowText = _needShowText;
178+
}
179+
void setGridLinesNumber(int _gridLinesNumber)
180+
{
181+
if(_gridLinesNumber <= 0)
182+
_gridLinesNumber = 1;
183+
gridLinesNumber = _gridLinesNumber;
184+
}
185+
void setPointIdxToPrint(int _cursorPos)
186+
{
187+
if(_cursorPos >= plotDataX.rows || _cursorPos < 0)
188+
_cursorPos = plotDataX.rows - 1;
189+
cursorPos = _cursorPos;
190+
}
172191
//render the plotResult to a Mat
173192
void render(OutputArray _plotResult)
174193
{
@@ -189,8 +208,8 @@ namespace cv
189208
int ImageXzero = (int)InterpXdataFindZero.at<double>(NumVecElements,0);
190209
int ImageYzero = (int)InterpYdataFindZero.at<double>(NumVecElements,0);
191210

192-
double CurrentX = plotDataX.at<double>(NumVecElements-1,0);
193-
double CurrentY = plotDataY.at<double>(NumVecElements-1,0);
211+
double CurrentX = plotDataX.at<double>(cursorPos,0);
212+
double CurrentY = plotDataY.at<double>(cursorPos,0);
194213

195214
drawAxis(ImageXzero,ImageYzero, CurrentX, CurrentY, plotAxisColor, plotGridColor);
196215

@@ -245,6 +264,10 @@ namespace cv
245264
double plotMinY_plusZero;
246265
double plotMaxY_plusZero;
247266
int plotLineWidth;
267+
bool needShowGrid;
268+
bool needShowText;
269+
int gridLinesNumber;
270+
int cursorPos;
248271

249272
//colors of each plot element
250273
Scalar plotLineColor;
@@ -319,22 +342,30 @@ namespace cv
319342
setPlotBackgroundColor(Scalar(0, 0, 0));
320343
setPlotLineColor(Scalar(0, 255, 255));
321344
setPlotTextColor(Scalar(255, 255, 255));
345+
setShowGrid(true);
346+
setShowText(true);
347+
setGridLinesNumber(10);
348+
setPointIdxToPrint(-1);
322349
}
323350

324351
void drawAxis(int ImageXzero, int ImageYzero, double CurrentX, double CurrentY, Scalar axisColor, Scalar gridColor)
325352
{
326-
drawValuesAsText(0, ImageXzero, ImageYzero, 10, 20);
327-
drawValuesAsText(0, ImageXzero, ImageYzero, -20, 20);
328-
drawValuesAsText(0, ImageXzero, ImageYzero, 10, -10);
329-
drawValuesAsText(0, ImageXzero, ImageYzero, -20, -10);
330-
drawValuesAsText("X = %g",CurrentX, 0, 0, 40, 20);
331-
drawValuesAsText("Y = %g",CurrentY, 0, 20, 40, 20);
353+
if(needShowText)
354+
{
355+
drawValuesAsText(0, ImageXzero, ImageYzero, 10, 20);
356+
drawValuesAsText(0, ImageXzero, ImageYzero, -20, 20);
357+
drawValuesAsText(0, ImageXzero, ImageYzero, 10, -10);
358+
drawValuesAsText(0, ImageXzero, ImageYzero, -20, -10);
359+
drawValuesAsText((format("X_%d = ", cursorPos) + "%g").c_str(), CurrentX, 0, 0, 40, 20);
360+
drawValuesAsText((format("Y_%d = ", cursorPos) + "%g").c_str(), CurrentY, 0, 20, 40, 20);
361+
}
332362

333363
//Horizontal X axis and equispaced horizontal lines
334-
int LineSpace = 50;
364+
int LineSpace = cvRound(plotSizeHeight / (float)gridLinesNumber);
335365
int TraceSize = 5;
336366
drawLine(0, plotSizeWidth, ImageYzero, ImageYzero, axisColor);
337367

368+
if(needShowGrid)
338369
for(int i=-plotSizeHeight; i<plotSizeHeight; i=i+LineSpace){
339370

340371
if(i!=0){
@@ -349,7 +380,9 @@ namespace cv
349380

350381
//Vertical Y axis
351382
drawLine(ImageXzero, ImageXzero, 0, plotSizeHeight, axisColor);
383+
LineSpace = cvRound(LineSpace * (float)plotSizeWidth / plotSizeHeight );
352384

385+
if(needShowGrid)
353386
for(int i=-plotSizeWidth; i<plotSizeWidth; i=i+LineSpace){
354387

355388
if(i!=0){
@@ -420,13 +453,13 @@ namespace cv
420453

421454
};
422455

423-
Ptr<Plot2d> createPlot2d(InputArray _plotData)
456+
Ptr<Plot2d> Plot2d::create(InputArray _plotData)
424457
{
425458
return Ptr<Plot2dImpl> (new Plot2dImpl (_plotData));
426459

427460
}
428461

429-
Ptr<Plot2d> createPlot2d(InputArray _plotDataX, InputArray _plotDataY)
462+
Ptr<Plot2d> Plot2d::create(InputArray _plotDataX, InputArray _plotDataY)
430463
{
431464
return Ptr<Plot2dImpl> (new Plot2dImpl (_plotDataX, _plotDataY));
432465
}

modules/tracking/samples/benchmark.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ struct AlgoWrap
165165

166166
void plotLTRC(Mat &img) const
167167
{
168-
Ptr<plot::Plot2d> p_ = plot::createPlot2d(getLTRC());
168+
Ptr<plot::Plot2d> p_ = plot::Plot2d::create(getLTRC());
169169
p_->render(img);
170170
}
171171

0 commit comments

Comments
 (0)