Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2.cv2 as cv2
- from math import pi
- circle = cv2.imread("E:\Photos_for_TZ\circle.jpg")
- circle1 = cv2.cvtColor(circle, cv2.COLOR_BGR2GRAY)
- ret, thresh = cv2.threshold(circle1, 150, 255, cv2.THRESH_BINARY)
- contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
- cv2.drawContours(circle, contours, 1, (30, 170, 230), 2)
- cv2.drawContours(circle, contours, 2, (255, 170, 0), 2)
- outside = contours[1]
- inside = contours[2]
- x, y, w, h = cv2.boundingRect(outside)
- cv2.rectangle(circle, (x, y), (x + w, y + h), (0, 0, 255), 1)
- (x,y),radius = cv2.minEnclosingCircle(outside)
- center = (int(x),int(y))
- radius = int(radius)
- cv2.circle(circle,center,radius,(0, 255, 0), thickness = 1)
- print('P контура внешней окружности: ' + str(cv2.arcLength(outside, True)))
- print('S контура внешней окружности: ' + str(cv2.contourArea(outside)))
- print('S внешнего прямоугольника: '+ str(w*h))
- print('S внешней ограничивающей окружности: ' + str(pi*(radius**2)) + '\n')
- x, y, w, h = cv2.boundingRect(inside)
- cv2.rectangle(circle, (x, y), (x + w, y + h), (255, 0, 0),1)
- (x,y),radius = cv2.minEnclosingCircle(inside)
- center = (int(x),int(y))
- radius = int(radius)
- cv2.circle(circle,center,radius,(0,255,0),1)
- print('P контура внутренней окружности: ' + str(cv2.arcLength(inside, True)))
- print('S контура внутренней окружности: ' + str(cv2.contourArea(inside)))
- print('S внутреннего прямоугольника: '+ str(w*h))
- print('S внутренней ограничивающей окружности: ' + str(pi*(radius**2)) + '\n')
- cv2.imshow('Circle', circle)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement