Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. # check other adjustments (contrast, luminance)
  2. # check if you can disable auto adjustments in camera
  3. # check for all these to be adaptively applied
  4.  
  5. import cv2
  6. import numpy as np
  7. import math
  8.  
  9. # get image
  10. img = cv2.imread('C:\\Users\\calin\\Desktop\\Bosch\\lateralControl\\sampleImage.jpg', -1)
  11.  
  12. # scale down
  13. # check other dimensions
  14. # check other interpolation methods
  15. # check speed without resizing
  16. width = 700
  17. height = 400
  18.  
  19. img = cv2.resize(img, (width, height), interpolation = cv2.INTER_AREA)
  20.  
  21. # crop to region of interest
  22. # check other shapes for RoI
  23. """
  24. def cropRegion(img, vertices):
  25. mask = np.zeros_like(img)
  26. channelCount = img.shape[2]
  27. matchMaskColor = (255,) * channelCount
  28. cv2.fillPoly(mask, vertices, matchMaskColor)
  29. maskedImage = cv2.bitwise_and(img, mask)
  30. return maskedImage
  31.  
  32. roiVertices = [
  33. (30, height - 30),
  34. (width / 2, height / 2),
  35. (width - 30, height - 30)
  36. ]
  37.  
  38. img = cropRegion(img, np.array([roiVertices], np.int32),)
  39. img = img[(int(height/2)):(height - 30), 30:(width - 30)]
  40. """
  41. # grayscale
  42. # check performance without grayscale
  43. # check selecting a single channel (R, G, B) instead of grayscale
  44. img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  45.  
  46. # otsu - works only without cropping
  47. # check different values of blur
  48. # check with/without blur
  49. # check with/without
  50. img = img[(int(height/1.8)):(height - 30), (int(width*0.2)):(width - (int(width*0.15)))]
  51.  
  52. img = cv2.GaussianBlur(img, (5,1), 0)
  53. img1, img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
  54.  
  55. # adaptive mean threshold
  56. # check different values
  57. #img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, -10)
  58.  
  59. # adaptive gaussian threshold
  60. # check different values
  61. #img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, -15)
  62.  
  63. # opening/closing (erosion + dilation)
  64. # check different values for kernel
  65. #kernel = np.ones((1,2), np.uint8)
  66. #img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
  67.  
  68. #kernel = np.ones((3,3), np.uint8)
  69. #img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
  70.  
  71. # canny
  72. #img = cv2.Canny(img, 100, 200)
  73.  
  74. # crop
  75. def cropRegion(img, vertices):
  76. mask = np.zeros_like(img)
  77. channelCount = 1
  78. matchMaskColor = (255,) * channelCount
  79. cv2.fillPoly(mask, vertices, matchMaskColor)
  80. maskedImage = cv2.bitwise_and(img, mask)
  81. return maskedImage
  82.  
  83. roiVertices = [
  84. (0, len(img)),
  85. (len(img[0]) / 2, 0),
  86. (len(img[0]), len(img))
  87. ]
  88.  
  89. img = cropRegion(img, np.array([roiVertices], np.int32),)
  90.  
  91. # custom method
  92. """
  93. m = len(img)
  94. n = len(img[0])
  95.  
  96. bigSm = 0
  97. bigNm = 0
  98.  
  99. for i in range(m):
  100. sm = 0
  101. nm = 0
  102. for j in range (n):
  103. if img[i][j] != 0 :
  104. sm = sm + j
  105. nm = nm + 1
  106. if nm != 0:
  107. aux = int(sm/nm)
  108. bigSm = bigSm + aux
  109. bigNm = bigNm + 1
  110. img[i][aux] = 255
  111.  
  112. bigAux = int(bigSm/bigNm)
  113. for i in range(m):
  114. img[i][bigAux] = 255
  115. """
  116.  
  117. cv2.imshow('image', img)
  118. cv2.waitKey(0)
  119. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement