Ask Your Question

lfcooh's profile - activity

2018-09-26 20:58:07 -0600 received badge  Popular Question (source)
2015-10-22 08:08:24 -0600 commented question Does anyone know an appropriate algorithm to assign track id?

Sorry, Now I will show only core class

2015-10-22 08:07:01 -0600 received badge  Editor (source)
2015-10-22 07:30:00 -0600 asked a question 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 ...
(more)
2015-10-15 06:29:30 -0600 commented question How to improve Kalman Filter performance?

Do you have an example of each case? Please tell me. Thank a lot

2015-10-14 08:53:35 -0600 asked a question How to improve Kalman Filter performance?

I want to detect and track player on video but when a detected object lost, it get a new tracking ID. I want to get old tracking id with old object. How can I improve it? This code owner tell me " You should config Kalman Filter " . I'm very confuse about it.

public class Kalman extends KalmanFilter {
private KalmanFilter kalman;
private Point LastResult;
private double deltatime;

public void init() {

}

public Kalman(Point pt){
    kalman = new KalmanFilter(4, 2, 0, CvType.CV_32F);

    Mat transitionMatrix = new Mat(4, 4, CvType.CV_32F, new Scalar(0));
    float[] tM = { 
            1, 0, 1, 0, 
            0, 1, 0, 1,
            0, 0, 1, 0,
            0, 0, 0, 1 } ;
    transitionMatrix.put(0,0,tM);

    kalman.set_transitionMatrix(transitionMatrix);

    LastResult = pt;
    Mat statePre = new Mat(4, 1, CvType.CV_32F, new Scalar(0)); // Toa do (x,y), van toc (0,0)
    statePre.put(0, 0, pt.x);
    statePre.put(1, 0, pt.y);
    kalman.set_statePre(statePre);

    kalman.set_measurementMatrix(Mat.eye(2,4, CvType.CV_32F));

    Mat processNoiseCov = Mat.eye(4, 4, CvType.CV_32F);
    processNoiseCov = processNoiseCov.mul(processNoiseCov, 1e-4); //1e-4
    kalman.set_processNoiseCov(processNoiseCov);

    Mat id1 = Mat.eye(2,2, CvType.CV_32F);
    id1 = id1.mul(id1,1e-1);   
    kalman.set_measurementNoiseCov(id1);

    Mat id2 = Mat.eye(4,4, CvType.CV_32F);
    //id2 = id2.mul(id2,0.1);
    kalman.set_errorCovPost(id2);
}

public Kalman(Point pt, double dt, double Accel_noise_mag) {
    kalman = new KalmanFilter(4, 2, 0, CvType.CV_32F);
    deltatime = dt;

    Mat transitionMatrix = new Mat(4, 4, CvType.CV_32F, new Scalar(0));
    float[] tM = { 
            1, 0, 1, 0, 
            0, 1, 0, 1,
            0, 0, 1, 0,
            0, 0, 0, 1 } ;
    transitionMatrix.put(0,0,tM);

    kalman.set_transitionMatrix(transitionMatrix);

    // init
    LastResult = pt;
    Mat statePre = new Mat(4, 1, CvType.CV_32F, new Scalar(0)); // Toa do (x,y), van toc (0,0)
    statePre.put(0, 0, pt.x);
    statePre.put(1, 0, pt.y);
    statePre.put(2, 0, 0);
    statePre.put(3, 0, 0);
    kalman.set_statePre(statePre);

    Mat statePost = new Mat(4, 1, CvType.CV_32F, new Scalar(0));
    statePost.put(0, 0, pt.x);
    statePost.put(1, 0, pt.y);
    statePost.put(2, 0, 0);
    statePost.put(3, 0, 0);
    kalman.set_statePost(statePost);

    kalman.set_measurementMatrix(Mat.eye(2,4, CvType.CV_32F));

    //Mat processNoiseCov = Mat.eye(4, 4, CvType.CV_32F);
    Mat processNoiseCov = new Mat(4, 4, CvType.CV_32F, new Scalar(0));
    float[] dTime = { (float) (Math.pow(deltatime, 4.0) / 4.0), 0,
            (float) (Math.pow(deltatime, 3.0) / 2.0), 0, 0,
            (float) (Math.pow(deltatime, 4.0) / 4.0), 0,
            (float) (Math.pow(deltatime, 3.0) / 2.0),
            (float) (Math.pow(deltatime, 3.0) / 2.0), 0,
            (float) Math.pow(deltatime, 2.0), 0, 0,
            (float) (Math.pow(deltatime, 3.0) / 2.0), 0,
            (float) Math.pow(deltatime, 2.0) };
    processNoiseCov.put(0, 0, dTime);

    processNoiseCov = processNoiseCov.mul(processNoiseCov, Accel_noise_mag); // Accel_noise_mag = 0.5
    kalman.set_processNoiseCov(processNoiseCov);

    Mat id1 = Mat.eye(2,2, CvType.CV_32F);
    id1 = id1.mul(id1,1e-1);
    kalman.set_measurementNoiseCov(id1);

    Mat id2 = Mat.eye(4,4, CvType.CV_32F);
    id2 = id2.mul(id2,.1);
    kalman.set_errorCovPost(id2);
}

public Point getPrediction() {
    Mat prediction ...
(more)
2015-08-26 20:35:41 -0600 received badge  Student (source)
2015-08-18 03:05:03 -0600 asked a question How can I identify each object?

Hi everyone! I use code like this. I want to identify each object that detect from video for tracking each object I want to know position(x,y) of each object. How can I do? Please help me. Thank a lot.

public class MotionDetection 
{
    static int SENSITIVITY_VALUE = 30;
    static int BLUR_SIZE = 5;
    static int WIDTH = 640;
    static int HEIGHT = 480;
    static int count = 0;
    static OpenCVWindow thresholdWindow;
    static OpenCVWindow drawnWindow;
    //static OpenCVWindow testWindow;

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        thresholdWindow = new OpenCVWindow(WIDTH, HEIGHT);
        drawnWindow = new OpenCVWindow(WIDTH, HEIGHT);
        //testWindow = new OpenCVWindow(WIDTH, HEIGHT);
        readFromCamera();

    }

    static void readFromCamera() {
        while (true) {
            VideoCapture camera = new VideoCapture("D:\\Videos\\side.mp4");
            if (!camera.isOpened()) {
                System.out.println("Cannot open file");
            } else {
                Mat previousFrame = new Mat();
                Mat currentFrame = new Mat();
                Mat grayImage1 = new Mat();
                Mat grayImage2 = new Mat();
                Mat differenceImage = new Mat();
                Mat thresholdImage = new Mat();
                boolean frameSuccess;

                // THE INFINITE LOOP
                camera.read(previousFrame);

                Imgproc.resize(previousFrame, previousFrame, new Size(WIDTH, HEIGHT));
                Imgproc.cvtColor(previousFrame, grayImage1,
                        Imgproc.COLOR_BGR2GRAY);

                while (true) {

                    frameSuccess = camera.read(currentFrame);
                    if (frameSuccess == true) {
                        Imgproc.resize(currentFrame, currentFrame, new Size(WIDTH, HEIGHT));
                        Imgproc.cvtColor(currentFrame, grayImage2,
                                Imgproc.COLOR_BGR2GRAY);

                    } else {
                        break;
                    }

                    // DIFFERENCE
                    Core.absdiff(grayImage1, grayImage2, differenceImage);
                    Imgproc.threshold(differenceImage, thresholdImage,
                            SENSITIVITY_VALUE, 255, Imgproc.THRESH_BINARY);
                    Imgproc.blur(thresholdImage, thresholdImage, new Size(
                            BLUR_SIZE, BLUR_SIZE));
                    //Imgproc.medianBlur(thresholdImage, thresholdImage,1);
                    Imgproc.threshold(thresholdImage, thresholdImage,
                            SENSITIVITY_VALUE, 255, Imgproc.THRESH_BINARY);

                    thresholdWindow.showImage(thresholdImage);

                    searchForMovement(thresholdImage, currentFrame);

                    //Previous frame is now this frame
                    grayImage2.copyTo(grayImage1);
                }
                camera.release();
            }

        }
    }

    static void searchForMovement(Mat thresholdImage, Mat frame) {
        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        Mat hierarchy = new Mat();
        Imgproc.findContours(thresholdImage, contours, hierarchy,
                Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
        Rect objectBoundingRectangle = new Rect(0, 0, 0, 0);
        for (int i = 0; i < contours.size(); i++) {
            objectBoundingRectangle = Imgproc.boundingRect(contours.get(i));
            if (objectBoundingRectangle.area() > 25 & objectBoundingRectangle.area() < 100 ) {            
                            Core.rectangle(frame, objectBoundingRectangle.tl(), objectBoundingRectangle.br(), new Scalar(0,255,0));                            
            }
        }

    drawnWindow.showImage(frame);

}

In this code, I know a lot of position(x,y) of all object.

2015-08-12 01:07:52 -0600 received badge  Enthusiast
2015-08-11 01:44:48 -0600 commented question How to detect small object like a football player?

Hi sir , Now I use Rect and got output "x = 738, y = 316,width = 9, Height = 3" .to draw rectangle over detected object. I want to fix object(each player) to have 1 object and a lot of position(x,y). For tracking every player in the field. Can I do like this? Thank you very much

2015-08-10 04:32:32 -0600 commented question How to detect small object like a football player?

Excuse me, I found some technique now and I got this output Images Do you know how to remove unwanted object? I want only player.

2015-08-06 21:21:59 -0600 commented question How to detect small object like a football player?

I try using bg subtraction, it got this output link text but I want only player, ball and line of field. How can I do? This is original image link text

2015-07-02 04:54:57 -0600 commented question How to detect small object like a football player?

Thank you sir. After that what technique I should use? I want to detect ,tracking and get position of object.

2015-07-02 04:23:30 -0600 commented question How to detect small object like a football player?

This size of object(player) can detect,Right? Can it be smaller than this?

2015-07-01 21:43:41 -0600 commented question How to detect small object like a football player?

Do you have an example code for backgroud substraction? Thank you

2015-06-25 23:57:36 -0600 asked a question How to detect small object like a football player?

I want to know what a techniques can use for this problem. I have an example of captured image from video. In real situation it is video file

image description