|
| 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() |
0 commit comments