Skip to content

Commit 97ea5bf

Browse files
sturkmen72vpisarev
authored andcommitted
Update plot module (#1367)
* Update plot.hpp * update * plot_demo * remove_tabs * Update plot_demo.cpp * Update plot.hpp * Update plot.hpp
1 parent eef53c2 commit 97ea5bf

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

modules/plot/include/opencv2/plot.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,19 @@ namespace cv
9999
* @brief Creates Plot2d object
100100
*
101101
* @param data \f$1xN\f$ or \f$Nx1\f$ matrix containing \f$Y\f$ values of points to plot. \f$X\f$ values
102+
* @param _invertOrientation true means the y axis is inverted
102103
* will be equal to indexes of correspondind elements in data matrix.
103104
*/
104-
CV_WRAP static Ptr<Plot2d> create(InputArray data);
105+
CV_WRAP static Ptr<Plot2d> create(InputArray data, bool _invertOrientation=false);
105106

106107
/**
107108
* @brief Creates Plot2d object
108109
*
109110
* @param dataX \f$1xN\f$ or \f$Nx1\f$ matrix \f$X\f$ values of points to plot.
110111
* @param dataY \f$1xN\f$ or \f$Nx1\f$ matrix containing \f$Y\f$ values of points to plot.
112+
* @param _invertOrientation true means the y axis is inverted
111113
*/
112-
CV_WRAP static Ptr<Plot2d> create(InputArray dataX, InputArray dataY);
114+
CV_WRAP static Ptr<Plot2d> create(InputArray dataX, InputArray dataY, bool _invertOrientation=false);
113115
};
114116
//! @}
115117
}

modules/plot/samples/plot_demo.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <opencv2/highgui.hpp>
2+
#include <opencv2/plot.hpp>
3+
#include <iostream>
4+
5+
using namespace cv;
6+
7+
int main()
8+
{
9+
Mat data_x(1, 50, CV_64F);
10+
Mat data_y(1, 50, CV_64F);
11+
12+
for (int i = 0; i < 50; i++)
13+
{
14+
data_x.at<double>(0, i) = (i - 25);
15+
data_y.at<double>(0, i) = (i - 25)*(i - 25)*(i - 25);
16+
}
17+
18+
std::cout << "data_x : " << data_x << std::endl;
19+
std::cout << "data_y : " << data_y << std::endl;
20+
21+
Mat plot_result;
22+
23+
Ptr<plot::Plot2d> plot = plot::Plot2d::create(data_x, data_y);
24+
plot->render(plot_result);
25+
26+
imshow("default orientation", plot_result);
27+
28+
plot = plot::Plot2d::create(data_x, data_y,true);
29+
plot->render(plot_result);
30+
31+
imshow("inverted orientation", plot_result);
32+
waitKey();
33+
34+
return 0;
35+
}

modules/plot/src/plot.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ namespace cv
5757
{
5858
public:
5959

60-
Plot2dImpl(InputArray plotData)
60+
Plot2dImpl(InputArray plotData, bool _invertOrientation)
6161
{
62+
invertOrientation = _invertOrientation;
6263
Mat _plotData = plotData.getMat();
6364
//if the matrix is not Nx1 or 1xN
6465
if(_plotData.cols > 1 && _plotData.rows > 1)
@@ -84,8 +85,9 @@ namespace cv
8485

8586
}
8687

87-
Plot2dImpl(InputArray plotDataX_, InputArray plotDataY_)
88+
Plot2dImpl(InputArray plotDataX_, InputArray plotDataY_, bool _invertOrientation)
8889
{
90+
invertOrientation = _invertOrientation;
8991
Mat _plotDataX = plotDataX_.getMat();
9092
Mat _plotDataY = plotDataY_.getMat();
9193
//f the matrix is not Nx1 or 1xN
@@ -199,11 +201,15 @@ namespace cv
199201
int NumVecElements = plotDataX.rows;
200202

201203
Mat InterpXdata = linearInterpolation(plotMinX, plotMaxX, 0, plotSizeWidth, plotDataX);
202-
Mat InterpYdata = linearInterpolation(plotMinY, plotMaxY, 0, plotSizeHeight, plotDataY);
204+
Mat InterpYdata = invertOrientation ?
205+
linearInterpolation(plotMaxY, plotMinY, 0, plotSizeHeight, plotDataY) :
206+
linearInterpolation(plotMinY, plotMaxY, 0, plotSizeHeight, plotDataY);
203207

204208
//Find the zeros in image coordinates
205209
Mat InterpXdataFindZero = linearInterpolation(plotMinX_plusZero, plotMaxX_plusZero, 0, plotSizeWidth, plotDataX_plusZero);
206-
Mat InterpYdataFindZero = linearInterpolation(plotMinY_plusZero, plotMaxY_plusZero, 0, plotSizeHeight, plotDataY_plusZero);
210+
Mat InterpYdataFindZero = invertOrientation ?
211+
linearInterpolation(plotMaxY_plusZero, plotMinY_plusZero, 0, plotSizeHeight, plotDataY_plusZero) :
212+
linearInterpolation(plotMinY_plusZero, plotMaxY_plusZero, 0, plotSizeHeight, plotDataY_plusZero);
207213

208214
int ImageXzero = (int)InterpXdataFindZero.at<double>(NumVecElements,0);
209215
int ImageYzero = (int)InterpYdataFindZero.at<double>(NumVecElements,0);
@@ -264,6 +270,7 @@ namespace cv
264270
double plotMinY_plusZero;
265271
double plotMaxY_plusZero;
266272
int plotLineWidth;
273+
bool invertOrientation;
267274
bool needShowGrid;
268275
bool needShowText;
269276
int gridLinesNumber;
@@ -453,15 +460,15 @@ namespace cv
453460

454461
};
455462

456-
Ptr<Plot2d> Plot2d::create(InputArray _plotData)
463+
Ptr<Plot2d> Plot2d::create(InputArray _plotData, bool _invertOrientation)
457464
{
458-
return Ptr<Plot2dImpl> (new Plot2dImpl (_plotData));
465+
return Ptr<Plot2dImpl> (new Plot2dImpl (_plotData, _invertOrientation));
459466

460467
}
461468

462-
Ptr<Plot2d> Plot2d::create(InputArray _plotDataX, InputArray _plotDataY)
469+
Ptr<Plot2d> Plot2d::create(InputArray _plotDataX, InputArray _plotDataY, bool _invertOrientation)
463470
{
464-
return Ptr<Plot2dImpl> (new Plot2dImpl (_plotDataX, _plotDataY));
471+
return Ptr<Plot2dImpl> (new Plot2dImpl (_plotDataX, _plotDataY, _invertOrientation));
465472
}
466473
}
467474
}

0 commit comments

Comments
 (0)