Guest User

Untitled

a guest
Oct 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. import cv2
  2. # Importing the Opencv Library
  3. import numpy as np
  4. # Importing NumPy,which is the fundamental package for scientific computing with Python
  5.  
  6. # Reading Image
  7. img = cv2.imread("/home/kris/Рабочий стол/17.jpg")
  8. cv2.namedWindow("Original Image",cv2.WINDOW_NORMAL)
  9. # Creating a Named window to display image
  10. cv2.imshow("Original Image",img)
  11. # Display image
  12.  
  13. # RGB to Gray scale conversion
  14. img_gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
  15. cv2.namedWindow("Gray Converted Image",cv2.WINDOW_NORMAL)
  16. # Creating a Named window to display image
  17. cv2.imshow("Gray Converted Image",img_gray)
  18. # Display Image
  19.  
  20. # Noise removal with iterative bilateral filter(removes noise while preserving edges)
  21. noise_removal = cv2.bilateralFilter(img_gray,9,75,75)
  22. cv2.namedWindow("Noise Removed Image",cv2.WINDOW_NORMAL)
  23. # Creating a Named window to display image
  24. cv2.imshow("Noise Removed Image",noise_removal)
  25. # Display Image
  26.  
  27. # Histogram equalisation for better results
  28. equal_histogram = cv2.equalizeHist(noise_removal)
  29. cv2.namedWindow("After Histogram equalisation",cv2.WINDOW_NORMAL)
  30. # Creating a Named window to display image
  31. cv2.imshow("After Histogram equalisation",equal_histogram)
  32. # Display Image
  33.  
  34. # Morphological opening with a rectangular structure element
  35. kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
  36. morph_image = cv2.morphologyEx(equal_histogram,cv2.MORPH_OPEN,kernel,iterations=15)
  37. cv2.namedWindow("Morphological opening",cv2.WINDOW_NORMAL)
  38. # Creating a Named window to display image
  39. cv2.imshow("Morphological opening",morph_image)
  40. # Display Image
  41.  
  42. # Image subtraction(Subtracting the Morphed image from the histogram equalised Image)
  43. sub_morp_image = cv2.subtract(equal_histogram,morph_image)
  44. cv2.namedWindow("Subtraction image", cv2.WINDOW_NORMAL)
  45. # Creating a Named window to display image
  46. cv2.imshow("Subtraction image", sub_morp_image)
  47. # Display Image
  48.  
  49. # Thresholding the image
  50. ret,thresh_image = cv2.threshold(sub_morp_image,0,255,cv2.THRESH_OTSU)
  51. cv2.namedWindow("Image after Thresholding",cv2.WINDOW_NORMAL)
  52. # Creating a Named window to display image
  53. cv2.imshow("Image after Thresholding",thresh_image)
  54. # Display Image
  55.  
  56. # Applying Canny Edge detection
  57. canny_image = cv2.Canny(thresh_image,250,255)
  58. cv2.namedWindow("Image after applying Canny",cv2.WINDOW_NORMAL)
  59. # Creating a Named window to display image
  60. cv2.imshow("Image after applying Canny",canny_image)
  61. # Display Image
  62. canny_image = cv2.convertScaleAbs(canny_image)
  63.  
  64. # dilation to strengthen the edges
  65. kernel = np.ones((3,3), np.uint8)
  66. # Creating the kernel for dilation
  67. dilated_image = cv2.dilate(canny_image,kernel,iterations=1)
  68. cv2.namedWindow("Dilation", cv2.WINDOW_NORMAL)
  69. # Creating a Named window to display image
  70. cv2.imshow("Dilation", dilated_image)
  71. # Displaying Image
  72.  
  73. # Finding Contours in the image based on edges
  74. new,contours, hierarchy = cv2.findContours(dilated_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  75. contours= sorted(contours, key = cv2.contourArea, reverse = True)[:10]
  76. # Sort the contours based on area ,so that the number plate will be in top 10 contours
  77. screenCnt = None
  78. # loop over our contours
  79. for c in contours:
  80. # approximate the contour
  81. peri = cv2.arcLength(c, True)
  82. approx = cv2.approxPolyDP(c, 0.06 * peri, True) # Approximating with 6% error
  83. # if our approximated contour has four points, then
  84. # we can assume that we have found our screen
  85. if len(approx) == 4: # Select the contour with 4 corners
  86. screenCnt = approx
  87. break
  88. final = cv2.drawContours(img, [screenCnt], -1, (0, 255, 0), 3)
  89. # Drawing the selected contour on the original image
  90. cv2.namedWindow("Image with Selected Contour",cv2.WINDOW_NORMAL)
  91. # Creating a Named window to display image
  92. cv2.imshow("Image with Selected Contour",final)
  93.  
  94. # Masking the part other than the number plate
  95. mask = np.zeros(img_gray.shape,np.uint8)
  96. new_image = cv2.drawContours(mask,[screenCnt],0,255,-1,)
  97. new_image = cv2.bitwise_and(img,img,mask=mask)
  98. cv2.namedWindow("Final_image",cv2.WINDOW_NORMAL)
  99. cv2.imshow("Final_image",new_image)
  100.  
  101. # Histogram equal for enhancing the number plate for further processing
  102. y,cr,cb = cv2.split(cv2.cvtColor(new_image,cv2.COLOR_RGB2YCrCb))
  103. # Converting the image to YCrCb model and splitting the 3 channels
  104. y = cv2.equalizeHist(y)
  105. # Applying histogram equalisation
  106. final_image = cv2.cvtColor(cv2.merge([y,cr,cb]),cv2.COLOR_YCrCb2RGB)
  107. # Merging the 3 channels
  108. cv2.namedWindow("Enhanced Number Plate",cv2.WINDOW_NORMAL)
  109. # Creating a Named window to display image
  110. cv2.imshow("Enhanced Number Plate",final_image)
  111. # Display image
  112. cv2.waitKey() # Wait for a keystroke from the user
Add Comment
Please, Sign In to add comment