# Using k means for image clustering

I am using c++,OpenCV library and in my software, I have estimated the optical flow in a video. Now, I want to group some moving objects, e.g. moving cars. I have used a dense optical flow algorithm (Farneback).

My first thoughts so far are to use "k means" algorithm to do the clustering.

I have thought of using the results of the Farneback optical flow to compute the displacement of the frames in each direction as following :

E.g. :

Let Dx be the displacement in x direction( either positive or negative) and Dy the displacement in y direction(either positive or negative) .

Then i pass the array [Dx,Dy] as an input to k means with k=2 clusters. I hope this will give a rough background / foreground substraction .

However i am facing problems in computing the displacemet because the output of calcOpticalFlowFarneback is InputOutputArray flow . Should I access this array using a function like that for example ? :

findDisplacements(const Mat& flow, int step) {

const Point2f& Dx,Dy;

const Point2f& fxy = flow.at<point2f>(y, x);

Dx=Point(cvRound(x+fxy.x))-Point(x,y);

Dy=Point(cvRound(y+fxy.y))-Point(y,x);

}