Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. import cv2.cv2 as cv2
  2. import numpy
  3. import math as m
  4.  
  5. def draw_line_P(img, x1, y1, x2, y2, color = (0, 0, 255), thickness = 2, lineType=cv2.LINE_AA):
  6. cv2.line(img, (x1, y1), (x2, y2), color, thickness, lineType)
  7.  
  8. def draw_circle(center, radius,img, color=(0, 255, 0),thickness=2,lineType=cv2.LINE_AA):
  9. cv2.circle(img, center,radius, color,thickness, lineType)
  10.  
  11. foto = cv2.imread('D:/pitonlaba/laba6/6_1_1.png',cv2.IMREAD_GRAYSCALE)
  12. tofo = cv2.imread('D:/pitonlaba/laba6/6_1_1.png',cv2.IMREAD_COLOR)
  13. tofo1 = cv2.imread('D:/pitonlaba/laba6/6_1_1.png',cv2.IMREAD_COLOR)
  14. tofo2 = cv2.imread('D:/pitonlaba/laba6/6_1_1.png',cv2.IMREAD_COLOR)
  15.  
  16. ing , img = cv2.threshold(foto, 122, 255 , cv2.THRESH_BINARY_INV)
  17.  
  18. lines = cv2.HoughLinesP( image = img, rho = 1, theta = numpy.pi/720, threshold = 255, maxLineGap = 15)
  19.  
  20. OTVET = 0
  21. (a, b , c ) = numpy.shape(lines)
  22. (x1 , y1 , x2 , y2) = lines[0][0]
  23. max = (((x1-x2)**2)+((y1-y2)**2))
  24.  
  25. for H in range(1,a):
  26. (x1 , y1 , x2 , y2) = lines[H][0]
  27. if (((x1-x2)**2)+((y1-y2)**2)) > max:
  28. max = (((x1-x2)**2)+((y1-y2)**2))
  29. OTVET = H
  30.  
  31. (x1 , y1 , x2 , y2) = lines[OTVET][0]
  32. draw_line_P(tofo, x1 , y1 ,x2, y2)
  33. draw_line_P(tofo2, x1 , y1 ,x2, y2)
  34.  
  35. rez1Canny = cv2.GaussianBlur(foto,(9,9),0) #кени
  36.  
  37. threshold1 = 100 #кени
  38. threshold2 = 200 #кени
  39.  
  40. rez1Canny = cv2.Canny(foto, threshold1, threshold2, apertureSize= 7 , L2gradient= True)
  41.  
  42. Circles = cv2.HoughCircles( rez1Canny , cv2.HOUGH_GRADIENT, dp = 2,minDist = 1)
  43.  
  44. OTVET = 0
  45. (a, b , c ) = numpy.shape(Circles)
  46. (x , y , r) = Circles[0][0]
  47. max = m.pi *r*r
  48.  
  49. for H in range(1,a):
  50. (x , y ,r) = Circles[0][H]
  51. print(m.pi *r*r )
  52. if m.pi *r*r > max:
  53. max = m.pi *r*r
  54. OTVET = H
  55.  
  56. (x , y , r) = Circles[0][OTVET]
  57. draw_circle((x , y) , r, tofo1)
  58. draw_circle((x , y) , r, tofo2)
  59.  
  60. cv2.imshow('isxodnik', foto)
  61. cv2.imshow('rez line', tofo)
  62. cv2.imshow('rez circles', tofo1)
  63. cv2.imshow('rez line and circles', tofo2)
  64. cv2.waitKey(0)
  65. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement