0% found this document useful (0 votes)
61 views9 pages

Elc Report

Uploaded by

vaibhavnayyar04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views9 pages

Elc Report

Uploaded by

vaibhavnayyar04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Geometrical Shape

Detection and
Recognition using
Python in Image
Processing
An ELC activity report submitted in partial fulfilment of the requirements
for the degree of

Bachelor of Engineering
in

Electronics and Computer Engineering/


Electronics and Communication Engineering /
Electronics Engineering (VLSI Design and
Technology
Submitted by

Name-> Vaibhav Nayyar

Roll Number-> 102306099

DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING


THAPAR INSTITUTE OF ENGINEERING & TECHNOLOGY, PATIALA

February-April 2024
Objective-> The objective of this project is to develop a Python
program that can identify and classify basic geometric shapes such
as circles, squares, triangles, and rectangles within an image.
(Geometrical Shape Detection and Recognition using Python
in Image Processing.)
Introduction->
Geometrical shape detection and recognition represent a
fundamental aspect of computer vision and image processing, with
wide-ranging applications across various industries and fields. From
automated quality control in manufacturing to object detection in
robotics and autonomous vehicles, the ability to identify and classify
geometric shapes within images is indispensable.
The significance of geometrical shape detection lies in its utility for
understanding and interpreting visual data. By analysing the spatial
arrangement of pixels in an image, algorithms can infer the
presence of distinct geometric structures, enabling automated
systems to make informed decisions based on visual inputs. This
capability finds applications in numerous domains, including
industrial automation, medical imaging, surveillance, and
augmented reality.
This project aims to delve into this crucial aspect of image
processing by developing a Python program capable of detecting
and recognizing basic geometric shapes, such as circles,
squares, triangles, and rectangles, within a given image.
The process of shape detection and recognition typically involves
several key steps. Initially, the input image is pre-processed to
enhance relevant features and reduce noise. This preprocessing
may include operations such as grayscale conversion,
smoothing, and thresholding. Subsequently, contours are
extracted from the processed image, representing the boundaries of
distinct objects or shapes present in the scene. These contours are
then analysed and classified based on their geometric properties,
such as the number of vertices, angles, and aspect ratios.
In this project, we will utilize the OpenCV (Open-Source Computer
Vision) library, a powerful tool for image processing and computer
vision tasks in Python, to implement the shape detection and
recognition algorithm. OpenCV provides a rich set of functions and
algorithms for tasks such as image filtering, edge detection, contour
detection, and shape analysis, making it well-suited for our
purposes.
Through this endeavour, we aim to not only develop a functional
shape detection system but also gain insights into the underlying
principles of image processing and computer vision. By
understanding the techniques and algorithms involved, we can
appreciate the intricacies of visual perception and its computational
realization, paving the way for further exploration and innovation in
this fascinating field.

EXAMPLE OF APPLICATION OF SHAPE DETECTION AND


IMAGE PROCESSING.
Flow Chart->

Insertion of
Convert To GRAY
Libraries and Image resize Threshold
SCALE
image.

Set Different
Find and draw Connecntion of Dilation on
kernel matrices for
Contours over disjoining edges in threshold image
morphologocal
image the shape. output.
operations

Name the shape Output

Python Code->
Visit the following link to access the code-
https://colab.research.google.com/drive/
1tDsN_LDg9Eg8A4UqlMj7GQOe-w7nIvsg?usp=drive_link
The same code is also pasted in blocks as follows-
import cv2 #iporting computer vision libraries
import PIL #python imaging library
import numpy as np #numerical operations
from matplotlib import pyplot as plt
from PIL import Image #Opening, rotating and displaying an image
from matplotlib import image as mpimg #Image read and Image show function

img=Image.open("9.jpeg")
plt.imshow(img)

print(img.format)
print(img.size)
print(img.mode)
img=np.array(img)
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

plt.imshow(gray,cmap='gray')
_,threshold=cv2.threshold(gray,110,255,cv2.THRESH_BINARY_INV)

plt.imshow(threshold,cmap='gray')

#Different kernel matrices for morphologocal operations.


kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(4,4))
kernelo=cv2.getStructuringElement(cv2.MORPH_RECT,(6,6))
kernelc=cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))

#Dilation on threshold image output.


dilated=cv2.dilate(threshold,kernel,iterations=3)
plt.imshow(dilated,cmap='gray')

#connecntion of disjoining edges in the shape.


closed=cv2.morphologyEx(dilated,cv2.MORPH_CLOSE,kernelc,iterations=3)
plt.imshow(closed,cmap='gray')

opening=cv2.morphologyEx(dilated,cv2.MORPH_OPEN,kernelc,iterations=8)
plt.imshow(opening,cmap='gray')

thinned = cv2.ximgproc.thinning(opening)
plt.imshow(thinned,cmap='gray')

dilated_t=cv2.dilate(thinned,kernel,iterations=5)
plt.imshow(dilated,cmap='gray')

contours, _ = cv2.findContours(dilated_t, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

i=0
#list for storing names of shapes
for contour in contours:

#here we are ignoring first contour because


#find countour function detects whole image as shape
if i==0:
i=1
continue
approx = cv2.approxPolyDP(
contour, 0.02 * cv2.arcLength(contour, True), True)

#to draw contours over the image


cv2.drawContours(img, [contour], 0, (0, 0, 255), 2)

#centroid of each shape


M = cv2.moments(contour)
if M['m00'] != 0.0:
x = int(M['m10']/M['m00'])
y = int(M['m01']/M['m00'])

#put name of shape


if len(approx) == 3:
cv2.putText(img,'Triangle',(x,y),
cv2.FONT_HERSHEY_SIMPLEX,2,(255,0,0),5)

elif len(approx) == 4:
cv2.putText(img,'Rectangle',(x,y),
cv2.FONT_HERSHEY_SIMPLEX,2,(0,255,0),5)

elif len(approx) == 5:
cv2.putText(img,'Pentagon',(x,y),
cv2.FONT_HERSHEY_SIMPLEX,2,(0,255,255),5)

elif len(approx) == 6:
cv2.putText(img,'Hexagon',(x,y),
cv2.FONT_HERSHEY_SIMPLEX,0.8,(255,0,255),2)

else:
cv2.putText(img,'circle',(x,y),
cv2.FONT_HERSHEY_SIMPLEX,2,(0,0,255),5)

plt.imshow(img)

Output at Various Stages->


Original Image- Grayscale Image-

Threshold Image- Image after kernel and


dilation of threshold-

Connection of disjoining edges-

Thinning and dilating again-


FINAL IMAGE AFTER CONTOURS-

Learnings->
1. Understanding the basics of image processing techniques like
thresholding, contour detection, and moments calculation.
2. Implementing contour approximation to recognize shapes.
3. Exploring how to determine the shape type based on the
number of vertices and aspect ratio.
4. Gaining practical experience in working with OpenCV library
for image processing tasks.
5. Retrieve Python programming syntax and basic functions
used in image processing libraries like OpenCV.
6. Apply image processing techniques to detect and analyze
geometrical shapes in sample images or datasets.
7. Design and implement customized algorithms for detecting
and recognizing specific types of geometrical shapes in
images.
References->
1. OpenCV Documentation: https://docs.opencv.org/
2. PPT provided on LMS.
3. "Learning OpenCV 4 Computer Vision with Python 3" by
Joseph Howse, Joe Minichino, and Prateek Joshi.
4. "Python Computer Vision" by Jan Erik Solem.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy