Commit ba008ae7 authored by William Michalski's avatar William Michalski
Browse files

"Ajout d'une fonction de detection du centre par corrélation, avec les têtes de vis"

parent 4065d34b
......@@ -24,7 +24,7 @@ def centroid(img):
cX, cY = 0, 0
return cX, cY
img = cv2.imread("labyTest.jpg")
img = cv2.imread("test_continuous.jpg")
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)#Espace HSV, plus simple de separer les couleurs
#Paramètre du seuillage par couleur, à adapter au besoin
......
progra/Detection_bille/testLaby_res.jpg

2.19 MB | W: | H:

progra/Detection_bille/testLaby_res.jpg

38.2 KB | W: | H:

progra/Detection_bille/testLaby_res.jpg
progra/Detection_bille/testLaby_res.jpg
progra/Detection_bille/testLaby_res.jpg
progra/Detection_bille/testLaby_res.jpg
  • 2-up
  • Swipe
  • Onion skin
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 18 16:20:04 2021
@author: william
@ref :
https://www.geeksforgeeks.org/multiple-color-detection-in-real-time-using-python-opencv/
https://learnopencv.com/find-center-of-blob-centroid-using-opencv-cpp-python/
https://docs.opencv.org/master/d9/d61/tutorial_py_morphological_ops.html
"""
import numpy as np
import cv2
import matplotlib
import matplotlib.pyplot as plt
from math import sqrt
def centroid(img):
M = cv2.moments(img)
if M["m00"] != 0:
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
else:
cX, cY = 0, 0
return cX, cY
img = cv2.imread("test_continuous.jpg")
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
mask = cv2.imread("mask2.png")
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
w, h = mask.shape
res = cv2.matchTemplate(img, mask, cv2.TM_CCOEFF_NORMED)
_,_,_,(Ax,Ay) = cv2.minMaxLoc(res)
cv2.circle(res, (Ax,Ay), 4, (0,0,0),-1)
_,_,_,(Bx,By) = cv2.minMaxLoc(res)
(Ax,Ay) = (Ax+h/2,Ay+w/2)
(Bx,By) = (Bx+w/2,By+h/2)
AB=sqrt((Ax-Bx)**2+(Ay-By)**2)
if(Ay<By and Bx>Ax):
Cx,Cy = (int(Ay+AB/sqrt(2)),int(Bx-AB/sqrt(2)))
elif(By<Ay and Ax>Bx):
Cx,Cy = (int(By+AB/sqrt(2)),int(Ax-AB/sqrt(2)))
else:
print("Erreur lors de la détection des vis")
raise
(Ax,Ay) = (int(Ax),int(Ay))
(Bx,By) = (int(Bx),int(By))
cv2.circle(img, (Ax,Ay), 4, (0,233,255),-1)
cv2.circle(img, (Bx,By), 4, (0,233,255),-1)
cv2.circle(img, (Cx,Cy), 4, (0,233,255),-1)
plt.figure(1)
plt.subplot(1,2,1),plt.title("img"), plt.imshow(img)
plt.subplot(1,2,2),plt.title("corr"), plt.imshow(res)
cv2.imwrite("testCentre_res.jpg",img)
\ No newline at end of file
......@@ -12,7 +12,7 @@ https://docs.opencv.org/master/d9/d61/tutorial_py_morphological_ops.html
"""
import numpy as np
import cv2
import matplotlib.pyplot as plt
from math import sqrt
#Rouge
BALL_LOWER = np.array([136, 50, 50], np.uint8)
......@@ -26,10 +26,6 @@ SCREWS_UPPER = np.array([120, 255, 255], np.uint8)
FINISH_LOWER = np.array([25, 52, 72], np.uint8)
FINISH_UPPER = np.array([102, 255, 255], np.uint8)
TRAY_SIZE = 10#Longueur de cote du plateau (en pixels)
SCREWS_OFFSET = 1#Distance entre bord du plateau et les vis (en pixels)
MAZE_OFFSET = 1#Distance entre vis et bord du labyrinthe (en pixels, approximative, mais non majoree !)
CONTOURS_THICKNESS = 40#Epaisseur des contours de securite
def centroid(img):#Detection du barycentre d'une image binaire
......@@ -50,10 +46,30 @@ def detect_ball_centroid(img):
img_ball = select_img_canal(img, BALL_LOWER, BALL_UPPER)#A priori pas besoin d'opération morphologique, on distingue bien
return centroid(img_ball)
def detect_tray_center(img):
img_center = select_img_canal(img, SCREWS_LOWER, SCREWS_UPPER)#A voir si besoin d'un d'opération morpho
dx, dy = centroid(img_center)
return (int(dx - TRAY_SIZE/4 + SCREWS_OFFSET/2) , int(dy - TRAY_SIZE/4 + SCREWS_OFFSET/2))
#Detection du centre par correlation entre une image du plateau et une image d'une tete de vis (servant de masque)
def detect_tray_center(img,mask):
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
mask_gray = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
w, h = mask_gray.shape
corr = cv2.matchTemplate(img_gray, mask_gray, cv2.TM_CCOEFF_NORMED)
_,_,_,(Ax,Ay) = cv2.minMaxLoc(corr)#Detection du pixel le plus ressemblant au masque
cv2.circle(corr, (Ax,Ay), 4, (0,0,0),-1)
_,_,_,(Bx,By) = cv2.minMaxLoc(corr)#Dectection du second pixel
(Ax,Ay) = (Ax+h//2,Ay+w//2)#Recentrage dans l'image d'origine
(Bx,By) = (Bx+w//2,By+h//2)
#Calcul du centre du plateau C, connaissant les positions des vis A et B
AB=sqrt((Ax-Bx)**2+(Ay-By)**2)
if(Ay<By and Bx>Ax):
C = (int(Ay+AB/sqrt(2)),int(Bx-AB/sqrt(2)))
elif(By<Ay and Ax>Bx):
C = (int(By+AB/sqrt(2)),int(Ax-AB/sqrt(2)))
else:
print("Erreur lors de la détection des vis")
raise
return (Ax,Ay), (Bx,By), C
def clean_maze_img(img, cx, cy):
......
......@@ -53,7 +53,7 @@ def solve_maze(img, ball_position, finish_position):
#%% Test
#import matplotlib.pyplot as plt
#
#img = cv2.imread("testLaby_bin.jpg")
#img = cv2.imread("Images/testLaby_bin.jpg")
#img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#l,c = img.shape
#img = cv2.resize(img,(c//20,l//20))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment