Aman Preet Gulati — Updated On May 11th, 2022
Advanced Computer Vision Maths Python

This article was published as a part of the Data Science Blogathon.

Overview of Angle detection

In this article, we will be making the angle detection application using computer vision we have often come across geometry in our high school and have struggled to draw the different angles using a protractor, though here also we will be doing the same thing this time with the help of computer vision we will draw three dots and after connecting them we will detect what degrees of the angle it will form.


Image Source: KnowledgeNile


Real-world application of Angle detection

  1. Mathematical operation: Angle detection is one of the problem statements to be solved.
  2. Digital classes: Modern classes can widely use this concept for a better visual experience.
  3. AI game development: When we want AI to automate the game.

Let’s build this 🙂

We will start by loading all the necessary libraries

import cv2
import math
import matplotlib.pyplot as plt

Providing a path to the file we want to evaluate and reading it

path_of_image = 'protractor.jpg'
image_angle = cv2.imread(path_of_image)
dots_list = []




  1. First of all, we are creating a variable to store the path of the image on which we will be drawing and detecting the angles.
  1. Then we are reading the image whose path we have just stored and with the help of CV2s imread function and store it in the variable to use for further preprocessing.
  1. Here we will have one more variable which will hold the coordinates of a dot that we will be creating to join those 2 dots ( we will discuss more it just in our next segment).

Function to draw the dot and join them to form a line

So, first, let’s discuss what would be the functionality of it:

  1. First of all, this function will be responsible for creating a dot on the image whenever we used to click the left button or double click and then by using dot function we will draw the dot on the cv2 image.
  1. Along with creating a dot on the image this function will be also joining the lines which will join the three dots i.e. it will be joining the dot with the help of line function.
def dots_of_mouse(mouse_click,x_cor,y_cor,flag_var,parameters):
    if mouse_click == cv2.EVENT_LBUTTONDOWN:
        size_of_dot = len(dots_list)
        if size_of_dot != 0 and size_of_dot % 3 != 0:


  1. First, let’s discuss the parameters that this function will mainly use:
    • mouse_click: This parameter holds what kind of mouse click is occurred whether it is the right button clicked or left button or any other that the mouse can produce.
    • x_cor: This is the X value of the dot.
    • y_cor: This is the Y value of the dot. Both x and y value is responsible to draw a single dot on the image.
  1. Our very first step will be to validate that the mouse click that has occurred and it should be a left button click then only further preprocessing work will be done.
  1. After the validation, we will see the size or the length of dots_list and then with the help of the if condition we will see that the length/size of the dot should neither be 0 nor be divisible by 3.
  1. Just after the above condition will be True then only we will draw the arrowed line on the image following the x value and the Y value.
  1. We can again draw the dot so that after one detection of angle from the previous dots we can do that again in the same instance also we will append the dots in our list of dots when you will print this variable you will see the exact value where the dot is drawn.

Defining gradient for Angle detection

Image Source: KhanAcademy


def finding_gradient(dot1,dot2):
    return (dot2[1]-dot1[1])/(dot2[0]-dot1[0])


So here we will be finding the gradient of the dots or in simple terms, we will find the slope of 2 dots.

  1. In this function, we will take dot 1 and dot 2 as the parameters to find the slope of those two dots.
  2. Then we will find the slope using the mathematical formula i.e. y2-y1/x2-x1 and then we will simply return the slope of the given dots.

The function that will calculate the angle

Image Source: wikiHow


Now, here comes the function which will do the main task for us i.e to evaluate the angle based on the 3 dots we will draw on the image. When we will see the parameters of the image it only has one parameter i.e. dots_list i.e this function will require only the coordinates of X and Y of those dots to get the angle as the result.

def evaluate_angle(dots_list):
    dot1, dot2, dot3 = dots_list[-3:]
    m1 = finding_gradient(dot1,dot2)
    m2 = finding_gradient(dot1,dot3)
    angleR = math.atan((m2-m1)/(1+(m2*m1)))
    angleD = round(math.degrees(angleR))


  1. Firstly we can see that we are initializing the position of dots separately i.e. assigning the values of dots_list to dot1, 2, and 3.
  1. Now we will store the slope of 2 dots in m1 and m2 respectively which finding_gradient the function will return.
  1. Here, we will use the math.atan function because we have to find the arc i.e. angle. Then we will use the formulae to find the angle when the slopes are already given and the formula is as follows:
    • tan inverse = m2-m1/(1+(m2xm1) where m1 and m2 are slopes of 2 dots.
  1. Then we will use the degree function from the math module to get the result in the form of degrees.
  1. At the last, we will simply use putText the method to show the value of the angle.

A function that will show the angle on the image

while True:

    if len(dots_list) % 3 == 0 and len(dots_list) !=0:
    if cv2.waitKey(1) & 0xFF == ord('q'):
        dots_list = []
        image_angle = cv2.imread(path_of_image)


  1. Everything in this function will be under the infinite while loop
  2. Then we will check that length of dots should be divisible by 3 and it should not be equal to 0.
  3. Then only we will call the evaluate angle function which will return the angle.
  4. We have used the setMouseCallback function to detect if the left button is clicked then we will call the function which will get the dots.
  5. At the last, we will destroy the windows when the ‘q’ key is pressed.


We saw in the output how we can draw 3 points and then we can get the angle that is formed. We can try with different images to test our function we just have to change the path of the image to check if other images are giving good results or not.


Here’s the repo link to this article. Hope you liked my article on Angle detection using Computer Vision. If you have any opinions or questions, then comment below.

Read on AV Blog about various predictions using Machine Learning.

About Me

Greeting to everyone, I’m currently working in TCS and previously, I worked as a Data Science Analyst in Zorba Consulting India. Along with full-time work, I’ve got an immense interest in the same field, i.e. Data Science, along with its other subsets of Artificial Intelligence such as Computer Vision, Machine Learning, and Deep learning; feel free to collaborate with me on any project on the domains mentioned above (LinkedIn).

The media shown in this article is not owned by Analytics Vidhya and are used at the Author’s discretion.

About the Author

Our Top Authors

Download Analytics Vidhya App for the Latest blog/Article

Leave a Reply Your email address will not be published. Required fields are marked *