Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. import cv2.cv2 as cv2
  2. from math import pi  
  3.  
  4. circle = cv2.imread("E:\Photos_for_TZ\circle.jpg")
  5. circle1 = cv2.cvtColor(circle, cv2.COLOR_BGR2GRAY)
  6. ret, thresh = cv2.threshold(circle1, 150, 255, cv2.THRESH_BINARY)
  7. contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
  8.  
  9. cv2.drawContours(circle, contours, 1, (30, 170, 230), 2)
  10. cv2.drawContours(circle, contours, 2, (255, 170, 0), 2)
  11.  
  12. outside = contours[1]
  13. inside = contours[2]
  14.  
  15. x, y, w, h = cv2.boundingRect(outside)
  16. cv2.rectangle(circle, (x, y), (x + w, y + h), (0, 0, 255), 1)
  17. (x,y),radius = cv2.minEnclosingCircle(outside)
  18. center = (int(x),int(y))
  19. radius = int(radius)
  20. cv2.circle(circle,center,radius,(0, 255, 0), thickness = 1)
  21. print('P контура внешней окружности: ' + str(cv2.arcLength(outside, True)))
  22. print('S контура внешней окружности: ' + str(cv2.contourArea(outside)))
  23. print('S внешнего прямоугольника: '+ str(w*h))
  24. print('S внешней ограничивающей окружности: ' + str(pi*(radius**2)) + '\n')
  25.  
  26. x, y, w, h = cv2.boundingRect(inside)
  27. cv2.rectangle(circle, (x, y), (x + w, y + h), (255, 0, 0),1)
  28. (x,y),radius = cv2.minEnclosingCircle(inside)
  29. center = (int(x),int(y))
  30. radius = int(radius)
  31. cv2.circle(circle,center,radius,(0,255,0),1)
  32. print('P контура внутренней окружности: ' + str(cv2.arcLength(inside, True)))
  33. print('S контура внутренней окружности: ' + str(cv2.contourArea(inside)))
  34. print('S внутреннего прямоугольника: '+ str(w*h))
  35. print('S внутренней ограничивающей окружности: ' + str(pi*(radius**2)) + '\n')
  36.  
  37. cv2.imshow('Circle', circle)
  38.  
  39. cv2.waitKey(0)
  40. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement