Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4. def draw_random_lines(img, w, n):
  5. for i in range(n):
  6. point1 = (np.random.randint(low = 0, high = w), np.random.randint(low = 0, high = w))
  7. point2 = (np.random.randint(low = 0, high = w), np.random.randint(low = 0, high = w))
  8. cv2.line(img,point1,point2,(255,0,0),5)
  9. x = y = 0
  10. while(y<w):
  11. while(x<w):
  12. if(np.any(img[x, y] != 0)):
  13. if(np.random.randint(low=0, high=100) < 60):
  14. img[x, y] = [255, 255, 255]
  15. else:
  16. img[x, y] = [0, 0, 0]
  17. else:
  18. if(np.random.randint(low=0, high=100) < 95):
  19. img[x, y] = [255, 255, 255]
  20. else:
  21. img[x, y] = [0, 0, 0]
  22. x+=1
  23. x=0
  24. y+=1
  25. return img
  26.  
  27. w = 512
  28. img = np.zeros((w,w,3), np.uint8)
  29. img = draw_random_lines(img, w, 5)
  30. cv2.imshow("Original", img)
  31. cv2.imwrite("alo.png", img)
  32. img = cv2.imread("alo.png")
  33.  
  34. gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  35. edges = cv2.Canny(gray,50,150,apertureSize = 3)
  36.  
  37. lines = cv2.HoughLines(edges,1,np.pi/180,200)
  38. for line in lines:
  39. for rho,theta in line:
  40. a = np.cos(theta)
  41. b = np.sin(theta)
  42. x0 = a*rho
  43. y0 = b*rho
  44. x1 = int(x0 + 1000*(-b))
  45. y1 = int(y0 + 1000*(a))
  46. x2 = int(x0 - 1000*(-b))
  47. y2 = int(y0 - 1000*(a))
  48.  
  49. cv2.line(img,(x1,y1),(x2,y2),(0,0,255),2)
  50.  
  51.  
  52. cv2.imshow("Detectada", img)
  53.  
  54. cv2.waitKey(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement