vectores pagina freepik flechas flecha editables curvas bonitas c++ opencv vector

c++ - pagina - OpenCV Cómo trazar vectores de velocidad como flechas en el uso de una sola imagen estática



freepik (3)

Estoy intentando trazar vectores de velocidad como en matlab usamos la función "quiver" http://www.mathworks.com/help/techdoc/ref/quiver.html

Necesito portar la misma metodología en C ++ usando la biblioteca OpenCV.

He escuchado que hay algunos métodos de flujo óptico, es decir, Lucas y Kanade (cvCalOpticalFlowLK) o Horn and Schunck (cvCalOpticalFlowHS) o método de coincidencia de bloques (cvCalOpticalFlowBM)

pero todas estas funciones toman dos imágenes, mientras que necesito usar una imagen porque estoy trabajando en las huellas dactilares.

Por favor ayúdame ...

[Editar] Solución encontrada

void cvQuiver(IplImage*Image,int x,int y,int u,int v,CvScalar Color, int Size,int Thickness){ cv::Point pt1,pt2; double Theta; double PI = 3.1416; if(u==0) Theta=PI/2; else Theta=atan2(double(v),(double)(u)); pt1.x=x; pt1.y=y; pt2.x=x+u; pt2.y=y+v; cv::line(Image,pt1,pt2,Color,Thickness,8); //Draw Line Size=(int)(Size*0.707); if(Theta==PI/2 && pt1.y > pt2.y) { pt1.x=(int)(Size*cos(Theta)-Size*sin(Theta)+pt2.x); pt1.y=(int)(Size*sin(Theta)+Size*cos(Theta)+pt2.y); cv::line(Image,pt1,pt2,Color,Thickness,8); //Draw Line pt1.x=(int)(Size*cos(Theta)+Size*sin(Theta)+pt2.x); pt1.y=(int)(Size*sin(Theta)-Size*cos(Theta)+pt2.y); cv::line(Image,pt1,pt2,Color,Thickness,8); //Draw Line } else{ pt1.x=(int)(-Size*cos(Theta)-Size*sin(Theta)+pt2.x); pt1.y=(int)(-Size*sin(Theta)+Size*cos(Theta)+pt2.y); cv::line(Image,pt1,pt2,Color,Thickness,8); //Draw Line pt1.x=(int)(-Size*cos(Theta)+Size*sin(Theta)+pt2.x); pt1.y=(int)(-Size*sin(Theta)-Size*cos(Theta)+pt2.y); cv::line(Image,pt1,pt2,Color,Thickness,8); //Draw Line } }


Basado en el código de Dan y la sugerencia de mkuse, aquí hay una función con la misma sintaxis que cv :: line ():

static void arrowedLine(InputOutputArray img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int line_type=8, int shift=0, double tipLength=0.1) { const double tipSize = norm(pt1-pt2)*tipLength; // Factor to normalize the size of the tip depending on the length of the arrow line(img, pt1, pt2, color, thickness, line_type, shift); const double angle = atan2( (double) pt1.y - pt2.y, (double) pt1.x - pt2.x ); Point p(cvRound(pt2.x + tipSize * cos(angle + CV_PI / 4)), cvRound(pt2.y + tipSize * sin(angle + CV_PI / 4))); line(img, p, pt2, color, thickness, line_type, shift); p.x = cvRound(pt2.x + tipSize * cos(angle - CV_PI / 4)); p.y = cvRound(pt2.y + tipSize * sin(angle - CV_PI / 4)); line(img, p, pt2, color, thickness, line_type, shift); }

Veremos si a quienes mantengan el repositorio de OpenCV les gustará :-)


El cvCalOpticalFlowLK no traza vectores de velocidad, calcula estos vectores de velocidad. Si no tiene estos vectores, debe llamar a esta función con dos imágenes. Supongo que ya tienes estos vectores, y solo quieres trazarlos.

En este caso, puede utilizar la función cv :: line , por ejemplo:

cv::line(yourImage, cv::Point(baseX, baseY), cv::Point(endX, endY));

¡Espero que esto ayude!


Estoy completando la respuesta actual aquí, que falla al dar el tamaño correcto de cada una de las puntas de las flechas. MATLAB lo hace de manera que cuando una flecha es casi un punto, no tiene ninguna punta, mientras que para las flechas largas muestra una gran punta, como muestra la siguiente imagen.

Para obtener este efecto, necesitamos normalizar el "tamaño de la punta" de cada flecha sobre el rango de longitud de las flechas. El siguiente código hace el truco

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

Y este es un ejemplo de cómo se vería este código OpenCV

Espero que esto ayude a otras personas en Google para el mismo problema.