Ask Your Question
0

Plotting values from video frames

asked 2017-01-19 05:09:30 -0600

sarmad gravatar image

updated 2017-01-19 05:12:24 -0600

HI

My question about plotting using cv::line , after doing some calculations , I'm getting double values for each video frame e.g :

0.3
0.288462
0.288462
0.289614
0.307465
0.20198 0.166522
0.16
0.24
0.249815
0.270398
0.269032

how can I plot them using cv line , or any other method ?

edit retag flag offensive close merge delete

4 answers

Sort by ยป oldest newest most voted
1

answered 2017-01-19 07:48:25 -0600

kbarni gravatar image

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);
edit flag offensive delete link more
0

answered 2017-01-19 11:08:41 -0600

sarmad gravatar image

I have addedd template<typename T> void graphArray into my code below , where double data[15784];

is saving double values , 15784 is the number of frames in the video file . The code compiles witout error , when

I run , it shows the plot window but it is empty and became unresponsive after a while .

 #include <dlib/opencv.h>
 #include <opencv2/highgui/highgui.hpp>
 #include <dlib/image_processing/frontal_face_detector.h>
 #include <dlib/image_processing/render_face_detections.h>
 #include <dlib/image_processing.h>
 #include <dlib/gui_widgets.h>

using namespace dlib;
using namespace std;
using namespace cv;


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);
    }



 int main()
     {
      try
     { 
    cv::VideoCapture cap("1.avi");
    if (!cap.isOpened())
    {
        cerr << "Unable to connect to camera" << endl;
        return 1;
    }

    image_window win;

    frontal_face_detector detector = get_frontal_face_detector();
    shape_predictor pose_model;
    deserialize("shape_predictor_68_face_landmarks.dat") >> pose_model;

   double data[15784];
    while(!win.is_closed())
    {
        // Grab a frame
        cv::Mat temp;
        cap >> temp;
    if ( temp.empty())
 {
    // reach to the end of the video file
    break;
}

        cv_image<bgr_pixel> cimg(temp);

        std::vector<rectangle> faces = detector(cimg);
        std::vector<full_object_detection> shapes;

        for (unsigned long i = 0; i < faces.size(); ++i)
      {


       full_object_detection shape = pose_model(cimg, faces[i]);


        double P37_41_x = shape.part(37).x() - shape.part(41).x();
        double P37_41_y=  shape.part(37).y() -shape.part(41).y() ;

        double output=sqrt((P37_41_x * P37_41_x) + (P37_41_y * P37_41_y));


       data[i]=output;

      graphArray<double>("Plot",data,10,255,true);

   shapes.push_back(pose_model(cimg, faces[i]));
   const full_object_detection& d = shapes[0];

              }

        win.clear_overlay();
        win.set_image(cimg);
        win.add_overlay(render_face_detections(shapes));

      }
 }
       catch(serialization_error& e)
      {
          cout << "You need dlib's default face landmarking model file to run this example." << endl;
          cout << "You can get it from the following URL: " << endl;
          cout << "   http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
          cout << endl << e.what() << endl;
         }
          catch(exception& e)
        {
            cout << e.what() << endl;
           }
       }
edit flag offensive delete link more

Comments

You might try to put the namedwindow line in the main function, just before the while(). There is also a bug: you should display data only after the for ends (right before the win.clear): graphArray<double>("Plot",data,faces.size(),255,true);

kbarni gravatar imagekbarni ( 2017-01-20 03:07:34 -0600 )edit

@kbarni , I have tried you suggestions , but it is the same problem I described .

sarmad gravatar imagesarmad ( 2017-01-22 12:33:18 -0600 )edit
0

answered 2017-01-19 06:40:58 -0600

pi-null-mezon gravatar image

Use putText

edit flag offensive delete link more

Comments

Thanks @pi-null-mezon ,

I don't want to display the values on the video , I need to plot them like matlab style.image

sarmad gravatar imagesarmad ( 2017-01-19 06:52:19 -0600 )edit
0

answered 2017-01-19 07:50:07 -0600

All these answers are nice indeed, but there is also the plot module in OpenCV itself! Take a look here: https://github.com/opencv/opencv_cont...

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-01-19 05:09:30 -0600

Seen: 617 times

Last updated: Jan 19 '17