Partage
  • Partager sur Facebook
  • Partager sur Twitter

comptage des voitures en utilisant java et opencv

    17 mai 2018 à 19:56:56

    Bonjour,

    cela fait des jours que je me prends la tête sur ce algorithme , je suis obligé de le faire pour mon projet de fin d’étude, un peu d'aide serait la bienvenue :).

    Je dois faire un algorithme qui compte les voitures dans un vidéo , j'ai utilisé la langage java et open cv

    mon programme fonctionne mais compte la voiture plusieurs fois 

    package application;
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayInputStream;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import org.opencv.core.Point;
    import org.opencv.core.Rect;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import org.opencv.core.Point;
    
    import org.opencv.core.Core;
    import org.opencv.core.CvType;
    import org.opencv.core.Mat;
    import org.opencv.core.MatOfByte;
    import org.opencv.core.MatOfPoint;
    import org.opencv.core.Scalar;
    import org.opencv.core.Size;
    import org.opencv.highgui.Highgui;
    import org.opencv.imgcodecs.Imgcodecs;
    import org.opencv.imgproc.Imgproc;
    import org.opencv.videoio.VideoCapture;
     
    public class JavaCVPrjt01 {
        static {
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        }
     
        static Mat imag = null;
     
        public static void main(String[] args) {
            JFrame jframe = new JFrame("Conteur ODV");
            jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JLabel vidpanel = new JLabel();
            jframe.setContentPane(vidpanel);
            jframe.setSize(640, 480);
            jframe.setVisible(true);
    
            Mat frame = new Mat();
            Mat outerBox = new Mat();
            Mat diff_frame = null;
            Mat tempon_frame = null;
            ArrayList<Rect> array = new ArrayList<Rect>();
            String filename ="C:\\Users\\user\\Downloads\\a1.mp4";
            VideoCapture camera = new VideoCapture(filename);
            Size sz = new Size(640, 480);
            int i = 0;
            int x=0;
            File ff=new File("C:\\Users\\user\\Downloads\\A3.txt");
            while (true) {
                if (camera.read(frame)) {
                    Imgproc.resize(frame, frame, sz);
                    imag = frame.clone();
                    outerBox = new Mat(frame.size(), CvType.CV_8UC1);
                    Imgproc.cvtColor(frame, outerBox, Imgproc.COLOR_BGR2GRAY);
                    Imgproc.GaussianBlur(outerBox, outerBox, new Size(3, 3), 0);
                	
                   
                    if (i == 0) {
                        jframe.setSize(frame.width(), frame.height());
                        diff_frame = new Mat(outerBox.size(), CvType.CV_8UC1);
                        tempon_frame = new Mat(outerBox.size(), CvType.CV_8UC1);
                        diff_frame = outerBox.clone();
                    }
     
                    if (i == 1) {
                        Core.subtract(outerBox, tempon_frame, diff_frame);
                        Imgproc.adaptiveThreshold(diff_frame, diff_frame, 255,Imgproc.ADAPTIVE_THRESH_MEAN_C,Imgproc.THRESH_BINARY_INV, 5, 2);
                        array = detection_contours(diff_frame);
                        if (array.size() > 0) {
     
                            Iterator<Rect> it2 = array.iterator();
                            while (it2.hasNext()) {
                                Rect obj = it2.next();
                                Imgproc.rectangle(imag, obj.br(), obj.tl(),
                                        new Scalar(0, 255, 0), 1);
                                                          if(obj.contains(new Point(420,380))&&obj.contains(new Point(380,420))&&obj.contains(new Point(380,420))&&obj.contains(new Point(420,380))){
                                                            	x++;
                                                            Imgproc.putText(imag,String.valueOf(x),  new Point(200,200), Core.FONT_ITALIC, 5 ,new  Scalar(255,0,0),9);
                                                            
                                                        try {    
                                                            ff.createNewFile();
                                                            FileWriter ffw =new FileWriter(ff);
                                                           
                                                            ffw.write(String.valueOf(x));
                                                            ffw.close();}catch (Exception e) {}
                                                            }
                            }
                          }
                    }
     
                    i = 1;
     
                    ImageIcon image = new ImageIcon(Mat2bufferedImage(imag));
                    vidpanel.setIcon(image);
                    vidpanel.repaint();
                    tempon_frame = outerBox.clone();
     
                }
            }
        }
     
        public static BufferedImage Mat2bufferedImage(Mat image) {
            MatOfByte bytemat = new MatOfByte();
            Imgcodecs.imencode(".jpg", image, bytemat);
            byte[] bytes = bytemat.toArray();
            InputStream in = new ByteArrayInputStream(bytes);
            BufferedImage img = null;
            try {
                img = ImageIO.read(in);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return img;
        }        
        public static int y=0;
    
        public static ArrayList<Rect> detection_contours(Mat outmat) {
            Mat v = new Mat();
            Mat vv = outmat.clone();
            List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
            Imgproc.findContours(vv, contours, v, Imgproc.RETR_LIST,
                    Imgproc.CHAIN_APPROX_SIMPLE);
     
            double maxArea = 1000;
            int maxAreaIdx = -10;
            Rect r = null;
            ArrayList<Rect> rect_array = new ArrayList<Rect>();
            
            for (int idx = 0; idx < contours.size(); idx++) {
            	Mat contour = contours.get(idx);
            	double contourarea = Imgproc.contourArea(contour); 
            if (contourarea > maxArea) {
                     maxArea = contourarea;
                    maxAreaIdx = idx;
                    r = Imgproc.boundingRect(contours.get(maxAreaIdx));
                    rect_array.add(r);
                    Imgproc.drawContours(imag, contours, maxAreaIdx, new Scalar(0,0, 255));
        
             	  
            }
        
                  }
            
         
    
          		
          		Imgproc.rectangle(imag,new Point(350,450),new Point(450,350),
                        new Scalar(255, 0, 0), 3);
     Imgproc.line(imag, new Point(350,450),new Point(450,350), new Scalar(255, 255, 0));        
            
     
            v.release();
     
            return rect_array;
     
        }
    }

    • Partager sur Facebook
    • Partager sur Twitter
      11 août 2018 à 14:34:09 - Message modéré pour le motif suivant : Merci de créer votre propre sujet


      GOD ALONE HAS THE LAST WORD

      comptage des voitures en utilisant java et opencv

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown