Docsity

Stereo Vision using two webcameras, Thesis of Computer Vision

Partial preview of the text

Download Stereo Vision using two webcameras and more Thesis Computer Vision in PDF only on Docsity!

Frédéric Uhrweiler Stéphane Vujasinovic

Author: Uhrweiller Frédéric and Vujasinovic Stéphane Matrikelnr.58566 EIT Matrikelnr .: 59092 Mechatronics

E-mail: [email protected] E-mail: [email protected]

Assistant Prof. Dr.-Ing. Ferdinand Olawsky

Project Coordinator MMT: Prof. Dr. Peter Weber

Project code: 17SS_OL_Remember

Deadline: 30.09.

2 Frédéric Uhrweiler Stéphane VujasinovicImprint Directory

Table of Contents

  1. Introduction .......................... .................................................. .................................................. ...............
  1. Conclusions ................... ................................................ .................................................. ........... 29

A. Summary .................................................. ....................................... 29

B. Conclusion .................................................. .................................................. ................................ 29

C. Outlook .............. .................................................. .................................................. ....................... 29

  1. Appendix ....................... .................................................. .................................................. .................... 30

Abbildungsverzeichnis

Figure 1: scheme to stereo camera ...................... .................................................. ........................... 5 Figure 2: OpenCV and Python Logo ............... .................................................. ................................. 5 Figure 3: Photo of the Logi Tech Webcam C170 (Source: www.logitech.fr) ..................................... 6 Figure 4: Picture of our self-built stereo camera ....................................... .............................. 6 Figure 5: Camera Operation ................. .................................................. ......................... 7 Figure 6: Configured Object ................... .................................................. 8 Figure 7: Radial Distortion (Source: Wikipedia). .................................................. ........................... 8 Figure 8. Tangential Distortion (Source: Learning OpenCV 3 - O'Reilly) ...... ... 9 Figure 9: Photo taken during calibration images ... ....................................... 9 Figure 10: Triangulation (Source: Learning OpenCV 3 - O'Reilly) ............................................. ....... 11 Figure 11: Genesis the corners of a chessboard ................... 18 Figure 12: Principle of epipolar lines ........................ .................................................. ..................... 20 Figure 13: Without Calibration ......................... .................................................. ................................. 20 Figure 14: Calibration .................................................. ... 20 pictures 15: Example of a stereo camera watching a scene ......................................... ..... 21 Figure 16: Compliance of Blocking with StereoSGBM .......... 21 Figure 17: Identification of Self Blocking with StereoSGBM .............................. ............................ 22 Figure 18: Example of the Five Directions of the StereoSGBM Algorithm at OpenCV ........ ...... 22 Figure 19: Result for the disparity card .............................................. Figure 20: Example of a Closing Filter. .................................................. ............................. 23 Figure 21: Result of a Disparity Card after a Closing Filter ........ ............................... 24 Figure 22: The normal scene seen from the left camera without rectification and calibration. 24 Figure 23: Disparity Card from the Upper Scene without Closing Filter and with ............................. 24 Figure 24: Ocean ColorMap .. .................................................. .................................................. ...... 25 Figure 25: WLS Filters on a Disparity Map with an Ocean Map Color and Without ............... 26 Figure 26: Calculating the Distance with the WLS Filter and the Disparity card ........................... 26 Figure 27: Experimental measurement of the disparity t as a function of the distance to the object. 27 Figure 28: Possible improvement with the first suggestion ............................. ........................... 28

Code Directory

Code 1: Package Importation .......... .................................................. .................................................. .. 15 Code 2: Activating cameras with OpenCV ....................................... ............................................. 15 Code 3: Image processing ............................................. .................................................. .................... 15 Code 4: Show pictures ........................ .................................................. Code 5 : Typical exit for a program .................................................. 16 Code 6: Calibration of Distortion in Python ......... .................................................. ........................... 19 Code 7: Show Disparity Map ................ .................................................. .................................... 22 Code 8: Parameters for the instance of StereoSGBM. .................................................. .................... 23 Code 9: Parameters for an WLS Filter ..................... .................................................. ........................... 25 Code 10: Creating a WLS Filter in Python ............ .................................................. ............. 25 Code 11: Inserting the WLS Filter in Python ........................... .................................................. ......... 25 Code 12: The Regression Formula in "Main_Stereo_Vision_Prog.py" ............................ ........................ 27

The stereo camera consists of two Logitech Webcam C170.

Figure 3: Photo from the Logitech Webcam C170 (Source: www.logitech.fr)

Video calls are optimal with images of 640x480 pixels. Focal length: 2.3mm. The self-built stereo camera:-built stereo camera

Figure 4: Photo of our self

2. Camera model

cameras capture the light rays of our environment. In principle, a camera like our eye works, the reflected light beams of our environment come in our eyes and you are collected on our retina. The "Pinhole Camera" is the simplest model. It's a good simplified model to understand how a camera works. In the model, all light beams are stopped by one surface. Only the beams that penetrate through the hole are configured reversely on a surface in the camera. The bottom picture illustrates this principle.

Figure 5: Functioning of a Camera

Source: https://funsizephysics.com/use-light-turn-world-upside/

