OpenVC人数不能正常运作

时间:2018-05-24 08:13:51

标签: java android opencv

由于某种原因,当我试图找到它们时,没有检测到人。我试图将python从这里移植到java。

    class videoCapture {

        public void run() {
    // create new classes to meet classes requirement

            VideoCapture cap = new VideoCapture(getClass().getResource("/peopleCounter.avi").getPath());
            BackgroundSubtractorMOG2 mog2 = Video.createBackgroundSubtractorMOG2();

            Mat frame = new Mat();
            AtomicInteger countU = new AtomicInteger(0);
            AtomicInteger countD = new AtomicInteger(0);
            for (int i = 0; i < 19; i++) {
                System.out.println(i);
                System.out.println(cap.get(i));
            }

            double width = cap.get(3);
            double height = cap.get(3);

            double frameA = width * height;
            double areaTH = frameA / 250;
            System.out.println("area threshold" + areaTH);

            double lineUp = 2 * (height / 5);
            double lineDown = 3 * (height / 5);


            double up_limit = 1 * (height / 5);
            double down_limit = 4 * (height / 5);

            System.out.println("Red line y" + lineUp);
            System.out.println("Blue line y:" + lineDown);

            Point pt1 = new Point(0, lineDown);
            Point pt2 = new Point(width, lineDown);
            List<MatOfPoint> ptsList1 = new ArrayList();
            ptsList1.add(new MatOfPoint(pt1, pt2));
            Point pt3 = new Point(0, lineUp);
            Point pt4 = new Point(width, lineUp);
            List<MatOfPoint> ptsList2 = new ArrayList();
            ptsList2.add(new MatOfPoint(pt3, pt4));

            Point pt5 = new Point(0, up_limit);
            Point pt6 = new Point(width, up_limit);
            List<MatOfPoint> ptsList3 = new ArrayList();
            ptsList3.add(new MatOfPoint(pt5, pt6));
            Point pt7 = new Point(0, down_limit);
            Point pt8 = new Point(width, down_limit);
            List<MatOfPoint> ptsList4 = new ArrayList();
            ptsList4.add(new MatOfPoint(pt7, pt8));

            /*ArrayList<Point> test = new ArrayList<Point>(Arrays.asList(new Point(20,3), new Point(2,33)));
            System.out.println(test);
            test.add(new Point(10,33));
            System.out.println(test);*/


            int font = Core.FONT_HERSHEY_SIMPLEX;
            List<Person> persons = new ArrayList<Person>();
            int maxPAge = 5;
            AtomicInteger pid = new AtomicInteger(1);
            Person mary = new Person(10, 10, 10, 10);

            int frameCount = 0;
            while (cap.read(frame)) {
                System.out.println(frameCount++);

                for (int i = 0; i < persons.size(); i++) {
                    Person pers = persons.get(i);
                    pers.ageOne();
                }

                Mat fgMask = new Mat();
                mog2.apply(frame, fgMask, 0.1);

                Mat output = new Mat();
                frame.copyTo(output, fgMask);

                Imgproc.threshold(fgMask, output, 200, 255, Imgproc.THRESH_BINARY);
                Mat kernelOp = new Mat(new Size(3, 3), CvType.CV_8UC1, new Scalar(255));
                Mat kernelC1 = new Mat(new Size(11, 11), CvType.CV_8UC1, new Scalar(255));

                Imgproc.morphologyEx(fgMask, output, Imgproc.MORPH_OPEN, kernelOp);
                Imgproc.morphologyEx(fgMask, output, Imgproc.MORPH_CLOSE, kernelC1);

                List<MatOfPoint> contours = new ArrayList<>();
                //Imgproc.findContours(frame, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
                Imgproc.findContours(fgMask, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

                for (int i = 0; i < contours.size(); i++) {
                    double area = Imgproc.contourArea(contours.get(i));

                    if (area > areaTH) {
                        Moments M = Imgproc.moments(contours.get(i));
                        double cx = M.get_m10() / M.get_m00();
                        double cy = M.get_m01() / M.get_m00();

                        Rect rectVal = Imgproc.boundingRect(contours.get(i));
                        AtomicBoolean newBool = new AtomicBoolean(true);

                        IntStream.range((int) up_limit, (int) down_limit).forEachOrdered(n -> {
                            for (int h = 0; h < persons.size(); h++) {
                                Person pers = persons.get(h);
                                if (Math.abs(cx - 1) <= width && Math.abs(cy - 1) <= height) {
                                    newBool.set(false);
                                    pers.updateCoords((int) cx, (int) cy);
                                    if (pers.goingUp(lineDown, lineUp) == true) {
                                        countU.getAndIncrement();
                                    } else if (pers.goingDown(lineDown,lineUp) == true) {
                                        countD.getAndIncrement();
                                    }

                                } if (pers.getState() == '1') {
                                    if (pers.getDir() == "down" && pers.getY() < down_limit) {
                                        pers.setDone();
                                    } else if (pers.getDir() == "up" && pers.getY() < down_limit) {
                                        pers.setDone();
                                    }
                                }

                            }
                            if (newBool.get() == true) {
                                Person p = new Person(pid.get() ,(int) cx,(int) cy, maxPAge);
                                persons.add(p);
                                pid.getAndIncrement();
                            }
                        });

                        Imgproc.circle(frame, new Point((int) cx, (int) cy), 5, new Scalar (0,0,255),-1);
                        Imgproc.rectangle(frame, new Point(rectVal.x,rectVal.y), new Point(rectVal.x+rectVal.width, rectVal.y+rectVal.height),new Scalar (0,10,155), 2);
                        Imgproc.drawContours(frame, contours,-1,new Scalar (0,0,255),-1);


                    }


                }

                String strUp = "UP: " + countU;
                String strDown = "Down" + countD;
                Imgproc.polylines(frame, ptsList1,false,new Scalar(255,0,0),2);
                Imgproc.polylines(frame, ptsList2,false,new Scalar(0,0,255),2);
                Imgproc.polylines(frame, ptsList3,false,new Scalar(255,255,255),1);
                Imgproc.polylines(frame, ptsList4,false,new Scalar(255,255,255),1);

                Imgproc.putText(frame,strUp, new Point(10,40), font, .5,new Scalar(255,255,255),2);
                Imgproc.putText(frame,strUp, new Point(10,40), font, .5,new Scalar(0,0,255),2);
                Imgproc.putText(frame,strDown, new Point(10,40), font, .5,new Scalar(255,255,255),2);
                Imgproc.putText(frame,strDown,new Point(10,90),font, .5, new Scalar(255,0,0), 1);

                DisplayImage.displayImage(DisplayImage.Mat2BufferedImage(frame));

            }

            cap.release();
        }

    }

    public class ScanTest {

        public static void main(String[] args) {
            System.out.println("Hello, OpenCV");
            // Load the native library.
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
            new videoCapture().run();


        }
    }

import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import java.util.ArrayList;
import java.util.List;

public class Person {
    private int i;
    private int x;
    private int y;
    private ArrayList<Point>  tracks;
    private Scalar rgb;
    private boolean done;
    private char state;
    private int age;
    private int maxAge;
    private String dir;


    public Person(int i, int xi, int yi, int maxAge) {
        this.i = i;
        this.x = xi;
        this.y = yi;
        this.tracks = new ArrayList<Point>();
        this.rgb = new Scalar((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256));
        this.done = false;
        this.state = '0';
        this.age = 0;
        this.maxAge = maxAge;
        this.dir = "Nil";
    }

    public Scalar getRGB() {
        return rgb;
    }

    public ArrayList<Point> getTracks(){
        return tracks;
    }

    public double getId() {
        return i;
    }

    public char getState() {
        return  state;
    }

    public String getDir() {
        return  dir;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public void updateCoords(int xn, int yn) {
        this.age = 0;
        this.tracks.add(new Point(x,y));
        this.x = xn;
        this.y = yn;
    }

    public void setDone() {
        this.done = true;
    }

    public boolean goingUp(double midStart, double midEnd) {
        if (tracks.size() >= 2) {
            if (state == 0) {
                if (tracks.get(-1).x < midEnd && tracks.get(0).x >= midEnd) {
                    this.state = '1';
                    this.dir = "up";
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }return false;
    }

    public boolean goingDown(double midStart, double midEnd) {
        if (tracks.size() >= 2) {
            if (state == 0) {
                if (tracks.get(-1).x > midStart && tracks.get(0).x <= midStart) {
                    this.state = '1';
                    this.dir = "down";
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }return false;
    }

    public boolean ageOne(){
        this.age++;
        if (this.age > this.maxAge){
            this.done = true;
            return  true;
        }
        return false;
    }

}

嗨,由于某种原因,当我试图找到它们时,人们没有被发现。我试图将python从这里移植到java。我认为它与检测轮廓有关。有时人们周围有一个矩形,但它很简短。

0 个答案:

没有答案