Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. # import the necessary packages
  2. from __future__ import division
  3. import numpy as np
  4. import cv2
  5. from matplotlib import pyplot as plt
  6.  
  7. images = [
  8. [cv2.imread("valentin.png"), "valentin"],
  9. [cv2.imread("left.png"), "left"],
  10. #[cv2.imread("right.png"), "right"],
  11. #[cv2.imread("noway.png"), "noway"],
  12. #[cv2.imread("up.png"), "uo"]
  13. ]
  14.  
  15. def ToBlackWhite(img):
  16. height, width = img.shape[:2]
  17.  
  18. for i in range(0,height):
  19. for j in range(0,width):
  20. # BGR
  21. if (img[i,j,2] > 2*img[i,j,1] and img[i,j,2] > 2*img[i,j,0]) or (img[i,j,0] > 2*img[i,j,1] and img[i,j,0] > 2*img[i,j,2]):
  22. img[i,j] = [255,255,255]
  23. else:
  24. img[i,j] = [0,0,0];
  25. return img
  26.  
  27. def equal(p1, p2):
  28. return abs(np.int16(p1) - np.int16(p2)) < 10
  29.  
  30.  
  31. def calculate(img1, img2):
  32. count = 0
  33. height1, width1 = img1.shape[:2]
  34. height2, width2 = img2.shape[:2]
  35.  
  36. img1 = cv2.resize(img1, (width2, height2))
  37.  
  38.  
  39. for w in range(0,width2):
  40. for h in range(0,height2):
  41. if equal(img1[h][w], img2[h][w]):
  42. count = count + 1
  43. print "calculate", count, "from", width2,width2
  44.  
  45. return count/(width2*height2)
  46.  
  47.  
  48. #for (lower, upper) in boundaries:
  49. # lower = np.array(lower, dtype = "uint8")
  50. #upper = np.array(upper, dtype = "uint8")
  51.  
  52. # find the colors within the specified boundaries and apply
  53. # the mask
  54. # mask = cv2.inRange(img1, lower, upper)
  55. # output = cv2.bitwise_and(image, image, mask = mask)
  56.  
  57. def isWhitePixel(image, w, h):
  58. #print w,h
  59. #print image[w][h]
  60. return image[w][h][0] > 200 and image[w][h][1] > 200 and image[w][h][2] > 200
  61.  
  62. def isLeftTurn(positions):
  63. dif = 10
  64. count = 0
  65. perespect = ((positions[0] - positions[1])+(positions[1] - positions[2]))/2
  66. dif = dif+perespect
  67. print perespect
  68. if positions[0] - positions[1] > dif:
  69. count = count +1
  70. if positions[1] - positions[2] > dif:
  71. count = count +1
  72. if positions[2] - positions[3] > dif:
  73. count = count +1
  74. if positions[3] - positions[4] > dif:
  75. count = count +1
  76. if positions[4] - positions[5] > dif:
  77. count = count +1
  78. #if count >3:
  79. print "left", count
  80.  
  81. def isRightTurn(positions):
  82. dif = 10
  83. count = 0
  84. perespect = ((positions[0] - positions[1])+(positions[1] - positions[2]))/2
  85. dif = dif + perespect
  86. if positions[1] - positions[0] > dif:
  87. count = count +1
  88. if positions[2] - positions[1] > dif:
  89. count = count +1
  90. if positions[3] - positions[2] > dif:
  91. count = count +1
  92. if positions[4] - positions[3] > dif:
  93. count = count +1
  94. if positions[5] - positions[4] > dif:
  95. count = count +1
  96. #if count > 3:
  97. print "right", count
  98.  
  99. def isWhite(image, w, h):
  100. return isWhitePixel(image, w, h) and isWhitePixel(image, w+2, h) and isWhitePixel(image, w+4, h) and isWhitePixel(image, w+6, h) and isWhitePixel(image, w+8, h)
  101.  
  102. def processRoad(img):
  103. height, width = img.shape[:2]
  104.  
  105. lower = np.array([200, 200, 200], dtype = "uint8")
  106. upper = np.array([255, 255, 255], dtype = "uint8")
  107. mask = cv2.inRange(img, lower, upper)
  108. output = cv2.bitwise_and(image, image, mask = mask)
  109. # output = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
  110. #output2 = cv2.Canny(output,100,200)
  111. cv2.imshow("road", output)
  112.  
  113. h, w = image.shape[:2]
  114.  
  115. positions = []
  116.  
  117. for i in range(h-10, 2, -15):
  118. for j in range(w-10, 2, -15):
  119. #print i,h,j,w
  120. if isWhite(output, i, j):
  121. #print "positions append",j
  122. positions.append(j)
  123. break
  124. if len(positions) > 0:
  125. print len(positions)
  126. if len(positions) > 5:
  127. if isLeftTurn(positions):
  128. print "left"
  129. elif isRightTurn(positions):
  130. print "right"
  131. else:
  132. print "none"
  133.  
  134.  
  135. # load the image
  136. #image = cv2.imread("img.png")
  137.  
  138. video_capture = cv2.VideoCapture(1)
  139. cv2.startWindowThread()
  140. cv2.namedWindow("preview")
  141.  
  142. for idx, image in enumerate(images):
  143. img = ToBlackWhite(image[0].copy())
  144. img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  145. img = cv2.Canny(img,100,200)
  146. images[idx] = [img, image[1]]
  147.  
  148.  
  149.  
  150. # loop over the boundaries
  151. while True:
  152.  
  153.  
  154. ret, image = video_capture.read()
  155. h, w = image.shape[:2]
  156. image = cv2.resize(image, (int(w/4), int(h/4)))
  157. cv2.waitKey(1)
  158.  
  159. #processRoad(image.copy())
  160. #continue
  161.  
  162. image = ToBlackWhite(image.copy())
  163.  
  164.  
  165.  
  166. output = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
  167. output2 = cv2.Canny(output,100,200)
  168.  
  169.  
  170. im, contours,hierarchy = cv2.findContours(output2.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
  171. print len(contours)
  172. bigger_size = 0
  173. biggest_cnt = None
  174. for cnt in contours:
  175. x,y,w,h = cv2.boundingRect(cnt)
  176. if w*h > bigger_size:
  177. bigger_size = w*h
  178. biggest_cnt = cnt
  179. print "size",w*h,"pos",x,y
  180.  
  181. if biggest_cnt is not None:
  182. x,y,w,h = cv2.boundingRect(biggest_cnt)
  183. #cv2.rectangle(output2,(x-1,y-1),(x+w+2,y+h+2),(0,255,0),2)
  184.  
  185. cv2.imshow("preview", output2)
  186.  
  187. best_res = 0
  188. best_image = "noimage"
  189.  
  190. for image in images:
  191. res = calculate( output2[y: y+h,x: x+w], img)
  192. if res > best_res:
  193. best_image = image[1]
  194. best_res = res
  195.  
  196. print "best match", best_image
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement