Skip to content

Commit ba6a809

Browse files
committed
added circle detection
1 parent 8370aac commit ba6a809

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

Python/circle_detection/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Description
2+
3+
A Python Script to detect shapes,shapes area, color,centroid from images.
4+
5+
## How to execute this Script
6+
7+
+ Run `pip install opencv-python` to install the required packages.
8+
+ Add the image to same folder with the name **img.jpeg**
9+
+ Run the script using `python3 circle.py`
10+
+ The script will printout circle radius centre coordinates as well as image with same
11+
12+
## example
13+
14+
### INPUT
15+
![](/Python/circle_detection/img.jpeg)
16+
### OUTPUT
17+
![](/Python/circle_detection/out.png)
18+

Python/circle_detection/circle.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#importing libraries required
2+
import numpy as np
3+
import cv2 as cv
4+
#reading image
5+
img = cv.imread('img.jpeg')
6+
output = img.copy()
7+
#converting image to greyscale
8+
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
9+
gray = cv.medianBlur(gray, 5)
10+
#hough circle detection method for circle detection
11+
circles = cv.HoughCircles(gray, cv.HOUGH_GRADIENT, 1, 20,
12+
param1=50, param2=30, minRadius=0, maxRadius=0)
13+
#all detected circles
14+
detected_circles = np.uint16(np.around(circles))
15+
# looping on all detected circles to print centroid coordinates and radius
16+
for (x, y ,r) in detected_circles[0, :]:
17+
cv.circle(output, (x, y), r, (0, 0, 0), 3)
18+
cv.circle(output, (x, y), 2, (0, 255, 255), 3)
19+
print("center x={},y={},radius = {}\n".format(x,y,r))
20+
21+
#showing the output image
22+
cv.imshow('output',output)
23+
cv.waitKey(0)
24+
cv.destroyAllWindows()

Python/circle_detection/img.jpeg

16.3 KB
Loading

Python/circle_detection/out.png

178 KB
Loading

0 commit comments

Comments
 (0)