Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- import math
- img = cv2.imread('TUP74.jpg', cv2.IMREAD_COLOR)
- height, width = img.shape[:2]
- _, thresh = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 31, 255, cv2.THRESH_BINARY)
- # Find top/bottom of the circle, to determine radius
- row_info = cv2.findNonZero(cv2.reduce(thresh, 1, cv2.REDUCE_MAX))
- first_y, last_y = row_info[0][0][1], row_info[-1][0][1]
- diameter = last_y - first_y
- radius = int(diameter / 2)
- center_y = first_y + radius
- # Repeat again, just on first column
- row_info = cv2.findNonZero(thresh[:,0])
- first_y, last_y = row_info[0][0][1], row_info[-1][0][1]
- h = last_y - first_y
- center_x = int(math.sqrt(radius**2 - (h/2)**2))
- cv2.circle(img, (center_x,center_y), radius, (0,0,255), 2)
- s = int(math.sqrt(radius**2 / 2))
- tl = (center_x - s, center_y - s)
- br = (center_x + s, center_y + s)
- cv2.rectangle(img, tl, br, (255,0,0), 2)
- cv2.imshow('', img)
- cv2.waitKey()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement