Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.85 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import math
  4.  
  5. def draw_line_P(img, x1, y1, x2, y2, color = (0, 0, 255), thickness = 2,
  6. lineType=cv2.LINE_AA):
  7.     cv2.line(img, (x1, y1), (x2, y2), color, thickness, lineType)
  8.  
  9. def draw_circle(img, center, radius, color=(0, 0, 255), thickness = 2,
  10. lineType = cv2.LINE_AA):
  11.     cv2.circle(img, center, radius, color, thickness, lineType)
  12.  
  13. img = cv2.imread("C:\Program Files (x86)\Photos for TZ\6_1.png",cv2.IMREAD_COLOR)
  14. allLines = np.copy(img)
  15. longestLine = np.copy(img)
  16. allCircles = np.copy(img)
  17. biggestCircle = np.copy(img)
  18.  
  19. img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  20. img_grayinv = cv2.bitwise_not(img_gray)
  21.  
  22. lines = cv2.HoughLinesP(img_grayinv, rho = 1, theta = np.pi/720, threshold = 230,
  23. minLineLength=150, maxLineGap = 15)
  24.  
  25. maxLen = 0
  26. x1P, y1P, x2P, y2P = 0, 0, 0, 0
  27. for line in lines:
  28.     x1, y1, x2, y2 = line[0]
  29.     currentLen = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
  30.     if currentLen >= maxLen:
  31.         maxLen = currentLen
  32.         x1P, y1P, x2P, y2P = x1, y1, x2, y2
  33.     draw_line_P(allLines, x1, y1, x2, y2)
  34. draw_line_P(longestLine, x1P, y1P, x2P, y2P)
  35.  
  36. edges = cv2.Canny(img_gray, 0, 255)
  37. circles = cv2.HoughCircles(edges, cv2.HOUGH_GRADIENT, dp = 1, minDist = 250,
  38. param1 = 255, param2 = 97, minRadius = 0, maxRadius = 0)                            
  39.  
  40. xmax, ymax, rmax = 0, 0, 0
  41. for circle in circles[0]:
  42.     x0, y0, r = circle
  43.     if r > rmax:
  44.         xmax, ymax, rmax = x0, y0, r
  45.     draw_circle(allCircles, (x0, y0), r)
  46. draw_circle(biggestCircle, (xmax, ymax), rmax)
  47.  
  48. cv2.imwrite("C:\Program Files (x86)\Photos for TZ\allLines.png", allLines)
  49. cv2.imwrite("C:\Program Files (x86)\Photos for TZ\longestLine.png", longestLine)
  50. cv2.imwrite("C:\Program Files (x86)\Photos for TZ\allCircles.png", allCircles)
  51. cv2.imwrite("C:\Program Files (x86)\Photos for TZ\biggestCircle.png", biggestCircle)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement