Reader small image

You're reading from  Qt 5 and OpenCV 4 Computer Vision Projects

Product typeBook
Published inJun 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789532586
Edition1st Edition
Languages
Right arrow
Author (1)
Zhuo Qingliang
Zhuo Qingliang
author image
Zhuo Qingliang

Zhuo Qingliang (a.k.a. KDr2 online) is presently working at Beijing Paoding Technology Co. LTD., a start-up Fintech company in China that is dedicated to improving the financial industry by using artificial intelligence technologies. He has over 10 years experience in Linux, C, C++, Python, Perl, and Java development. He is interested in programming, doing consulting work, participating in and contributing to the open source community (of course, includes the Julia community).
Read more about Zhuo Qingliang

Right arrow

Real-Time Car Detection and Distance Measurement

In the previous chapter, we learned how to detect objects using the OpenCV library, both via the cascade classifiers approach and the deep learning approach. In this chapter, we will discuss how to measure the distance between the detected objects or between the object of interest and our camera. We will detect cars in a new application and measure the distance between cars and the distance between a car and the camera.

The following topics will be covered in this chapter:

  • Detecting cars using the YOLOv3 model with OpenCV
  • Methods to measure distance in different view angles
  • Measuring the distance between cars in the bird's eye view
  • Measuring the distance between a car and the camera in the eye-level view

Technical requirements

Like the previous chapters, you need to have Qt version 5 at a minimum and OpenCV 4.0.0 installed. Having basic knowledge of C++ and Qt programming is also a requirement.

We will use a deep learning model, YOLOv3, to detect cars, so having knowledge of deep learning will also be a big help. Since we introduced deep learning models in Chapter 6, Object Detection in Real Time, reading that chapter prior to this chapter is recommended.

All of the code for this chapter can be found in this book's code repository at https://github.com/PacktPublishing/Qt-5-and-OpenCV-4-Computer-Vision-Projects/tree/master/Chapter-07.

Check out the following video to see the code in action: http://bit.ly/2FdC0VF

Car detection in real time

Before measuring the distance between objects, we must detect the objects of interest to find out where they are. In this chapter, we have decided to measure the distance between cars, so we should start by detecting cars. In the preceding chapter, Chapter 6, Object Detection in Real Time, we learned how to detect objects in many ways, we saw that the YOLOv3 model has good performance in terms of accuracy, and fortunately, the car object class is in the category list of the coco dataset (that is, the coco.names file). Therefore, we will follow that method and use the YOLOv3 model to detect cars.

As we did in the previous chapters, we will create the new project of this chapter by copying one of the projects we have already finished. This time, let's copy the Detective application that we completed in the previous chapter as the new project for this...

Distance measurement

There are many ways to measure or estimate the distance between the objects or between the object and the camera in different situations. For example, if our objects of interest or our camera are moving in a known and fixed velocity, with the motion detection and object detection technology, we can estimate the distance between the objects in the view of the camera easily. Alternatively, if we get our hands on a stereo camera, we can follow https://www.ijert.org/research/distance-measurement-system-using-binocular-stereo-vision-approach-IJERTV2IS121134.pdf to measure the distance.

However, for our situation, we only have a common webcam in a fixed position, so how could we measure the distance from it? Well, it's possible to do with some prerequisites.

Let's talk about measuring the distance between the objects first. The prerequisites in this situation...

Measuring the distance between cars or between the car and the camera

With the principles we talked about in the preceding section at hand, let's utilize them in order to measure distances in our application.

As we mentioned previously, we will measure from two different views. First, let's look at the bird's eye view.

Measuring the distance between cars in a bird's eye view

To be able to see cars from a bird's eye view, I fixed my camera to a window in my office, which is on the eighth floor, and let it face the ground. Here's one of the pictures I got from my camera:

You can see that the cars on the road are running from the left of the picture to the right. It is not an absolute bird&apos...

Summary

In this chapter, we planned to measure the distance between cars or between a car and a camera using OpenCV. First, we created a new application named DiGauge to detect cars from the camera by abridging the Detective application we developed in the previous chapter. Then, we talked about the principles of distance measuring in the computer vision domain in two view modes—the bird's eye view and the eye-level view. After that, we implemented the distance measurement features in these two view modes in our application and added a menu on the UI to switch between the two view modes.

In the next chapter, we will introduce a new technology called OpenGL and see how we can use it in Qt and how it can help us in the computer vision domain.

Questions

Try to answer the following question to test your knowledge of this chapter:

  1. Is there a better reference object that we can use when we're measuring the distance between cars?
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Qt 5 and OpenCV 4 Computer Vision Projects
Published in: Jun 2019Publisher: PacktISBN-13: 9781789532586
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Zhuo Qingliang

Zhuo Qingliang (a.k.a. KDr2 online) is presently working at Beijing Paoding Technology Co. LTD., a start-up Fintech company in China that is dedicated to improving the financial industry by using artificial intelligence technologies. He has over 10 years experience in Linux, C, C++, Python, Perl, and Java development. He is interested in programming, doing consulting work, participating in and contributing to the open source community (of course, includes the Julia community).
Read more about Zhuo Qingliang