This principle is very simple, but it is not a good way to get enough light at a fast Exposure. Therefore, lenses are used to collect more light beams at one point. The problem is that this lens brings distortion. One can distinguish two different types of distortion:

  • radial distortion
    • tangential distortion The radial distortion comes from the lens itself and the tangential distortion comes from the camera's geometry. The images can then be corrected using mathematical procedures. The calibration process allows a model of the camera's geometry and a model of the lens distortion. These models form the intrinsic parameters of a camera.

A. Focal length

The relative size of the image projected on the surface of the camera depends on the focal length. The pinhole model is the focal length, the distance between the hole and the area where the image is projected.

The Thales Theorem then gives: -x = f * (X / Z) With:

  • x: image of the object (minus sign is reversed by the image)
  • X: object size
  • Z: distance from hole to object
  • f : Focal length, distance from hole to image

x and y are the coordinates of the original point on the image area and thus the position of the corrected point is calculated.

There is also a tangential distortion because the lens is not perfectly parallel to the image surface. To correct this, two additional parameters are introduced, p1 and p2.

C. Calibration with OpenCV

library allows us to calculate the intrinsic parameters using specific functions, which is called calibration. This is possible with various checks of a chessboard.

Figure 9: Photo taken during calibration

The program for recording images for later calibration is called "Take_images_for_calibration.py". When the corners of the chessboard are detected on both cameras, two windows with the recognized image will open for each Camera. The images are either saved or deleted by the user. Good pictures can be seen in which the corners are clearly recognizable. These images are used later for the calibration in the main program "Main_Stereo_Vision_Prog.py". OpenCV recommends at least 10 pictures for each camera to get a good calibration. We've gotten good results with 50 pictures for each camera. To calibrate the cameras, the Python Code searches the corners of the chess board on each image for each camera using the OpenCV function: cv2.findChesssboardCorners

The position of the corners for each image is then stored in an image vector and the 3d scene object points are another vector. These imgpoints and objpoints in the cv2.calibeCamera () function then use the camera matrix, the distortion coefficients, the rotation and translation vectors at the output.

Figure 8. Tangential Distortion (Source: Learning OpenCV 3 - O'Reilly)

The cv2.getOptimalNewCameraMatrix () function enables us to get precise camera matrices that we later use in the cv2.stereoRectify () function.

After the calibration with OpenCV we get the following matrix M for our camera:

Matrix M without rectification (right camera):

matrix M

directly

Rectified (right camera):

303,497 0 869,592 233,909 0 0

871,

01

matrix M without rectification (left camera):

M matrix

directly

Rectified (left camera):

Stereoabbildung

A Declaration

the stereo vision allows the depth in to recognize an image, to make measurements in the image and to make 3D localizations. For this purpose, points that match the two cameras must be found. From this, you can then divert the distance between the camera and the point. The system's geometry helps simplify the calculation.

Before we explain the method for the mathematical orientation of the images, we first need to understand the epipolar geometry.

C. Epipolar Geometry

The above image shows us the model of a non-perfect stereo camera consisting of two Pinhole camera models. By crossing the line of the projecting centers (Ol, Or) with the leveling projections, the epipolar points are created el and he. The lines (pl, el) and (pr, er) are called epipolarlines. The image of all possible points of a point at a projecting level is the epipolar line lying on the other image plane and passing through the epipolar point and the desired point. This allows you to limit the search of the point to a single dimension rather than on a whole level. You can therefore summarize the following points:

  • Each 3D point in the view of a camera is contained in the epipolar plan
  • A feature in one plane must be on the corresponding epipolar lines of the other plane (epipol condition)
  • A two-dimensional search of a corresponding feature becomes a One-dimensional search is converted when you know the epipoloadometry.
  • The sequence of the points is kept, ie, two points A and B are found in the same order on the epipolar lines of a plane as in the other plane.

D. Essential and Fundamental matrices.

To understand how the epipolar lines are calculated, one must first explain the essential and fundamental matrices (according to Matrize E and F). The Essential Matrix E contains the information on how the two cameras are physically arranged. It describes the location of the second camera relative to the first camera by translation and rotation parameters. These parameters are not directly readable in the matrix, as they are used for configuration. In the section Stereo Calibration we explain how to calculate R and T (rotation matrix and translation vector). Matrix F contains the information of the essential matrix E, the physical arrangement of the cameras and the information about the intrinsic parameters of the cameras.

The relation between the configured point on the left picture pl and the right image pr is defined as:

TEp p

r

l = 0

You might think that this formula is the link between the left and the right point completely describes. However, one must note that the 3x3 matrix E is of rank 2. That is, this formula is the equation of a straight line.

In order to fully define the relationship between points, you must therefore take into account the intrinsic parameters.

We remember the q = Mp, with the intrinsic matrix M by replacing in the previous equation yields: q

r

T (M l -1) TEM -1Q l l = 0

Man replaced: F = (M l

-

And Thus, we obtain: q

r

-1) TEM l

TFq l = 0

E. Rotationand translation

matrixvector Now that we have explained the essential matrix E and the fundamental matrix F, we have to see how the rotation matrix and the translation vector are calculated.

We define the following notations:

  • P l

defines the positions of the point in the coordinate system of the left or right cameras

  • R l

and P

r

) define the rotation and translation of the camera to the point in the environment for the left (or right) camera.

  • R and T are the rotation and the translation that brings the right camera's coordinate system into the left camera camera system.

