Hello.
I am trying to calculate the distance between coordinates.
I am detecting the movement from camera/video, using for tennis ball tracking and saving the coordinates of all candidates
so that means the ball movement and also the player or whatever noise there could be. My final goal is to eliminate as much noise
as possible or all of it so there would be only coordinates from the ball left. If you have any suggestions on how i could achive that I would apricciate that.
But right now it's not my main question so it's not so important for now.
I store the coordinates in vector of points and also writing them to an excel file.
So now I have my excel file with the 1 column = frame count, 2 column = number of candidates on that frame, and after that all the x and y candidates.
What I want to do is calcualte the distance of each point with all the points in the next frame.
Let's say my excel with coordinates looks like that:
frame nr candidates X Y X Y X Y X Y
1 3 x1 y1 x2 y2 x3 y3
2 4 x5 y5 x6 y6 x7 y7 x8 y8
3 1 x9 y9
4 2 x10 y10 x11 y12
Now I want to calculate all the distances so the x1,y1 and x5,y5 distance, x1,y1 and x6,y6. x1,y1 and x7,y7. x1,y1 and x8,y8 then x2,y2 and x5,y5 and so on. So if i have 3 candidates in the 1st frame and 4 in the 2nd frame
I would need to get 12 distances
then the same thing with 2nd and 3rd : x5,y5 and x9,y9 then x6,y6 and x9,y9...
frame and 3rd and 4th and so on....
d = sqrt((x2-x1)^2 + (y2-y1)^2)
is the formula that I want to use.
How could i do this?
Do i have to use iterators?
I know how to do it if i only would have 2 points, found an example code on this site which looks like this :
#include <stdio.h>
#include <iostream>
#include <vector>
using std::cout;
using std::cin;
//2 dimensional vector struct
struct points
{
float x, y;
};
int main()
{
int Dimensions = 0;
float r;
std::vector<points> pointvect; // vector declaration
cout << "Set dimension of vector";
cin >> Dimensions; //with struct only 2D accepted
for (int i = 0; i<Dimensions; i++)
{ // Input coordinates x, ye
pointvect.resize(pointvect.size() + 1); //This will make pointvect be able to hold 1 more point at the end
cout << "Input x:";
cin >> pointvect[i].x; //assign values
cout << "Input y:";
cin >> pointvect[i].y;
}
for (int i = 0; i<pointvect.size() - 1; i++)
{
cout << "Distance between " << pointvect[i].x << "," << pointvect[i].y << " and " << pointvect[i + 1].x << "," << pointvect[i + 1].y << std::endl;
//Pythagorean Theorem (also used to calculate distance can be found by taking sqrt ( (y2 - y1)^2 + (x2-x1)^2 )
float Distance = sqrt(pow((pointvect[i].y - pointvect[i + 1].y), 2) + pow((pointvect[i].x - pointvect[i + 1].x), 2));
cout << "Distance:" << Distance << std::endl;
}
system("pause");
return 0;
}
Here is the my code where i create my excel file:
void writeToExell(vector < vector <Point> > &FrameVector)
{
Book* book = xlCreateBook();
Sheet* sheet = book->addSheet(L"Sheet1");
if (book)
{
if (sheet)
{
for (char k = 0; k < 40; k = k + 2)
{
sheet->writeStr(1, k + 3, L"x"); //print x,y to the first row
sheet->writeStr(1, k + 4, L"y");
}
for (int i = 0; i < FrameVector.size(); i++)
{
sheet->writeNum(i+1, 0, i); // frame count, esimene tulp
sheet->writeNum(i+1, 1, FrameVector[i].size()); // mitu koordinaati sisaldab antud frame, teise tulpa
for (int i_frame = 0; i_frame < FrameVector[i].size(); i_frame++)
{
sheet->writeNum(i+1, i_frame * 2 + 3, FrameVector[i][i_frame].x); // kirjutab välja x koordinaadid
sheet->writeNum(i+1, i_frame * 2 + 4, FrameVector[i][i_frame].y); // kirjutab välja y koordinaadid
}
}
}
}
if (book->save(L"koordinaadid.xls"))
{
::ShellExecute(NULL, L"open", L"koordinaadid.xls", NULL, NULL, SW_SHOW); // avab exceli
}
else
{
cout << book->errorMessage() << endl;
}
book->release();
}
I hope my question is clear enaugh. If not i will try to explain more. I am a beginner in c++ and in programming in general.
Sorry for my english.
Thank you in advance for any help.