This article was published as a part of the Data Science Blogathon.
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.
Let’s build this 🙂
import cv2 import math import matplotlib.pyplot as plt
path_of_image = 'protractor.jpg' image_angle = cv2.imread(path_of_image) plt.imshow(image_angle) dots_list = []
Output:
Code-breakdown
imread function
and store it in the variable to use for further preprocessing.So, first, let’s discuss what would be the functionality of it:
left button
or double click
and then by using dot function
we will draw the dot on the cv2 image.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: cv2.arrowedLine(image_angle,tuple(dots_list[round((size_of_dot-1)/3)*3]),(x_cor,y_cor),(0,0,255),2) cv2.circle(image_angle,(x_cor,y_cor),5,(0,0,255),cv2.FILLED) dots_list.append([x_cor,y_cor])
Code-breakdown:
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.arrowed line
on the image following the x value and the Y value.
def finding_gradient(dot1,dot2): return (dot2[1]-dot1[1])/(dot2[0]-dot1[0])
Code-breakdown
So here we will be finding the gradient of the dots or in simple terms, we will find the slope
of 2 dots.
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)) cv2.putText(image_angle,str(angleD),(dot1[0]-40,dot1[1]-20),cv2.FONT_HERSHEY_COMPLEX, 1.5,(0,0,255),2)
Code-breakdown
dots_list
to dot1, 2, and 3.finding_gradient
the function will return.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:
degree
function from the math module to get the result in the form of degrees.putText
the method to show the value of the angle.while True: if len(dots_list) % 3 == 0 and len(dots_list) !=0: evaluate_angle(dots_list) cv2.imshow('Image',image_angle) cv2.setMouseCallback('Image',dots_of_mouse) if cv2.waitKey(1) & 0xFF == ord('q'): dots_list = [] image_angle = cv2.imread(path_of_image)
Code-breakdown:
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.
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.