It arise: Pl

and Tl

14 Frédéric Uhrweiler, Stéphane Vujasinovic,

and accurately align the pixel rows, thereby making the epipolar lines horizontal to find the correspondence of a point in the two images more likely As a result of the alignment of both images, there are 8 terms, 4 for each camera:

  • a distortion vector
  • a rotation matrix R

rect

that needs to be applied to the image

  • a rectified camera matrix M

rect

  • a non- Rectified camera matrix M

OpenC V allows us to compute these terms with two algorithms: the Hartley algorithm and the bouguet algorithm.

  1. Hartley Algorithm The Hartley algorithm will find the same points in the two images. He tries to minimize the disparities and find homographs that put epipoles in infinite. This method does not require the intrinsic parameters for each camera to be calculated.

An advantage of this method is that calibration is possible only thanks to the observation of points in the scene. But a great disadvantage is that you have no scaling of the image, you only have the relative distance information. You can not measure exactly how far an object is removed from the cameras.

  1. Bouguet Algorithm The Bouguet algorithm uses the calculated rotation matrix and the Translation vector to rotate two projected levels around half a turn so they are in the same plane. Thus, the main beams are parallel and the planets Koplanar are not yet aligned. This will be done later. In the project we have used the Bouguet algorithm.

4. Operation of stereo images

As already mentioned, the program is encoded in Python and the OpenCV library is being used. How have we decided for the Python language and the OpenCV library because we have already had experience and because there is a lot of documentation about it. Another argument for this decision is that we just wanted to work with "Open Source" libraries.

Two Python programs were developed for this project.

The first "Take_images_for_calibration.py" is used to capture good pictures later in calibration of the two cameras (distortion calibration and stereo calibration).

The second program and thus the main program "Main_Stereo_Vision_Prog.py" is used for the stereo image. We calibrate in this program the cameras with the

images that were recorded, generate a disparity card and thanks to a straightforward experiment, we can find the measuring distance for each pixel. An WLS filter is used to detect the edges of objects better.

The Python programs are listed in the appendix.

A. Used Packages The

following packages were imported into the program:

  • The version of OpenCV.3.2.0 with opencv_contrib (includes the stereo functions) called "cv2" in Python contains:

o the image processing library o the stereo vision features

  • Numpy.1.12.

o Used for matrices (images consist of matrices)

  • Workbook of openpyxl

o Package to write data in an Excel file

  • "normalize" the library sklearn 0.18.

o sklearn enables machine learning but in this project you

only use the WLS Filter

Code 1: Package Importation

B. Video Loop

In order to work with the cameras you must first activate it. The cv2.VideoCapture () function activates both cameras by entering the number of ports from each camera (two objects are generated in the program that can use the methods of the class cv2.VideoCapture ().

Code 2: Activating cameras with OpenCV

To get an image from the cameras, the cv2.VideoCapture (). Read () method is used as an output to get an image of the scene that the camera looks at when calling this function. In order to get a video you have to call this method repeatedly in an infinite loop. To be more efficient, it is recommended to convert the BGR images into Gray images, which is performed using the cv2.cvtColor () function.

Code 3: Image Processing

C. Funktionsweise vom Programm

“Take_images_for_calibration.py”

Wenn dieses Programm gestartet wird, werden beide Kameras aktiv und zwei Fenster werden geöffnet damit der Benutzer sehen kann wo das Schachbrett in den Bildern positioniert ist.

Diagramm 1: Funktionsweise von "Take_images_for_calibration.py"

  1. Vektoren für die Kalibrierung Die Funktion cv2.findChessboardCorners() wird eine definierte Anzahl von Schachbrett-Ecken suchen und es werden folgende Vektoren erzeugt:
  • imgpointsR: enthält die Koordinate der Ecken im Rechtem Bild (im Bildraum)
  • imgpointsL: enthält die Koordinate der Ecken im Linkem Bild (im Bildraum)
  • objpoints: enthält die Koordinate der Ecken im Objektraum

Die Präzision der Koordinate der gefundenen Ecken wird erhöht in dem die Funktion cv2.cornerSubPix() eingesetzt wird.

  1. Erfassung der Bilder für die Kalibrierung Hat das Programm die Position der Schachbrett-Ecken auf beide Bilder erkannt, werden zwei neue Fenster geöffnet wo man die aufgenommenen Bilder bewerten kann. Wenn die Bilder nicht verschwommen sind und gut aussehen muss man auf die “s” (Save) Taste drücken um die Bildern zu speichern. Wenn das Gegenteil der Fall ist kann man auf die “c” (Cancel) Taste

drücken damit die Bilder nicht gespeichert werden. Mit der Funktion cv2.drawChessboardCorners() überlagert das Programm ein Schachbrett-Muster auf die Bilder.

Abbildung 11: Erkennung der Ecken von einem Schachbrett