Image Distance Measurement with only Camera using OpenCV
Object Detection
Friendly
1
, Harizahayu
1
and Rahmat Widya Sembiring
2
1
Computer Engineering, Politeknik Negeri Medan, Sumatera Utara, Indonesia
2
Informatica Management, Politeknik Negeri Medan, Sumatera Utara, Indonesia
Keywords: Image Processing, OpenCV, Machine Learning, Distance Measurement.
Abstract: Image processing combine with machine learning is used widely in image recognition, image classification,
and image detection. By using camera to detect an object has been done within several article by Guamán &
Naranjo and Shah S.P.. The other usage of image and object detection is in measurement of distance. Distance
measurement is possible by using additional peripheral such as sensor and extra camera. In previous research
by S.H.Chen, detecting an object by knowing focal length of camera and object using spatial dan disperse
technic can produce error percentage of max 5.72%, and deviation of 0.79. The distance used is in meter.
Thus the deviation is in 79cm. This research is intended to elaborate the usage of single camera in detecting
and measuring distance without using any sensor and can be used without knowing the focal length of a
camera. The distance to be measured is between the camera and the object. In this research, author used a
square object with different size and different distance range. By using one of the known data as a pivot in
calculating other image distance, the average error between 4%-7% . These result was achieved by using
different object size. The bigger the size of the object used as reference, the smaller the error percentage of
the measurement.
1 INTRODUCTION
Measurement is the act of determining a target's size,
length, weight, capacity, or other aspect. The distance
between an object is determined by using
measurement. Distance measurement can be done by
using ruler, sensor, and any other devices. In this
journal, author try to find a way to measure distance
by using only camera.
The usage of camera is more common in daily
live as camera is already part of gadget around us.
Camera can be found in almost everyday devices like
smartphones and laptop. Many research has been
done in order to determine the distance between
object and camera (Guamán & Naranjo, 2019;
Dirgantara, Rohman, & Yulianti, 2019) (Valocký,
Drahoš, & Haffner, 2020). One of the method is by
using object classification and machine learning. The
most common library for image classification is
OpenCv (Gupta, 2017).
In order to help people in extending the function
of a camera, this research try to formulate a
measurement and experiment the parameter and
formula to calculate the distance between a camera
and an object. By using only the camera dan method
propose, the camera can be used for measurement.
This research will used the image classification
and edge detection in detecting the distance between
camera and object. The library that is used are
OpenCv. This research is used in measuring short
distance between 20cm to 200cm.
2 RELATED WORKS
One of the research state how the modified camera
and with the help of sensor can enhanced distance
measurement (Valocký, Drahoš, & Haffner, 2020). In
research by Valocký, Drahoš, & Haffner, the distance
measurement is used for object measurement
detection in range between 110cm and 163cm with
average error of 41µm. The research is about
measuring distance between object and pattern.
While Shi-Huang Chen in his research (Chen &
Chen, 2011) used distance measurement using
camera with car license plate as object reference. In
Shi-Huang Chen research, the method used is the
method of triangular and founding the focal length of
774
Friendly, ., Harizahayu, . and Widya Sembiring, R.
Image Distance Measurement with only Camera using OpenCV Object Detection.
DOI: 10.5220/0010953300003260
In Proceedings of the 4th International Conference on Applied Science and Technology on Engineering Science (iCAST-ES 2021), pages 774-778
ISBN: 978-989-758-615-6; ISSN: 2975-8246
Copyright
c
2023 by SCITEPRESS Science and Technology Publications, Lda. Under CC license (CC BY-NC-ND 4.0)
camera. The distance measured are between 13m-
31m. The image recognition used are with image
capture in range of 5, 8, 11, 14, 17, 20, 23, 26, 29, 32,
35, 38, 41, 44, 47, and 5. The research data present in
Shi-Huang Cen article is present in the following
table.
Table 1: Recapitulation Of Distance Result Measurment
(Chen & Chen, 2011).
Real
Distance
Sparse Method Dense Method
Calculated
Distance
%
Erro
r
Devia
tion
Calculated
Distance
%
Erro
r
Devia
tion
13,8 14.59 5,72 0.79 13,81 0,07 0,01
20,6 21,39 3,83 0.79 20.,97 1,8 0,37
22,7 23,28 2,56 0.58 23 1,32 0,3
23,2 23,7 2,16 0,5 23,46 1,12 0,26
29,9 30,29 1,3 0,39 30,68 2,61 0,78
32,2 31,77 1,34 0,43 32,32 0,37 0,12
From the data presented, the measurement is set
in meter, thus the deviation are around 39 cm to 79
cm. The shortest the distance, the bigger the
deviation.
In other research to measure distance like
(Dirgantara, Rohman, & Yulianti, 2019) and (Gao,
Chen, Liu2, & Chen2, 2021) used the same method
by using references. In both paper state that by using
bottom part of a vehicle image, measuring the
distance between the two object can be achieved
though it state that it’s not accurate in a certain length.
As previous test result shown which is not clear, but
the average of error percentage are 1% and deviation
of 26 cm. Both research using mobile net and Yolo in
detecting image. While Jae Moon Lee (Lee, Hwang,
& Jung, 2021) in their research, the object detected by
using difference between 2 image with same object,
where there is a movement distance between 2 image.
2 RESEARCH DESIGN
Implementation of image detection is common
especially in image processing.
Figure 1: Image Capturing process in camera.
The process of measuring in this research is
following the common concept of image capturing in
camera lens. The image illustration is describe in
Figure 1.
This research will used 2 different object to
calculate the distance of the object. The first object is
16cm x 16 cm and 20cm x 20cm. The method propose
in this paper are to create a comparison between the
measured distance to get the ideal pivot used for
measurement.
Different from the process of captured image by
the camera, this research will approach by using the
concept of triangle. This deduction come from the
perspective of image we get from the capture image.
The further the distance of an object to camera, the
smaller the image captured. Figure 2 can describe the
process of image captured.
Figure 2: Image Capturing illustration.
The image shown how The further the distance of
an object to camera, the smaller the image result.
Despite the angel of the camera, the image proportion
will remain as long as the direction where the image
taken is equal.
To produce the formula, we must assume that the
angle at α in Figure 1 is equal both at real distance and
image distance. If we assume that the α is a formula
of tangent, then the formula can be described as:
tan(
α
) =
H
∶𝐷
=
h
∶ 𝐷
(1)
Thus
𝐷
=
𝐷
𝑥ℎ
𝐻
(2)
By using this formula, we can have a base
distance. As we follow the rules of triangle similarity,
as the image move further, so does the distance and
the image grow smaller. We can use one of the image
captured and with known distance as a reference. By
using this as a reference, we can derive the formula
by comparing the distance inside the reference image
(D
f
) and the height of the measured image captured.
The formula for calculated distance D
r
is calculated
using formula as follow:
Image Captured
H
D
Image Distance Measurement with only Camera using OpenCV Object Detection
775
𝐷
=
𝐻 𝑥 𝐷
(3)
To calculate the distance inside the image, this
research will be using this formula.
2.1 Algorithm
The algorithm used in testing the method in this
research will be using the opencv image classification
and then use an image as reference and calculated the
error percentage.
The process is started by collecting and arranging
the imaged into array. The image thus choose one by
one to be used as a reference. For each reference, the
formula will be calculated against other data. The
calculated distance is then compared to the image real
distance. The errors percentage and the deviation thus
calculated to produce the report.
The algorithm proposed is as describe in figure 3.
Figure 3: Algorithm Of Distance Measurement Testing.
3 EXPERIMENTAL RESULT
In this paper, the experiment is created by using
python programming language. The library used is
OpenCv. In experimenting the method, the object
used is determined and experiment used 2 object as
reference. The first one is a square paper with width
of 16cm x 16 cm and square paper with the width of
20cm x 20cm. The object’s distance thus measured
with ruler and the picture was taken by camera. The
distance is arranged in multiplication of 10. The
number of sample taken are 20 for each object. Some
image of the object taken shown in figure 3.
(a)
Image 1 taken from
10 cm distance
(b)
Image 1 taken from
100 cm distance
(c)
Image 2 taken from
10 cm distance
(d)
Image 2 taken from
100 cm distance
Figure 4: Image taken from different distance.
The process of calculating distance is done by
iterating the image inside the dataset. The process
include the image masking, image detection and
measuring the area of the reference object. Since the
object used is a square, the side of the object can be
calculated as a square root of the area. Thus the side
of the object can be calculated easily. After the
calculation of the sides of the object, the calculation
will continue to calculated the distance by comparing
the imaginary Distance and then reference distance
and will produce the real distance. Since the
information of the dataset is available, the process
continues to calculate the deviation and the error of
the measurement. The calculated data will be written
in the image and shown. The simulation is shown in
Figure 5.
Calculate the error percentage and
deviation
start
Collect and arrange
ima
g
e data in I
1
and I
2
Choose next image
as reference in I
1
Calculate the D
f
from
reference image
Calculate the Real Distance base
from reference image
Get next ima
g
e
Next image
available?
Finish
No more ima
g
e
Yes
No more
image
Yes
Next image
ava
il
ab
l
e?
iCAST-ES 2021 - International Conference on Applied Science and Technology on Engineering Science
776
Figure 5: The Process Of Simulation.
The test result of the simulation can be seen in
table 2 and table 3. Table 2 shown the testing result
of simulation by using object reference of square with
dimension of 16 cm x 16 cm. Table 3 shown the
testing result of simulation by using object reference
of square with dimension of 20 cm x 20 cm.
Table 2: Test Result Using Object Reference of Square
Object of 16 cmx 16 cm.
Do
Error
(
%
)
Deviation
in cm
Avg Max Min Avg Max Min
10 76.59 99.03 38.6 48.48 99.51 5.57
20 30.19 43.6 9.59 27.04 60.72 2.63
30 20.54 34.17 7.08 19.85 47.36 1.92
40 14.56 38.52 6.18 14.25 36.55 2.12
50 10.22 42.1 0.22 9.26 26.45 0.13
60 10.08 42.23 0.22 9.08 26.06 0.11
70 8.91 43.58 2.34 7.28 21.9 1.31
80 8 44.9 1.01 5.61 17.62 0.9
90 7.72 45.45 0.69 4.97 15.79 0.69
100 7.6 45.83 0.69 4.59 14.51 0.62
110 7.53 46.78 0.1 3.86 11.18 0.13
120 7.51 46.47 0.48 4.04 12.27 0.63
130 7.52 46.73 0.1 3.88 11.36 0.11
140 7.7 47.19 0.4 3.78 9.72 0.6
150 7.84 47.4 0.37 3.79 9.01 0.56
160 7.99 47.6 0.04 3.86 9.08 0.08
170 8.17 47.75 0.26 4.01 9.14 0.46
180 8.01 47.62 0.04 3.88 9.09 0.07
190 8.42 47.93 0.35 4.26 9.21 0.59
200 11.28 49.76 3.5 7.5 9.9 6.51
In order to eliminate unnecessary decrease int
error percentage and deviation, the test result using
reference image against the reference image itself is
remove since the test result always shown no
deviation and no error.
Table 3: Test Result Using Object Reference of Square
Object of 20 cmx 20 cm.
Do
Error (%) Deviation (in cm)
Av
g
Max Min Av
g
Max Min
20 27.66 39.52 12.07 25.49 54.19 3.23
30 14.99 24.5 8.59 15.77 36.6 2.41
40 7.48 17.82 2.19 8.29 22.57 1.07
50 5.82 19.58 1.53 6.3 18.69 0.87
60 4.05 22.21 0.08 3.69 13.37 0.06
70 4.06 22.15 0.05 3.73 13.5 0.05
70 4.04 22.33 0.02 3.63 13.12 0.02
80 4.27 21.65 0.33 4.12 14.48 0.33
90 4.83 20.8 0.76 4.96 16.16 0.75
100 4.42 21.39 0.33 4.35 14.99 0.26
110 4.04 22.35 0.02 3.62 13.09 0.01
120 4.07 22.1 0.05 3.75 13.58 0.04
130 4.1 22.63 0.36 3.55 12.52 0.26
140 4.28 23.16 0.07 3.5 11.44 0.11
150 4.3 23.21 0.07 3.5 11.33 0.1
160 4.36 23.3 0.12 3.53 11.14 0.18
170 8.5 28.33 1.61 7.12 11.22 2.94
180 7.17 27.16 0.06 5.54 8.46 0.11
190 7.12 27.11 0.02 5.49 8.36 0.04
200 7.11 27.1 0.02 5.48 8.33 0.04
From the result table can be seen that the deviation of
distance measurement is highest when the reference
is using smaller image reference. As the pixel size
tend to grow small, the accuracy is increased. In the
Table 2, the accuracy is the highest when using the
object reference of 120cm while using the image
larger in Table 3 shown that the accuracy getting
better result when the object reference is 110cm. The
deviation of measurement is decreasing as the object
reference is higher. This happen in both table.
Image Distance Measurement with only Camera using OpenCV Object Detection
777
4 CONCLUSIONS
By using comparison method and triangular
similarity, this research shown a great accuracy of
96% or 4.04% error. The deviation of measurement
achieved from this experiment is average of 3.02%.
Although some of the measurement result shown
small deviation but against other image captured
shown greater deviation. The irregular result in this
research show that the method and formula need to be
improve. But comparing to greater deviation in other
research, this method can be shown in less needed
accuracy measurement. By using the calculation
formula of:
𝐷
=
𝐻 𝑥 𝐷
(3)
We can calculate the distance between object only
using object references. Comparing this result with
other method in other research cited in this research,
which computing and detecting complex image,
measuring the distance by using determined object
can result to much better accuracy in short distance
and greater for further distance. The downside of this
method is that the object reference used must be
varied according to the distance as smaller object can
not be detected in further distance.
ACKNOWLEDGEMENTS
This work was supported and funded by Politeknik
Negeri Medan PDK Program for publication and
research.
REFERENCES
Chen, S.-H., & Chen, R.-S. (2011). Vision-Based Distance
Estimation for Multiple Vehicles Using Single Optical
Camera Feature. Second International Conference on
Innovations in Bio-inspired Computing and
Applications. Kaohsiung.
Cyganek, B. (2013). Object Detection And Recognition In
Digital Images. Chichester: John Wiley & Sons, Ltd.
Dirgantara, F. M., Rohman, A. S., & Yulianti, L. (2019).
Object Distance Measurement System Using
Monocular Camera on Vehicle. International
Conference on Electrical Engineering, Computer
Science and Informatics (EECSI 2019). Bandung.
Gao, W., Chen, Y., Liu2, Y., & Chen2, B. (2021). Distance
Measurement Method for Obstacles in front of Vehicles
Based on Monocular Vision. 2020 2nd International
Conference on Computer, Communications and
Mechatronics Engineering (CCME2020). Xiamen,
China: IOP.
Guamán, L. R., & Naranjo, J. E. (2019). Object detection in
rural roads through Single Shot Multibox Detector
Mobilenet network. Proceedings of 2019 the 9th
International Workshop on Computer Science and
Engineering. Hong Kong.
Gupta, B. &. (2017). Study on Object Detection using Open
CV - Python. International Journal of Computer
Applications, 17-21.
Lee, J. M., Hwang, K., & Jung, I. H. (2021). Real Distance
Measurement Using Object Detection of Artificial
Intelligence. Turkish Journal of Computer and
Mathematics Education Vol.12 No.6(2021), pp. 557-
563.
Sasaki, N., Tomaru, S., & Nakamura, S. (n.d.).
Development of Inter-Vehicle Distance Measurement
System using Camera-Equipped Portable Device. 2017
17th International Conference on Control, Automation
and Systems. Jeju, South Korea.
Shah, S. P. (2021). A Motion Detection System in Python
and Opencv. International Conference on Intelligent
Communication Technologies and Virtual Mobile
Networks (ICICV), pp. 1378-1382.
Valocký, F., Drahoš, P., & Haffner, O. (2020). Measure
distance between Camera and Object using Camera
Sensor. Cybernetics & Informatics (K&I), 1-4.
iCAST-ES 2021 - International Conference on Applied Science and Technology on Engineering Science
778