Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. #прогрессивное преобразование Хафа
  4. img = cv2.imread('/home/leti/documents/lab/laba6/6_1.png',cv2.IMREAD_REDUCED_COLOR_2)
  5. gray =255-cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  6. edges = cv2.Canny(gray,50,150,apertureSize = 3)
  7. minLineLength =30
  8. maxLineGap =1
  9. cv2.imshow('5.jpg',gray)
  10. # вариант1
  11. # lenght=7
  12. # i=5
  13. # while lenght>5:
  14. # lines = cv2.HoughLinesP(gray,1,np.pi/1800,i,minLineLength,maxLineGap)
  15. # i=i+20
  16. # lenght=len(lines)
  17. # print(len(lines))
  18. # print(i)
  19. # print(len(lines))
  20. # for x1,y1,x2,y2 in lines[len(lines)-1]:
  21. # cv2.line(img,(x1,y1),(x2,y2),(0,255,0),1 )
  22. # вариант2
  23. lines = cv2.HoughLinesP(gray,1,np.pi/1800,700,minLineLength,maxLineGap)
  24. dlina=[]
  25. for _ in range(len(lines)):
  26. dlina.append(0)
  27. for i in range(len(lines)):
  28. for x1,y1,x2,y2 in lines[i]:
  29. dlina[i]=(y2-y1)**2+(x2-x1)**2
  30. print(dlina.index(max(dlina)))
  31. for x1,y1,x2,y2 in lines[dlina.index(max(dlina))]:
  32. cv2.line(img,(x1,y1),(x2,y2),(0,255,0),3 )
  33. cv2.imshow('houghlines5.jpg',img)
  34. cv2.imwrite('/home/leti/documents/lab/laba6/houghlines5.jpg',img)
  35. #вариант 1 окружность
  36. circles=cv2.HoughCircles(gray,cv2.HOUGH_GRADIENT,2,30,param1=100,param2=300)
  37. print(max(circles[0,:,2]))
  38. index=0
  39. while circles[0,index,2]<max(circles[0,:,2]):
  40. index=index+1
  41. print(circles[0,index])
  42. [x0, y0, r]=circles[0,index]
  43. cv2.circle(img,(x0,y0),r,(0,255,0),3 )
  44. cv2.imshow('houghlines5.jpg',img)
  45. cv2.imwrite('/home/leti/documents/lab/laba6/houghlines5.jpg',img)
  46. cv2.waitKey(0)
  47. cv2.destroyAllWindows
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement