Click here to Skip to main content
13,896,761 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 15 Dec 2017
Licenced MIT

Analog Gauge Reader

, 15 Dec 2017
Rate this:
Please Sign up or sign in to vote.
This sample application takes an image or video frame of an analog gauge and reads the value using functions from the OpenCV* computer vision library.

Editorial Note

This article is for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers

This sample application takes an image or video frame of an analog gauge and reads the value using functions from the OpenCV* computer vision library. It consists of two parts: calibration and measurement. During calibration, the application calibrates an image of a gauge (provided by the user) by prompting the user to enter the range of gauge values in degrees. It then uses these calibrated values in the measurement stage to convert the angle of the dial into a meaningful value.

What You’ll Learn

  • Circle detection
  • Line detection

Gather Your Materials

  • Python* 2.7 or greater
  • OpenCV version 3.3.0 or greater
  • An image of a gauge (or you can use the sample one provided)


  1. Take a picture of a gauge or use the gauge-1.jpg provided. If you name it something other than gauge-1.jpg, make sure to change that in the main() function.
  2. Run the application (download the .zip at the end of this article first) and enter the requested values, using the output file gauge-#-calibration.jpg to determine the values. Here's an example of what the calibration image looks like:

    For the calibration image above, you would enter in the following values:

  3. The application by default reads the value of the gauge of the image you used for calibration. For the provided image, it gives a result of 16.4 psi. Not bad.

Original image:

Found line: (not normally an output, just to show more of what's going on).

gauge-2.jpg is provided for the user to try.

Get the Code

Code is included in this folder of the repository in the .py file.

How It Works

The main functions used from the OpenCV* library are HoughCircles (to detect the outline of the gauge and center point) and HoughLines (to detect the dial).

Basic filtering is done as follows: For circles (this happens in calibrate_gauge())

  • only return circles from HoughCircles that are within reasonable range of the image height (this assumes the gauge takes up most of the view)
  • average the resulting circles and use the average for the center point and radius For lines (this happens in get_current_value())
  • apply a threshold using cv2.threshold. and cv2.THRESH_BINARY_INV with threshold of 175 and maxValue of 255 work fine
  • remove all lines outside a given radius
  • check if a line is within an acceptable range of the radius
  • use the first acceptable line as the dial

There is a considerable amount of trigonometry involved to create the calibration image, mainly sine and cosine to plot the calibration image lines and arctangent to get the angle of the dial. This approach sets 0/360 to be the -y axis (if the image has a cartesian grid in the middle) and it goes clock-wise. There is a slight modification to make the 0/360 degrees be at the -y axis, by an addition (i+9) in the calculation of p_text[i][j]. Without this +9 the 0/360 point would be on the +x axis. So this implementation assumes the gauge is aligned in the image, but it can be adjusted by changing the value of 9 to something else.

IMPORTANT NOTICE: This software is sample software. It is not designed or intended for use in any medical, life-saving or life-sustaining systems, transportation systems, nuclear systems, or for any other mission-critical application in which the failure of the system could lead to critical injury or death. The software may not be fully tested and may contain bugs or errors; it may not be intended or suitable for commercial release. No regulatory approvals for the software have been obtained, and therefore software may not be certified for use in certain countries or environments.


This article, along with any associated source code and files, is licensed under The MIT License


About the Author

Intel Corporation
United States United States
You may know us for our processors. But we do so much more. Intel invents at the boundaries of technology to make amazing experiences possible for business and society, and for every person on Earth.

Harnessing the capability of the cloud, the ubiquity of the Internet of Things, the latest advances in memory and programmable solutions, and the promise of always-on 5G connectivity, Intel is disrupting industries and solving global challenges. Leading on policy, diversity, inclusion, education and sustainability, we create value for our stockholders, customers and society.
Group type: Organisation

43 members

You may also be interested in...

Comments and Discussions

Question[My vote of 2] CODE IN FOLDER ?? WHERE IS THE CODE EXAMPLE ? Pin
stixoffire29-Nov-18 0:36
memberstixoffire29-Nov-18 0:36 
QuestionExample Pin
Rumpilstilzkin3-Jan-18 7:38
memberRumpilstilzkin3-Jan-18 7:38 
AnswerRe: Example Pin
stixoffire29-Nov-18 0:41
memberstixoffire29-Nov-18 0:41 
QuestionWhere is the code Pin
Katghoti19-Dec-17 6:18
memberKatghoti19-Dec-17 6:18 
AnswerRe: Where is the code Pin
stixoffire29-Nov-18 0:41
memberstixoffire29-Nov-18 0:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190306.1 | Last Updated 15 Dec 2017
Article Copyright 2017 by Intel Corporation
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid