In the previous segments, you learned the basics of OpenCV. Now, you’ll learn to apply to apply those operations for detecting an object, in this case, a vehicle in a video.
To detect a moving vehicle, we need at least two consecutive frames. The following video will make this more clear. Here, we will work with only the first two frames of the video (the name of the video is AnirudhaBridge ). Later, we can extend these operations to the entire video, which is essentially a collection of frames. You can download the video used in the following lecture here.
You can download the notebook from the link provided above. In the above video, you saw that we can hardly recognize any change in these two frames with bare eyes. That’s because, generally, a video has a frame rate of 24 to 30, so it become hard to detect any substantial change in any two consecutive frames. This task may be difficult for human eyes but taking the difference between consecutive frames clearly captures the motion. Let’s continue with the lecture.
By taking the difference, you observed the change only in the portion where there was motion. Other stationary objects such as trees, houses will cancel out in the subsequent frames.
The output showing the outline of the moving vehicles is faint. Making the boundary sharp will increase the probability of detecting the vehicle. So, thresholding is done, Still, the outline of the vehicle is broken. As you know, contours are used to extract objects having connected points, therefore we try to fill gaps in outline with dilation and erosion for better identification of the boundary of the vehicle. You can extract and plot the individual contours/vehicles.
Even after dilation and erosion, you can still see that there are black dots between or in the outline of the vehicle. Remember from the previous segment that doing heavy morphological transformation can degrade the text shape. Similarly, in this case, doing heavy morphological transformation can degrade the shape of the vehicle and can also decrease the distance between 2 vehicles, making difficult to extract individual contours.
Next, we will fill gaps in vehicles by converting the contours to hulls.
In the image below, the grey colour shows the contour and the red colour shows the convex hull. The convex hull is the smallest convex set that contains all the points of contour.
Now that you know how to identify the moving objects, let’s proceed to the next segment.
Report an error