Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4.  
  5. def drawlines(img_source, lines):
  6. img = np.zeros_like(img_source)
  7. if lines is not None:
  8. for line in lines:
  9. pnt = line[0]
  10. cv2.line(img, (pnt[0], pnt[1]), (pnt[2], pnt[3]), 255, 3)
  11. cv2.imshow('lines', img)
  12.  
  13.  
  14. def image_processing(img_source):
  15. h1 = cv2.getTrackbarPos('R1', 'image')
  16. s1 = cv2.getTrackbarPos('G1', 'image')
  17. v1 = cv2.getTrackbarPos('B1', 'image')
  18.  
  19. h2 = cv2.getTrackbarPos('R2', 'image')
  20. s2 = cv2.getTrackbarPos('G2', 'image')
  21. v2 = cv2.getTrackbarPos('B2', 'image')
  22.  
  23. img_source = cv2.GaussianBlur(img_source, (7,7), 0)
  24. cv2.imshow('img_source', img_source)
  25. matrice_input = np.float32([ [146, 204], [256, 205], [31, 300], [348, 301] ])
  26. matrice_output = np.float32([[400, 400], [0, 400], [400, 0], [0, 0]])
  27. if (img_source.shape[2]) == 3:
  28. img_source = cv2.cvtColor(img_source, cv2.COLOR_BGR2HSV)
  29. cv2.imshow('hsv', img_source)
  30. img_source = cv2.inRange(img_source, np.array([h1,s1,v1]), np.array([h2,s2,v2]))
  31.  
  32. cv2.imshow('bw', img_source)
  33. #img_source = transform_perspective(matrice_input ,matrice_output,img_source,(400, 400))
  34.  
  35. #img_source = cv2.Canny(img_source, 200, 300, 3)
  36.  
  37. #lines = cv2.HoughLinesP(img_source, 1, np.pi/180, 180, 5, 15)
  38. #drawlines(img_source, lines)
  39. return img_source
  40.  
  41. def transform_perspective(matrice_input, matrice_output, img, matrice_size):
  42. transformation_matrice = cv2.getPerspectiveTransform(matrice_input, matrice_output)
  43. img = cv2.warpPerspective(img, transformation_matrice, matrice_size )
  44. return img
  45.  
  46.  
  47.  
  48. def get_benchmarks(time_benchmark1, time_benchmark2):
  49. time_elapsed = (time_benchmark2 - time_benchmark1) / cv2.getTickFrequency()
  50. fps = 1 / time_elapsed
  51. print (fps, time_elapsed)
  52. return 0
  53.  
  54.  
  55. def nothing(x):
  56. pass
  57.  
  58. def main():
  59. src = cv2.VideoCapture(0)
  60. # (test.h264) src.shape() = (400, 400)
  61. cv2.namedWindow('image')
  62. cv2.createTrackbar('R1', 'image', 0, 255, nothing)
  63. cv2.createTrackbar('G1', 'image', 0, 255, nothing)
  64. cv2.createTrackbar('B1', 'image', 0, 255, nothing)
  65. cv2.createTrackbar('R2', 'image', 0, 255, nothing)
  66. cv2.createTrackbar('G2', 'image', 0, 255, nothing)
  67. cv2.createTrackbar('B2', 'image', 0, 255, nothing)
  68.  
  69.  
  70. while True:
  71.  
  72.  
  73.  
  74.  
  75. time_benchmark1 = cv2.getTickCount()
  76. ret, img_source = src.read()
  77. img_new = image_processing(img_source)
  78.  
  79.  
  80. time_benchmark2 = cv2.getTickCount()
  81. get_benchmarks(time_benchmark1, time_benchmark2)
  82.  
  83.  
  84. cv2.imshow('Output', img_new)
  85. if cv2.waitKey(1) & 0xFF == ord('q'):
  86. break
  87.  
  88.  
  89. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement