Commit e5aee48e949c4abed201855ab98d428d5e5451a7

Authored by Justine
1 parent 0ec8f11a

ajout du programme pour trouver la direction

Showing 1 changed file with 97 additions and 0 deletions   Show diff stats
Python/Final/Direction.py 0 → 100644
... ... @@ -0,0 +1,97 @@
  1 +import cv2
  2 +import numpy as np
  3 +import math
  4 +import imutils
  5 +import matplotlib.pyplot as plt
  6 +
  7 +def FindContours(img):
  8 + imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
  9 + ret,thresh = cv2.threshold(imgray,127,255,0)
  10 + _,contours,hierarchy = cv2.findContours(thresh, 1, 2)
  11 + return contours
  12 +
  13 +def EnhanceBrightness(frame, n):
  14 + imgHLS = cv2.cvtColor(frame, cv2.COLOR_BGR2HLS)
  15 + imgHLS[:,:,1] += n
  16 + imgLight = cv2.cvtColor(imgHLS, cv2.COLOR_HLS2BGR)
  17 + return imgLight
  18 +
  19 +def Filter(lightFrame):
  20 + kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
  21 + img = cv2.filter2D(lightFrame, -1, kernel)
  22 + filtered = cv2.bilateralFilter(img, 5, 150, 150)
  23 + return filtered
  24 +
  25 +def FindArrow(frame):
  26 + size = 0
  27 + height, width, channels = frame.shape
  28 + mask = np.zeros((height, width, 3), dtype = "uint8")
  29 + contours = FindContours(frame)
  30 + for cnt in contours:
  31 + tmp = cv2.contourArea(cnt)
  32 + approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True)
  33 + if len(approx) > 6 and len(approx) < 9 and tmp > 1000 and tmp < 20000:
  34 + cv2.drawContours(mask, [cnt], 0, (255,255,255), -1)
  35 + size = tmp
  36 + arrows = cv2.bitwise_and(frame, mask)
  37 + return size, arrows, mask
  38 +
  39 +def FindOrientation(imgArrow):
  40 + x = 0
  41 + y = 0
  42 + w = 0
  43 + h = 0
  44 + contours = FindContours(imgArrow)
  45 + for cnt in contours:
  46 + tmp = cv2.contourArea(cnt)
  47 + M = cv2.moments(cnt)
  48 + if tmp > 1000 :
  49 + x,y,w,h = cv2.boundingRect(cnt)
  50 + if x != 0:
  51 + verticale = math.sqrt(math.pow((x-x), 2)+math.pow((y-(y+h)), 2))
  52 + horizontale = math.sqrt(math.pow((x-(x+w)),2)+math.pow((y-y),2))
  53 + centerCoord = (x + verticale/2, (x+w)+(horizontale/2))
  54 +
  55 + extLeft, extRight, extTop, extBot = FindExtremPoints(imgArrow)
  56 + droite = math.sqrt(math.pow((extTop[0] - extRight[0]), 2)+math.pow((extTop[1] - extRight[1]), 2))
  57 + gauche = math.sqrt(math.pow((extLeft[0] - extTop[0]), 2)+math.pow((extLeft[1] - extTop[1]), 2))
  58 + haut = math.sqrt(math.pow((extTop[0] - extRight[0]), 2)+math.pow((extTop[1] - extRight[1]), 2))
  59 + bas = math.sqrt(math.pow((extRight[0] - extBot[0]), 2)+math.pow((extRight[1] - extBot[1]), 2))
  60 +
  61 + if (horizontale > verticale) and (droite < gauche):
  62 + print("droite")
  63 + return 1
  64 + elif (horizontale > verticale) and (gauche < droite):
  65 + print("gauche")
  66 + return 2
  67 + elif (verticale > horizontale) and (haut < bas):
  68 + print ("haut")
  69 + return 3
  70 + elif (verticale > horizontale) and (bas < haut):
  71 + print ("bas")
  72 + return 4
  73 + else :
  74 + print ("NULL")
  75 + return -1
  76 + else :
  77 + return -1
  78 +
  79 +def FindExtremPoints(imgArrow):
  80 + imgray = cv2.cvtColor(imgArrow,cv2.COLOR_BGR2GRAY)
  81 + ret,thresh = cv2.threshold(imgray,127,255,0)
  82 + cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  83 + cnts = imutils.grab_contours(cnts)
  84 + c = max(cnts, key=cv2.contourArea)
  85 + extLeft = tuple(c[c[:, :, 0].argmin()][0])
  86 + extRight = tuple(c[c[:, :, 0].argmax()][0])
  87 + extTop = tuple(c[c[:, :, 1].argmin()][0])
  88 + extBot = tuple(c[c[:, :, 1].argmax()][0])
  89 + return extLeft, extRight, extTop, extBot
  90 +
  91 +def Direction(frame) :
  92 + imgLight = EnhanceBrightness(frame, 3)
  93 + imgFiltered = Filter(imgLight)
  94 + imgFiltered = EnhanceBrightness(imgFiltered, 2)
  95 + size, imgArrows, mask = FindArrow(imgFiltered)
  96 + orientation = FindOrientation(imgArrows)
  97 + return orientation
... ...