How can I identify each object?

asked 2015-08-18 03:05:03 -0600

lfcooh gravatar image

updated 2015-08-18 06:41:52 -0600

theodore gravatar image

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.

edit retag flag offensive close merge delete