Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. from skimage.io import imread
  2. from skimage.filters import threshold_otsu
  3. import matplotlib.pyplot as plt
  4.  
  5. filename = './video15.mp4'
  6.  
  7. import cv2
  8. #cap = cv2.VideoCapture(filename)
  9. #cap = cv2.VideoCapture(0)
  10. count = 0
  11. #while cap.isOpened():
  12. #ret,frame = cap.read()
  13. #if ret == True:
  14. #cv2.imshow('window-name',frame)
  15. #cv2.imwrite("./output/frame%d.jpg" % count, frame)
  16. #count = count + 1
  17. #if cv2.waitKey(10) & 0xFF == ord('q'):
  18. #break
  19. #else:
  20. #break
  21. #cap.release()
  22. #cv2.destroyAllWindows()
  23.  
  24. # car image -> grayscale image -> binary image
  25. import imutils
  26. #car_image = imread("./output/frame%d.jpg"%(count-1), as_gray=True)
  27. #car_image = imutils.rotate(car_image, 270)
  28. car_image = imread("carMK3.jpg", as_gray=True)
  29. # it should be a 2 dimensional array
  30. print(car_image.shape)
  31.  
  32. # the next line is not compulsory however, a grey scale pixel
  33. # in skimage ranges between 0 & 1. multiplying it with 255
  34. # will make it range between 0 & 255 (something we can relate better with
  35.  
  36. gray_car_image = car_image * 255
  37. fig, (ax1, ax2) = plt.subplots(1, 2)
  38. ax1.imshow(gray_car_image, cmap="gray")
  39. threshold_value = threshold_otsu(gray_car_image)
  40. binary_car_image = gray_car_image > threshold_value
  41. print(binary_car_image)
  42. ax2.imshow(binary_car_image, cmap="gray")
  43. ax2.imshow(gray_car_image, cmap="gray")
  44. plt.show()
  45.  
  46. # CCA (finding connected regions) of binary image
  47.  
  48.  
  49. from skimage import measure
  50. from skimage.measure import regionprops
  51. import matplotlib.pyplot as plt
  52. import matplotlib.patches as patches
  53.  
  54. # this gets all the connected regions and groups them together
  55. label_image = measure.label(binary_car_image)
  56.  
  57. # print(label_image.shape[0]) #width of car img
  58.  
  59. # getting the maximum width, height and minimum width and height that a license plate can be
  60. plate_dimensions = (0.03*label_image.shape[0], 0.08*label_image.shape[0], 0.15*label_image.shape[1], 0.3*label_image.shape[1])
  61. plate_dimensions2 = (0.08*label_image.shape[0], 0.2*label_image.shape[0], 0.15*label_image.shape[1], 0.4*label_image.shape[1])
  62. min_height, max_height, min_width, max_width = plate_dimensions
  63. plate_objects_cordinates = []
  64. plate_like_objects = []
  65.  
  66. fig, (ax1) = plt.subplots(1)
  67. ax1.imshow(gray_car_image, cmap="gray")
  68. flag =0
  69. # regionprops creates a list of properties of all the labelled regions
  70. for region in regionprops(label_image):
  71. # print(region)
  72. if region.area < 50:
  73. #if the region is so small then it's likely not a license plate
  74. continue
  75. # the bounding box coordinates
  76. min_row, min_col, max_row, max_col = region.bbox
  77. # print(min_row)
  78. # print(min_col)
  79. # print(max_row)
  80. # print(max_col)
  81.  
  82. region_height = max_row - min_row
  83. region_width = max_col - min_col
  84. # print(region_height)
  85. # print(region_width)
  86.  
  87. # ensuring that the region identified satisfies the condition of a typical license plate
  88. if region_height >= min_height and region_height <= max_height and region_width >= min_width and region_width <= max_width and region_width > region_height:
  89. flag = 1
  90. plate_like_objects.append(binary_car_image[min_row:max_row,
  91. min_col:max_col])
  92. plate_objects_cordinates.append((min_row, min_col,
  93. max_row, max_col))
  94. rectBorder = patches.Rectangle((min_col, min_row), max_col - min_col, max_row - min_row, edgecolor="red",
  95. linewidth=2, fill=False)
  96. ax1.add_patch(rectBorder)
  97. # let's draw a red rectangle over those regions
  98. if(flag == 1):
  99. # print(plate_like_objects[0])
  100. plt.show()
  101.  
  102.  
  103.  
  104.  
  105. if(flag==0):
  106. min_height, max_height, min_width, max_width = plate_dimensions2
  107. plate_objects_cordinates = []
  108. plate_like_objects = []
  109.  
  110. fig, (ax1) = plt.subplots(1)
  111. ax1.imshow(gray_car_image, cmap="gray")
  112.  
  113. # regionprops creates a list of properties of all the labelled regions
  114. for region in regionprops(label_image):
  115. if region.area < 50:
  116. #if the region is so small then it's likely not a license plate
  117. continue
  118. # the bounding box coordinates
  119. min_row, min_col, max_row, max_col = region.bbox
  120. # print(min_row)
  121. # print(min_col)
  122. # print(max_row)
  123. # print(max_col)
  124.  
  125. region_height = max_row - min_row
  126. region_width = max_col - min_col
  127. # print(region_height)
  128. # print(region_width)
  129.  
  130. # ensuring that the region identified satisfies the condition of a typical license plate
  131. if region_height >= min_height and region_height <= max_height and region_width >= min_width and region_width <= max_width and region_width > region_height:
  132. # print("hello")
  133. plate_like_objects.append(binary_car_image[min_row:max_row,
  134. min_col:max_col])
  135. plate_objects_cordinates.append((min_row, min_col,
  136. max_row, max_col))
  137. rectBorder = patches.Rectangle((min_col, min_row), max_col - min_col, max_row - min_row, edgecolor="red",
  138. linewidth=2, fill=False)
  139. ax1.add_patch(rectBorder)
  140. # let's draw a red rectangle over those regions
  141. # print(plate_like_objects[0])
  142. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement