This repository was archived by the owner on Nov 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 212
Detecting geometrical shapes using opencv #247
Merged
powerexploit
merged 10 commits into
powerexploit:master
from
simarpreetsingh-019:master
Sep 17, 2020
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
910719f
Detecting geometrical shapes using opencv
simarpreetsingh-019 fd3dd49
Updated detecting_geometrical_shapes.py
simarpreetsingh-019 6be840e
Created Readme.md
simarpreetsingh-019 c769816
Updated readme.md
simarpreetsingh-019 95ce876
Created Canny edge detection.py
simarpreetsingh-019 545d9ac
Canny Edge Detection mathematics.py
simarpreetsingh-019 c7d0e40
Readme Created.
simarpreetsingh-019 b9d9c95
Images folder added
simarpreetsingh-019 1fc225f
Readme for canny edge detection ready.
simarpreetsingh-019 6590e85
Update Readme.md
simarpreetsingh-019 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
Image-Processing/Detecting Contours/detecting_geometrical_shapes.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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() | ||
|
||
|
||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove new line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay, will be updating by morning on all these.