OpenCV How to Plot velocity vectors as arrows in using single static image

前端 未结 3 1635
情深已故
情深已故 2020-12-23 22:59

I am trying to plot velocity vectors like in matlab we use \"quiver\" function http://www.mathworks.com/help/techdoc/ref/quiver.html

I need to port same methodology

3条回答
  •  旧时难觅i
    2020-12-23 23:14

    I am kind of completing the current answer here, which fails in giving the right size of each of the arrows' tip. MATLAB does it in a way that when an arrow is nearly a dot, it doesn't have any tip, while for long arrows it shows a big tip, as the following image shows.

    enter image description here

    To get this effect, we need to normalise the "tip size" of each of the arrow over the range of arrows' length. The following code does the trick

        double l_max = -10;
    
        for (int y = 0; y < img_sz.height; y+=10)                                                           // First iteration, to compute the maximum l (longest flow)
        {
            for (int x = 0; x < img_sz.width; x+=10)
            {
                double dx = cvGetReal2D(velx, y, x);                                                        // Gets X component of the flow
                double dy = cvGetReal2D(vely, y, x);                                                        // Gets Y component of the flow
    
                CvPoint p = cvPoint(x, y);
    
                double l = sqrt(dx*dx + dy*dy);                                                             // This function sets a basic threshold for drawing on the image
    
                if(l>l_max) l_max = l;
            }
        }
    
    
        for (int y = 0; y < img_sz.height; y+=10)
    {
        for (int x = 0; x < img_sz.width; x+=10)
        {
            double dx = cvGetReal2D(velx, y, x);                                                        // Gets X component of the flow
            double dy = cvGetReal2D(vely, y, x);                                                        // Gets Y component of the flow
    
            CvPoint p = cvPoint(x, y);
    
            double l = sqrt(dx*dx + dy*dy);                                                             // This function sets a basic threshold for drawing on the image
            if (l > 0)
            {
                double spinSize = 5.0 * l/l_max;                                                        // Factor to normalise the size of the spin depeding on the length of the arrow
    
                CvPoint p2 = cvPoint(p.x + (int)(dx), p.y + (int)(dy));
                cvLine(resultDenseOpticalFlow, p, p2, CV_RGB(0,255,0), 1, CV_AA);
    
                double angle;                                                                           // Draws the spin of the arrow
                angle = atan2( (double) p.y - p2.y, (double) p.x - p2.x );
    
                p.x = (int) (p2.x + spinSize * cos(angle + 3.1416 / 4));
                p.y = (int) (p2.y + spinSize * sin(angle + 3.1416 / 4));
                cvLine( resultDenseOpticalFlow, p, p2, CV_RGB(0,255,0), 1, CV_AA, 0 );
    
                p.x = (int) (p2.x + spinSize * cos(angle - 3.1416 / 4));
                p.y = (int) (p2.y + spinSize * sin(angle - 3.1416 / 4));
                cvLine( resultDenseOpticalFlow, p, p2, CV_RGB(0,255,0), 1, CV_AA, 0 );
    
            }
       }
    }
    

    And this is an example of how this OpenCV code would look like

    enter image description here

    I hope this help other people Googling for the same issue.

提交回复
热议问题