Does anyone know an appropriate algorithm to assign track id?
I use this code to track object from findContour method.
public class Tracker extends JTracker {
public Tracker(float _dt, float _Accel_noise_mag, double _dist_thres,
int _maximum_allowed_skipped_frames, int _max_trace_length) {
tracks = new Vector<>();
dt = _dt;
Accel_noise_mag = _Accel_noise_mag;
dist_thres = _dist_thres;
maximum_allowed_skipped_frames = _maximum_allowed_skipped_frames;
max_trace_length = _max_trace_length;
track_removed = 0;
}
double euclideanDist(Point p, Point q) {
Point diff = new Point(p.x - q.x, p.y - q.y);
return Math.sqrt(diff.x * diff.x + diff.y * diff.y);
}
public void update(Vector<Rect> rectArray, Vector<Point> detections, Mat imag) {
this.rectArray = rectArray;
this.imag = imag;
if (tracks.size() == 0) {
// If no tracks yet
for (int i = 0; i < detections.size(); i++) {
Track tr = new Track(detections.get(i), dt,
Accel_noise_mag, nextTractID++);
tracks.add(tr);
}
}
int N = tracks.size();
int M = detections.size();
double[][] Cost = new double[N][M]; // size: N, M
assignment.clear();
for (int i = 0; i < tracks.size(); i++) {
for (int j = 0; j < detections.size(); j++) {
Cost[i][j] = euclideanDist(tracks.get(i).prediction, detections.get(j));
}
}
AssignmentOptimal APS = new AssignmentOptimal();
APS.Solve(Cost, assignment);
Vector<Integer> not_assigned_tracks = new Vector<>();
for (int i = 0; i < assignment.size(); i++) {
if (assignment.get(i) != -1) {
if (Cost[i][assignment.get(i)] > dist_thres) {
assignment.set(i, -1);
not_assigned_tracks.add(i);
}
} else {
tracks.get(i).skipped_frames++;
}
}
for (int i = 0; i < tracks.size(); i++) {
if (tracks.get(i).skipped_frames > maximum_allowed_skipped_frames) {
tracks.remove(i);
assignment.remove(i);
track_removed++;
i--;
}
}
Vector<Integer> not_assigned_detections = new Vector<>();
Vector<Integer> assigned_id = new Vector<>();
if (tracks.size() <= 23) {
for (int i = 0; i < detections.size(); i++) {
if (!assignment.contains(i)) {
not_assigned_detections.add(i);
} else {
assigned_id.add(i);
}
}
}
if (not_assigned_detections.size() > 0) {
for (Integer not_assigned_detection : not_assigned_detections) {
Track tr = new Track(detections.get(not_assigned_detection), dt, Accel_noise_mag, nextTractID++);
tracks.add(tr);
trackIds.add(nextTractID);
}
}
// Update Kalman Filters state
updateKalman(imag, detections);
for (int j = 0; j < assignment.size(); j++) {
if (assignment.get(j) != -1) {
pt1 = new Point(
(int) ((rectArray.get(assignment.get(j)).tl().x + rectArray
.get(assignment.get(j)).br().x) / 2), rectArray
.get(assignment.get(j)).br().y);
pt2 = new Point(
(int) ((rectArray.get(assignment.get(j)).tl().x + rectArray
.get(assignment.get(j)).br().x) / 2), rectArray
.get(assignment.get(j)).tl().y);
//show ID
Imgproc.putText(imag, tracks.get(j).track_id + "", pt2,
2 * Core.FONT_HERSHEY_PLAIN, 1, new Scalar(255, 255,
255), 1);
if (tracks.get(j).history.size() < 100) {
tracks.get(j).history.add(pt1);
} else {
tracks.get(j).history.remove(0);
tracks.get(j).history.add(pt1);
}
double x = pt2.x;
double y = pt2.y;
if (tracks.get(j).track_id == 0) { //& ((pt2.x - p0.x)<=5 | (pt2.x - p0.x)>= -5) & ((pt2.y - p0.y)<=5 | (pt2.y - p0.y)>= -5)){
p0 = pt2;
double dist = euclideanDist(tracks.get(j).trace.get(tracks.get(j).trace.size() - 1), p0);
pointArray0.add(p0);
//System.out.println("Point1:"+p0);
//System.out.println(tracks.get(j).trace.get(tracks.get(j).trace.size()-1));
//System.out.println("Dist:"+dist);//Insert(0, pt2.x, pt2.y,dist);
}
if (tracks.get(j).track_id == 1) {
p1 = pt2;
double dist ...
dear, we don't need to see all* of your code, to understand, that you just love copypasta ...
please reduce it to the bare minimum.
Sorry, Now I will show only core class