Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Here is a function that plots the values of an array:

template<typename T> void graphArray(const char *title,T* data, int n, int height,bool cont)
{
Mat img(height+1,n,CV_8UC3);
img.setTo(Scalar(255,255,255));
T max=0;
for(int x=0;x<n;x++)
    if(data[x]>max)max=data[x];
if(!cont){
    for(int x=0;x<n;x++)
        img.at<Vec3b>((int)(height-data[x]*height/max),x)=Vec3b(255,0,0);
} else {
    int si,si1,inc;
    for(int x=0;x<n-1;x++){
        si=data[x]*height/max;si1=data[x+1]*height/max;
        if(si1>si)inc=1;else inc=-1;
        for(int v=si;v!=si1+inc;v+=inc)
            img.at<Vec3b>(height-v,x)=Vec3b(255,0,0);
    }
}
namedWindow(title,WINDOW_FREERATIO);
imshow(title,img);
}

The title parameter is the window title, data is the array to be drawn, n is the number of elements in data, height is the height of the image in pixels and cont defines if the line is continuous or dotted.

The function can be easily adapted for vectors or Mat variables. It's quite simple, but it does what you need. It detects the maximum, but the minimum is 0 (but you can simply normalize the minimum value, too).

Usage:

double data[10]; //or double *data;
//...give some values to data...
graphArray<double>("Plot",data,10,255,true);