The realization of vehicle detection and tracking based on OpenCV

Source: Internet
Author: User
Tags mixed
The realization of vehicle detection and tracking based on OpenCV

Http://www.cnblogs.com/yanneu/p/6251892.html

Recently, the teacher assigned a job, is to do a video-based vehicle detection and tracking, with about two weeks of time to do a simple, the effect is not ideal, but can not suppress some of their own knowledge to write down, here to put some blog on the network to organize a share to everyone, hope to help everyone, Because I am also a small white, so if there is anything to say the wrong place hope you crossing more correct.

First, install OPENCV and build environment

First of all, we have to install OPENCV, where there are many related tutorials on the network, here will not repeat. I am using the opencv3.10+vs2015.

Here are a few tutorials to be posted:

Download Address: http://opencv.org/downloads.html

Installation Tutorial: http://www.cnblogs.com/sopic/p/5265836.html (opencv3.0+vs2015)

http://blog.csdn.net/hustlx/article/details/50974336 (opencv3.10+vs2015)

There are also a lot of installation tutorials on other OPENCV versions, and there are just two of them posted here.

Ii. Introduction to OpenCV

I think the internet on this aspect of the good tutorial for everyone to post it, I am afraid to speak bad words recruit jokes.

The first is the series of blog of the light ink God, I basically learned from the blog of the Great God, and his "OpenCV3.0 Programming Primer" (strongly recommended. Very easy to understand. Online a search can be)

Light Ink OpenCV Introductory Tutorial

Of course, the OpenCV Chinese forum is also very good, but it feels like the content of the light ink great God

OPENCV Chinese Forum Tutorial

Third, the system structure design

Flow chart:

There may be some difficult questions to understand, as well as a few addresses for everyone to learn:

Mixed Gaussian modeling

Backgroundsubtractormog and BackgroundSubtractorMOG2

The source code is as follows:

#include <SDKDDKVer.h> #include <stdio.h> #include <tchar.h> #include <iostream> #include <
Opencv2\opencv.hpp> #include <opencv2\video\background_segm.hpp> using namespace CV;

using namespace Std; The contour is sorted by area descending, the purpose is to remove those small outline target bool Descsort (vector<point> p1, vector<point> p2) {return Contourarea (p1) >
Contourarea (p2);
    } int main () {//read in video videocapture capture ("e:\\ temporary \\workspace\\1.avi");
    Defines a mat variable that is used to store the image of each frame mat frame;
    Foreground Mat mask;
    Connected component Mat Srcimage;

    Results Mat result;
    Training background image with mixed Gaussian model ptr<backgroundsubtractormog2> bgsubtractor = createBackgroundSubtractorMOG2 ();

    Bgsubtractor->setvarthreshold (20);
    for (int k = 0; k < k++)//{/////Read current frame//capture >> frame;
    If the video playback is complete, exit the loop//if (Frame.empty ())//{//break;
    }//Bgsubtractor->apply (frame, mask, 0.2); }//imshow ("ForegroundTraining result ", mask);
        Loop displays each frame while (true) {//reads the current frame capture >> frame;
        If the video playback is complete, exit the loop if (Frame.empty ()) {break;
        } frame.copyto (Result);
        Cvtcolor (frame, frame, color_gray2bgr);

        Bgsubtractor->apply (frame, mask, 0.2);  Imshow ("original video", frame);  Displays the current frame//waitkey (30);
        Delay 30ms Imshow ("Mixed Gaussian Modeling", mask);

        Waitkey (30);
        Cvtcolor (Mask, mask, COLOR_GRAY2BGR);
        The median filter is first applied to the foreground, then the morphological expansion operation is performed to remove the pseudo target and the small target medianblur (mask, Mask, 5).

        Morphologyex (Mask, Mask, morph_dilate, Getstructuringelement (Morph_rect, Size (5, 5)));
        Test: First open operation again closed Operation Morphologyex (mask, Mask, Morph_close, Getstructuringelement (Morph_rect, Size (5, 5));
        
        Morphologyex (Mask, Mask, Morph_open, Getstructuringelement (Morph_rect, Size (5, 5)));
        Imshow ("Mixed Gaussian model", mask);

        Waitkey (30); Copy Mask.copyto (srcimage); 
        The size of the contour//outer vector of each unicom component represents the number of outlines in the image, and the vector size represents the number of points on the contour vector<vector<point>> contours;

        Get only the outer contour, get each pixel of each contour, and two pixels adjacent to the location difference not more than 1 findcontours (srcimage, contours, retr_external, chain_approx_none);

        Test Contour Acquisition Imshow ("Contour Acquisition", Srcimage);
        if (Contours.size () < 1) continue;

        External Matrix Rect RCT;

        The contour is sorted by area in descending order before the contour is an external matrix to remove the small target (pseudo-target) sort (Contours.begin (), Contours.end (), descsort); for (int i = 0; i < contours.size (); i++) {//when the external matrix area of the I-connected component is less than 1/6 of the maximum area, it is considered to be a pseudo-target if (con
            Tourarea (Contours[i]) < Contourarea (Contours[0])/5) break;
            Minimum matrix containing the contour RCT = Boundingrect (Contours[i]);

        Rectangle (result, RCT, Scalar (0, 255, 0), 2);
    } imshow ("results", result);
    } getchar ();
return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.