Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Detecting geometrical shapes using opencv #247

Merged
merged 10 commits into from
Sep 17, 2020
38 changes: 38 additions & 0 deletions Image-Processing/Detecting Contours/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[![forthebadge](https://forthebadge.com/images/badges/made-with-python.svg)](https://forthebadge.com)
## Detecting Geometrical shapes in an image using OpenCV
### What is Contours?

Contours can be explained simply as a curve joining all the continuous points (along the boundary), having the same color or intensity.
The contours are a useful tool for shape analysis and object detection and recognition.

### Why we discussing about contours?
We will be using Contours to first find the curving of object, then will be checkin and naming about shape of it depending on sides of object.

## Approach :
The approach we would use to detect the shape of a given polygon will be based on classifying the detected shape on the basis of a number of sides it has.
For example, if the detected polynomial has 3 sides, then it could be considered as a triangle, if the polynomial has 4 sides then it could be classified as a square or a rectangle, and so on.

### Important :
Please add the image you want to use this on in the same folder you are saving the code, or else add the whole location.

## Language used : Python


## Libraries used:
1. OpenCV
2. Numpy


### Input

<div align="center">
<img src="https://github.com/simarpreetsingh-019/Awesome-Python-Scripts/blob/master/Image-Processing/Detecting%20Contours/shapes.png" width="250">
</div>


### output

<div align="center">
<img src="https://github.com/simarpreetsingh-019/Awesome-Python-Scripts/blob/master/Image-Processing/Detecting%20Contours/output2.JPG" width="250">
</div>

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import numpy as np
import cv2

img = cv2.imread('shapes.PNG')
imgGry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

ret , thrash = cv2.threshold(imgGry, 240 , 255, cv2.CHAIN_APPROX_NONE)
contours , hierarchy = cv2.findContours(thrash, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

for contour in contours:
approx = cv2.approxPolyDP(contour, 0.01* cv2.arcLength(contour, True), True)
cv2.drawContours(img, [approx], 0, (0, 0, 0), 5)
x = approx.ravel()[0]
y = approx.ravel()[1] - 5

if len(approx) == 3:
cv2.putText( img, "Triangle", (x, y), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0) )
elif len(approx) == 4 :
x, y , w, h = cv2.boundingRect(approx)
aspectRatio = float(w)/h
print(aspectRatio)

if aspectRatio >= 0.95 and aspectRatio < 1.05:
cv2.putText(img, "square", (x, y), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0))

else:
cv2.putText(img, "rectangle", (x, y), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0))

elif len(approx) == 5 :
cv2.putText(img, "pentagon", (x, y), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0))

elif len(approx) == 10 :
cv2.putText(img, "star", (x, y), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0))

else:
cv2.putText(img, "circle", (x, y), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0))

cv2.imshow('shapes', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Binary file added Image-Processing/Detecting Contours/output2.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Image-Processing/Detecting Contours/shapes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.