Skip to content
Snippets Groups Projects
Commit b7128b84 authored by benjamin.sitbon's avatar benjamin.sitbon
Browse files

Un peu d'ordre dans le code

parent fc34dcd3
No related branches found
No related tags found
No related merge requests found
......@@ -4,12 +4,18 @@ import cv2
import time
import numpy as np
import matplotlib.pyplot as plt
import cmath as math
#Variables utile pour trouver le centre de nos legos
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", help="path to the video file")
ap.add_argument("-a", "--min-area", type=int, default=500, help="minimum area size")
args = vars(ap.parse_args())
def nothing(x):
pass
def calc_angle(x1, y1, x2, y2):
def angle(x1, y1, x2, y2):
if x2 - x1 == 0:
return 0
......@@ -21,48 +27,71 @@ def find_center(mask):
cnts= cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cx = 0
cy = 0
x = 0
y = 0
for c in cnts:
if cv2.contourArea(c) > args["min_area"]:
M = cv2.moments(c)
cx = int(M["m10"] / M["m00"])
cy = int(M["m01"] / M["m00"])
x = int(M["m10"] / M["m00"])
y = int(M["m01"] / M["m00"])
return cx, cy
return x, y
cap = cv2.VideoCapture('wheel_racing.avi') # video
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", help="path to the video file")
ap.add_argument("-a", "--min-area", type=int, default=500, help="minimum area size")
args = vars(ap.parse_args())
angles = np.array([])
temps = np.array([])
count = 0
cv2.namedWindow('red')
cv2.namedWindow('orange')
cv2.namedWindow('main')
def Red_Trackbars():
cv2.createTrackbar('red_h_min','red',0,255,nothing)
cv2.setTrackbarPos('red_h_min','red',170)
cv2.createTrackbar('red_s_min','red',0,255,nothing)
cv2.setTrackbarPos('red_s_min','red',145)
cv2.createTrackbar('red_v_min','red',0,255,nothing)
cv2.setTrackbarPos('red_v_min','red',220)
cv2.createTrackbar('red_h_max','red',0,255,nothing)
cv2.setTrackbarPos('red_h_max','red',179)
cv2.createTrackbar('red_s_max','red',0,255,nothing)
cv2.setTrackbarPos('red_s_max','red',255)
cv2.createTrackbar('red_v_max','red',0,255,nothing)
cv2.setTrackbarPos('red_v_max','red',255)
def Orange_Trackbars():
cv2.createTrackbar('orange_h_min','orange',0,255,nothing)
cv2.setTrackbarPos('orange_h_min','orange',10)
cv2.createTrackbar('orange_s_min','orange',0,255,nothing)
cv2.setTrackbarPos('orange_s_min','orange',120)
cv2.createTrackbar('orange_v_min','orange',0,255,nothing)
cv2.setTrackbarPos('orange_v_min','orange',240)
cv2.createTrackbar('orange_h_max','orange',0,255,nothing)
cv2.setTrackbarPos('orange_h_max','orange',35)
cv2.createTrackbar('orange_s_max','orange',0,255,nothing)
cv2.setTrackbarPos('orange_s_max','orange',255)
cv2.createTrackbar('orange_v_max','orange',0,255,nothing)
cv2.setTrackbarPos('orange_v_max','orange',255)
cap = cv2.VideoCapture('wheel_racing.avi') # video
tab_ang = np.array([])
sec = np.array([])
count = 0
cv2.namedWindow('red')
cv2.namedWindow('orange')
cv2.namedWindow('main')
Red_Trackbars()
Orange_Trackbars()
while True:
......@@ -106,17 +135,17 @@ while True:
kernel = np.ones((5,5), np.uint8)
# rempli les trou dans les objets
mask_red = cv2.morphologyEx(RedMask, cv2.MORPH_CLOSE, kernel)
mask_orange = cv2.morphologyEx(OrangeMask, cv2.MORPH_CLOSE, kernel)
#Il arrivait d'avoir des zones noir dans le carré, j'ai donc tenté de remplir avec une fermeture
RedMask = cv2.morphologyEx(RedMask, cv2.MORPH_CLOSE, kernel)
OrangeMask = cv2.morphologyEx(OrangeMask, cv2.MORPH_CLOSE, kernel)
x1, y1 = find_center(mask_red)
x2, y2 = find_center(mask_orange)
x1, y1 = find_center(RedMask)
x2, y2 = find_center(OrangeMask)
angle = calc_angle(x1, y1, x2, y2)
ang = angle(x1, y1, x2, y2)
angles = np.append(angles, angle)
temps = np.append(temps, count)
tab_ang = np.append(tab_ang, ang)
sec = np.append(sec, count)
count += 1
cv2.imshow('main',mask)
......@@ -139,6 +168,6 @@ while True:
cap.release()
cv2.destroyAllWindows()
plt.plot(temps, angles, 'g')
plt.plot(sec, tab_ang, 'g')
plt.yscale('linear')
plt.show()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment