plot.cpp 18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
//################################################################################
//
//                    Created by Nuno Moutinho
//
//################################################################################

#include "precomp.hpp"

50 51 52 53
namespace cv
{
    namespace plot
    {
54 55
        using namespace std;

56
        class Plot2dImpl CV_FINAL : public Plot2d
57 58 59
        {
            public:

Suleyman TURKMEN's avatar
Suleyman TURKMEN committed
60
            Plot2dImpl(InputArray plotData)
61
            {
62
                Mat _plotData = plotData.getMat();
63 64
                //if the matrix is not Nx1 or 1xN
                if(_plotData.cols > 1 && _plotData.rows > 1)
65
                    CV_Error(Error::StsBadArg, "ERROR: Plot data must be a 1xN or Nx1 matrix.\n");
66

67
                CV_Assert(_plotData.type() == CV_64F);
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

                //in case we have a row matrix than needs to be transposed
                if(_plotData.cols > _plotData.rows)
                {
                    _plotData = _plotData.t();
                }

                plotDataY=_plotData;
                plotDataX = plotDataY*0;
                for (int i=0; i<plotDataY.rows; i++)
                {
                    plotDataX.at<double>(i,0) = i;
                }

                //calling the main constructor
                plotHelper(plotDataX, plotDataY);
84

85
            }
86

Suleyman TURKMEN's avatar
Suleyman TURKMEN committed
87
            Plot2dImpl(InputArray plotDataX_, InputArray plotDataY_)
88
            {
89 90
                Mat _plotDataX = plotDataX_.getMat();
                Mat _plotDataY = plotDataY_.getMat();
91 92
                //f the matrix is not Nx1 or 1xN
                if((_plotDataX.cols > 1 && _plotDataX.rows > 1) || (_plotDataY.cols > 1 && _plotDataY.rows > 1))
93
                    CV_Error(Error::StsBadArg, "ERROR: Plot data must be a 1xN or Nx1 matrix.\n");
94

95
                CV_Assert(_plotDataX.type() == CV_64F && _plotDataY.type() == CV_64F);
96 97 98 99 100 101 102 103 104 105 106 107 108

                //in case we have a row matrix than needs to be transposed
                if(_plotDataX.cols > _plotDataX.rows)
                {
                    _plotDataX = _plotDataX.t();
                }
                if(_plotDataY.cols > _plotDataY.rows)
                {
                    _plotDataY = _plotDataY.t();
                }

                plotHelper(_plotDataX, _plotDataY);
            }
109

110
            //set functions
111
            void setMinX(double _plotMinX) CV_OVERRIDE
112 113 114 115
            {
                plotMinX = _plotMinX;
                plotMinX_plusZero = _plotMinX;
            }
116
            void setMaxX(double _plotMaxX) CV_OVERRIDE
117 118 119 120
            {
                plotMaxX = _plotMaxX;
                plotMaxX_plusZero = _plotMaxX;
            }
121
            void setMinY(double _plotMinY) CV_OVERRIDE
122 123 124 125
            {
                plotMinY = _plotMinY;
                plotMinY_plusZero = _plotMinY;
            }
126
            void setMaxY(double _plotMaxY) CV_OVERRIDE
127 128 129 130
            {
                plotMaxY = _plotMaxY;
                plotMaxY_plusZero = _plotMaxY;
            }
131
            void setPlotLineWidth(int _plotLineWidth) CV_OVERRIDE
132
            {
133 134
                plotLineWidth = _plotLineWidth;
            }
135
            void setInvertOrientation(bool _invertOrientation) CV_OVERRIDE
Suleyman TURKMEN's avatar
Suleyman TURKMEN committed
136 137 138
            {
                invertOrientation = _invertOrientation;
            }
139
            void setNeedPlotLine(bool _needPlotLine) CV_OVERRIDE
140 141
            {
                needPlotLine = _needPlotLine;
142
            }
143
            void setPlotLineColor(Scalar _plotLineColor) CV_OVERRIDE
144 145 146
            {
                plotLineColor=_plotLineColor;
            }
147
            void setPlotBackgroundColor(Scalar _plotBackgroundColor) CV_OVERRIDE
148 149 150
            {
                plotBackgroundColor=_plotBackgroundColor;
            }
151
            void setPlotAxisColor(Scalar _plotAxisColor) CV_OVERRIDE
152 153 154
            {
                plotAxisColor=_plotAxisColor;
            }
155
            void setPlotGridColor(Scalar _plotGridColor) CV_OVERRIDE
156 157 158
            {
                plotGridColor=_plotGridColor;
            }
159
            void setPlotTextColor(Scalar _plotTextColor) CV_OVERRIDE
160 161 162
            {
                plotTextColor=_plotTextColor;
            }
163
            void setPlotSize(int _plotSizeWidth, int _plotSizeHeight) CV_OVERRIDE
164 165 166 167 168 169 170 171 172 173 174
            {
                if(_plotSizeWidth > 400)
                    plotSizeWidth = _plotSizeWidth;
                else
                    plotSizeWidth = 400;

                if(_plotSizeHeight > 300)
                    plotSizeHeight = _plotSizeHeight;
                else
                    plotSizeHeight = 300;
            }
175
            void setShowGrid(bool _needShowGrid) CV_OVERRIDE
176 177 178
            {
                needShowGrid = _needShowGrid;
            }
179
            void setShowText(bool _needShowText) CV_OVERRIDE
180 181 182
            {
                needShowText = _needShowText;
            }
183
            void setGridLinesNumber(int _gridLinesNumber) CV_OVERRIDE
184 185 186 187 188
            {
                if(_gridLinesNumber <= 0)
                    _gridLinesNumber = 1;
                gridLinesNumber = _gridLinesNumber;
            }
189
            void setPointIdxToPrint(int _cursorPos) CV_OVERRIDE
190 191 192 193 194
            {
                if(_cursorPos >= plotDataX.rows || _cursorPos < 0)
                    _cursorPos = plotDataX.rows - 1;
                cursorPos = _cursorPos;
            }
195
            //render the plotResult to a Mat
196
            void render(OutputArray _plotResult) CV_OVERRIDE
197 198
            {
                //create the plot result
199 200 201
                _plotResult.create(plotSizeHeight, plotSizeWidth, CV_8UC3);
                plotResult = _plotResult.getMat();
                plotResult.setTo(plotBackgroundColor);
202

203
                int NumVecElements = plotDataX.rows;
204

205
                Mat InterpXdata = linearInterpolation(plotMinX, plotMaxX, 0, plotSizeWidth, plotDataX);
206 207 208
                Mat InterpYdata = invertOrientation ?
                                  linearInterpolation(plotMaxY, plotMinY, 0, plotSizeHeight, plotDataY) :
                                  linearInterpolation(plotMinY, plotMaxY, 0, plotSizeHeight, plotDataY);
209

210 211
                //Find the zeros in image coordinates
                Mat InterpXdataFindZero = linearInterpolation(plotMinX_plusZero, plotMaxX_plusZero, 0, plotSizeWidth, plotDataX_plusZero);
212 213 214
                Mat InterpYdataFindZero = invertOrientation ?
                                          linearInterpolation(plotMaxY_plusZero, plotMinY_plusZero, 0, plotSizeHeight, plotDataY_plusZero) :
                                          linearInterpolation(plotMinY_plusZero, plotMaxY_plusZero, 0, plotSizeHeight, plotDataY_plusZero);
215

216 217
                int ImageXzero = (int)InterpXdataFindZero.at<double>(NumVecElements,0);
                int ImageYzero = (int)InterpYdataFindZero.at<double>(NumVecElements,0);
218

219 220
                double CurrentX = plotDataX.at<double>(cursorPos,0);
                double CurrentY = plotDataY.at<double>(cursorPos,0);
221

222
                drawAxis(ImageXzero,ImageYzero, CurrentX, CurrentY, plotAxisColor, plotGridColor);
223

224
                if(needPlotLine)
225
                {
226 227 228 229
                    //Draw the plot by connecting lines between the points
                    Point p1;
                    p1.x = (int)InterpXdata.at<double>(0,0);
                    p1.y = (int)InterpYdata.at<double>(0,0);
230

231 232 233 234 235
                    for (int r=1; r<InterpXdata.rows; r++)
                    {
                        Point p2;
                        p2.x = (int)InterpXdata.at<double>(r,0);
                        p2.y = (int)InterpYdata.at<double>(r,0);
236

237
                        line(plotResult, p1, p2, plotLineColor, plotLineWidth, 8, 0);
238

239 240
                        p1 = p2;
                    }
241 242 243
                }
                else
                {
244 245 246 247 248
                    for (int r=0; r<InterpXdata.rows; r++)
                    {
                        Point p;
                        p.x = (int)InterpXdata.at<double>(r,0);
                        p.y = (int)InterpYdata.at<double>(r,0);
249

250 251
                        circle(plotResult, p, 1, plotLineColor, plotLineWidth, 8, 0);
                    }
252
                }
253 254
            }

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
            protected:

            Mat plotDataX;
            Mat plotDataY;
            Mat plotDataX_plusZero;
            Mat plotDataY_plusZero;
            const char * plotName;

            //dimensions and limits of the plot
            int plotSizeWidth;
            int plotSizeHeight;
            double plotMinX;
            double plotMaxX;
            double plotMinY;
            double plotMaxY;
            double plotMinX_plusZero;
            double plotMaxX_plusZero;
            double plotMinY_plusZero;
            double plotMaxY_plusZero;
            int plotLineWidth;
Suleyman TURKMEN's avatar
Suleyman TURKMEN committed
275
            bool invertOrientation;
276 277 278 279
            bool needShowGrid;
            bool needShowText;
            int gridLinesNumber;
            int cursorPos;
280 281 282 283 284 285 286 287 288 289 290

            //colors of each plot element
            Scalar plotLineColor;
            Scalar plotBackgroundColor;
            Scalar plotAxisColor;
            Scalar plotGridColor;
            Scalar plotTextColor;

            //the final plot result
            Mat plotResult;

291 292 293
            //flag which enables/disables connection of plotted points by lines
            bool needPlotLine;

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
            void plotHelper(Mat _plotDataX, Mat _plotDataY)
            {
                plotDataX=_plotDataX;
                plotDataY=_plotDataY;

                int NumVecElements = plotDataX.rows;

                plotDataX_plusZero = Mat::zeros(NumVecElements+1,1,CV_64F);
                plotDataY_plusZero = Mat::zeros(NumVecElements+1,1,CV_64F);

                for(int i=0; i<NumVecElements; i++){
                    plotDataX_plusZero.at<double>(i,0) = plotDataX.at<double>(i,0);
                    plotDataY_plusZero.at<double>(i,0) = plotDataY.at<double>(i,0);
                }

                double MinX;
                double MaxX;
                double MinY;
                double MaxY;
                double MinX_plusZero;
                double MaxX_plusZero;
                double MinY_plusZero;
                double MaxY_plusZero;

318
                needPlotLine = true;
Suleyman TURKMEN's avatar
Suleyman TURKMEN committed
319
                invertOrientation = false;
320

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
                //Obtain the minimum and maximum values of Xdata
                minMaxLoc(plotDataX,&MinX,&MaxX);

                //Obtain the minimum and maximum values of Ydata
                minMaxLoc(plotDataY,&MinY,&MaxY);

                //Obtain the minimum and maximum values of Xdata plus zero
                minMaxLoc(plotDataX_plusZero,&MinX_plusZero,&MaxX_plusZero);

                //Obtain the minimum and maximum values of Ydata plus zero
                minMaxLoc(plotDataY_plusZero,&MinY_plusZero,&MaxY_plusZero);

                //setting the min and max values for each axis
                plotMinX = MinX;
                plotMaxX = MaxX;
                plotMinY = MinY;
                plotMaxY = MaxY;
                plotMinX_plusZero = MinX_plusZero;
                plotMaxX_plusZero = MaxX_plusZero;
                plotMinY_plusZero = MinY_plusZero;
                plotMaxY_plusZero = MaxY_plusZero;

                //setting the default size of a plot figure
                setPlotSize(600, 400);

                //setting the default plot line size
                setPlotLineWidth(1);

                //setting default colors for the different elements of the plot
                setPlotAxisColor(Scalar(0, 0, 255));
                setPlotGridColor(Scalar(255, 255, 255));
                setPlotBackgroundColor(Scalar(0, 0, 0));
                setPlotLineColor(Scalar(0, 255, 255));
                setPlotTextColor(Scalar(255, 255, 255));
355 356 357 358
                setShowGrid(true);
                setShowText(true);
                setGridLinesNumber(10);
                setPointIdxToPrint(-1);
359
            }
360

361 362
            void drawAxis(int ImageXzero, int ImageYzero, double CurrentX, double CurrentY, Scalar axisColor, Scalar gridColor)
            {
363 364 365 366 367 368 369 370 371
                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);
                }
372 373

                //Horizontal X axis and equispaced horizontal lines
374
                int LineSpace = cvRound(plotSizeHeight / (float)gridLinesNumber);
375 376 377
                int TraceSize = 5;
                drawLine(0, plotSizeWidth, ImageYzero, ImageYzero, axisColor);

378
               if(needShowGrid)
379 380 381 382 383 384 385 386 387 388 389 390 391
               for(int i=-plotSizeHeight; i<plotSizeHeight; i=i+LineSpace){

                    if(i!=0){
                        int Trace=0;
                        while(Trace<plotSizeWidth){
                            drawLine(Trace, Trace+TraceSize, ImageYzero+i, ImageYzero+i, gridColor);
                            Trace = Trace+2*TraceSize;
                        }
                    }
                }

                //Vertical Y axis
                drawLine(ImageXzero, ImageXzero, 0, plotSizeHeight, axisColor);
392
                LineSpace = cvRound(LineSpace * (float)plotSizeWidth / plotSizeHeight );
393

394
                if(needShowGrid)
395 396 397 398 399 400 401 402 403 404
                for(int i=-plotSizeWidth; i<plotSizeWidth; i=i+LineSpace){

                    if(i!=0){
                        int Trace=0;
                        while(Trace<plotSizeHeight){
                            drawLine(ImageXzero+i, ImageXzero+i, Trace, Trace+TraceSize, gridColor);
                            Trace = Trace+2*TraceSize;
                        }
                    }
                }
405 406
            }

407
            Mat linearInterpolation(double Xa, double Xb, double Ya, double Yb, Mat Xdata){
408

409
                Mat Ydata = Xdata*0;
410

411
                for (int i=0; i<Xdata.rows; i++){
412

413 414
                    double X = Xdata.at<double>(i,0);
                    Ydata.at<double>(i,0) = int(Ya + (Yb-Ya)*(X-Xa)/(Xb-Xa));
415

416 417 418
                    if(Ydata.at<double>(i,0)<0)
                        Ydata.at<double>(i,0)=0;
                }
419

420 421
                return Ydata;
            }
422

423
            void drawValuesAsText(double Value, int Xloc, int Yloc, int XMargin, int YMargin){
424

425 426
                char AxisX_Min_Text[20];
                double TextSize = 1;
427

428 429 430 431
                sprintf(AxisX_Min_Text, "%g", Value);
                Point AxisX_Min_Loc;
                AxisX_Min_Loc.x = Xloc+XMargin;
                AxisX_Min_Loc.y = Yloc+YMargin;
432

433 434
                putText(plotResult,AxisX_Min_Text, AxisX_Min_Loc, FONT_HERSHEY_COMPLEX_SMALL, TextSize, plotTextColor, 1, 8);
            }
435

436
            void drawValuesAsText(const char *Text, double Value, int Xloc, int Yloc, int XMargin, int YMargin){
437

438 439
                char AxisX_Min_Text[20];
                int TextSize = 1;
440

441 442 443 444
                sprintf(AxisX_Min_Text, Text, Value);
                Point AxisX_Min_Loc;
                AxisX_Min_Loc.x = Xloc+XMargin;
                AxisX_Min_Loc.y = Yloc+YMargin;
445

446 447
                putText(plotResult,AxisX_Min_Text, AxisX_Min_Loc, FONT_HERSHEY_COMPLEX_SMALL, TextSize, plotTextColor, 1, 8);
            }
448 449


450
            void drawLine(int Xstart, int Xend, int Ystart, int Yend, Scalar lineColor){
451

452 453 454 455 456 457
                Point Axis_start;
                Point Axis_end;
                Axis_start.x = Xstart;
                Axis_start.y = Ystart;
                Axis_end.x = Xend;
                Axis_end.y = Yend;
458

459 460 461
                line(plotResult, Axis_start, Axis_end, lineColor, plotLineWidth, 8, 0);
            }
        };
462

Suleyman TURKMEN's avatar
Suleyman TURKMEN committed
463
        Ptr<Plot2d> Plot2d::create(InputArray _plotData)
464
        {
Suleyman TURKMEN's avatar
Suleyman TURKMEN committed
465
            return Ptr<Plot2dImpl> (new Plot2dImpl (_plotData));
466 467
        }

Suleyman TURKMEN's avatar
Suleyman TURKMEN committed
468
        Ptr<Plot2d> Plot2d::create(InputArray _plotDataX, InputArray _plotDataY)
469
        {
Suleyman TURKMEN's avatar
Suleyman TURKMEN committed
470
            return Ptr<Plot2dImpl> (new Plot2dImpl (_plotDataX, _plotDataY));
471 472
        }
    }
473
}