0% found this document useful (0 votes)
25 views17 pages

Tanyagarg ELCactivity

Uploaded by

tgarg1168
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)
25 views17 pages

Tanyagarg ELCactivity

Uploaded by

tgarg1168
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/ 17

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


Submitted by

Tanya Garg

Roll Number – 102315075

DEPARTMENT OF ELECTRONICS AND COMMUNICATION


ENGINEERING

February- April 2024

THAPAR INSTITUTE OF ENGINEERING & TECHNOLOGY, PATIALA

Objective –
The primary goal of this project is
to design and implement an
intelligent system capable of
detecting and recognizing various
geometric shapes in digital
images. We aim to create a
versatile solution that can adapt to
different scenarios,
accommodating variations in
scale, orientation, and noise levels.
By leveraging Python's extensive
libraries such as OpenCV and
NumPy, we aspire to develop
algorithms that not only excel in
accuracy but also exhibit real-time
processing capabilities.

Introduction

In the dynamic realm of computer


vision and image processing, the
ability to comprehend and interpret
geometric shapes is a fundamental
aspect with widespread applications.
The project "ShapeSense" embarks on
a journey to explore the intricacies of
Geometrical Shape Detection and
Recognition using the powerful
programming language, Python. This
venture delves into the fascinating
intersection of mathematics, computer
science, and image analysis to unveil
the potential of automated shape
recognition systems.
Geometrical shape detection and
recognition play a pivotal role in
numerous fields, ranging from
industrial automation and robotics to
medical imaging and augmented
reality. These applications demand
precision, speed, and reliability, which
can be achieved through advanced
image processing techniques. With the
advent of Python and its rich
ecosystem of libraries, the
development of robust and efficient
shape detection algorithms has become
more accessible than ever.

Key Components:

1.) Image Preprocessing:


Before delving into shape detection,
the project emphasizes the importance
of image preprocessing. This phase
involves tasks such as noise reduction,
contrast enhancement, and edge
detection to optimize the input image
for subsequent shape analysis.

2.) Edge Detection:


One of the critical steps in shape
detection is the identification of edges
within an image. Leveraging state-of-
the-art edge detection algorithms, the
project aims to extract the boundaries
of objects, laying the foundation for
subsequent shape recognition.

3.) Shape Classification:


Python's machine learning capabilities
will be harnessed to develop a shape
classification model. This involves
training the system on a diverse dataset
of annotated shapes, enabling it to
generalize and accurately identify
different geometrical entities.

4.) User Interface:

To enhance user experience and


applicability, ShapeSense will
feature an intuitive user interface.
This interface will allow users to
upload images, visualize the
detected shapes, and access relevant
information about the recognized
geometrical entities.

 Flow Chart

Start

[User Initiates Image Upload]


--> [Image Preprocessing]

[Image Preprocessing]

v
[Apply Noise Reduction]

[Enhance Contrast]

[Edge Detection]

[Identify Object Boundaries]

v
[Shape Classification]

[Train Machine Learning


Model]

[Classify Shapes]

[User Interface]

|
v

[Display Results and


Information]

[End]

 Python code

 import cv2 #supporting computer vision library


 import PIL #python image library
 import numpy as np #numerical operations
 from matplotlib import pyplot as plt #visualisation library
in python for 2D plots
 from PIL import Image #opening, rotating, displaying an image
 from matplotlib import image as mpimg #image read and image
show function
 img= Image.open("ELCimage.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')
 kernelo=cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
 #Dilation on threshold image output
 dilated=cv2.dilate(threshold,kernel, iterations=3)
 plt.imshow(dilated, cmap='gray')
 #connection of disjoining edges in the shape
 closed=cv2.morphologyEx(dilated, cv2.MORPH_CLOSE,kernelc,
iterations=3)
 plt.imshow(closed, cmap='gray')
 opening=cv2.morphologyEx(closed,cv2.MORPH_OPEN, kernelo,
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_t, cmap='gray')
 contours,_=cv2.findContours(dilated_t,cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
 i=0
 for contour in contours:

 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 shape name
 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

JPEG
(1238,1280)
RGB
 Learnings:
The Geometrical Shape Detection and Recognition project
using Python in Image Processing offers several key
learnings:

1. **Image Preprocessing Techniques:**

- Understanding the significance of preparing images


through noise reduction, contrast enhancement, and edge
detection for improved analysis.

2. **Edge Detection Algorithms:**

- Familiarity with algorithms crucial for identifying


object boundaries, a fundamental step in shape
recognition.

3. **Machine Learning for Shape Classification:**

- Applying machine learning for shape classification,


involving training models to recognize various
geometrical shapes.

4. **Python Programming Skills:**

- Strengthening Python proficiency, particularly in using


libraries like OpenCV and NumPy, and integrating
machine learning frameworks.

5. **User Interface Design:**

- Designing an intuitive interface for user interaction,


including image upload, results display, and information
provision.

6. **Real-world Applications:**
- Recognizing practical applications of shape detection
in industries such as manufacturing, quality control, and
healthcare.

7. **Cross-disciplinary Integration:**

- Integrating concepts from mathematics, computer


science, and image analysis to create a holistic solution.

8. **Problem-solving and Optimization:**

- Addressing challenges related to scale, orientation, and


noise in shape detection, implementing optimization
techniques.

9. **Ethical Considerations:**

- Understanding copyright and licensing issues when


using external content, adhering to ethical data practices.

10. **Project Management:**

- Developing project management skills through logical


step breakdown, creating a flowchart, and organizing code
efficiently.

11. **Continuous Learning:**

- Recognizing the dynamic nature of technology,


emphasizing the importance of ongoing learning in image
processing, computer vision, and machine learning.

 References
 Books:
"Programming Computer Vision with Python" by Jan Erik
Solem

"OpenCV 4 with Python Blueprints" by Daniel Lélis


Baggio and Michael Beyeler

"Python Machine Learning" by Sebastian Raschka and


Vahid Mirjalili

 Online Documentation and Tutorials:

OpenCV Documentation: OpenCV Documentation

NumPy Documentation: NumPy Documentation

Scikit-learn Documentation: Scikit-learn Documentation

You might also like