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

Modif fonctionnement detection centre

parent 15e47db7
......@@ -27,34 +27,40 @@ def centroid(img):
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)
mask1 = cv2.imread("mask1.png")
mask2 = cv2.imread("mask2.png")
plt.figure(1)
mask = cv2.cvtColor(mask1, cv2.COLOR_BGR2GRAY)
w, h = mask.shape
img_masked = cv2.matchTemplate(img, mask, cv2.TM_CCOEFF_NORMED)
plt.subplot(1,3,2),plt.title("corr1"), plt.imshow(img_masked)
_,_,_,(Ax,Ay) = cv2.minMaxLoc(img_masked)
(Ax,Ay) = (Ax+h//2,Ay+w//2)
mask = cv2.cvtColor(mask2, cv2.COLOR_BGR2GRAY)
w, h = mask.shape
img_masked = cv2.matchTemplate(img, mask, cv2.TM_CCOEFF_NORMED)
plt.subplot(1,3,3),plt.title("corr2"), plt.imshow(img_masked)
_,_,_,(Bx,By) = cv2.minMaxLoc(img_masked)
(Bx,By) = (Bx+w//2,By+h//2)
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)))
Cx,Cy = (Ax,By)
elif(By<Ay and Ax>Bx):
Cx,Cy = (int(By+AB/sqrt(2)),int(Ax-AB/sqrt(2)))
Cx,Cy = (Bx, Ay)
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)
plt.subplot(1,3,1),plt.title("img"), plt.imshow(img)
cv2.imwrite("testCentre_res.jpg",img)
\ No newline at end of file
progra/Detection_vis/testCentre_res.jpg

34.9 KB | W: | H:

progra/Detection_vis/testCentre_res.jpg

34.9 KB | W: | H:

progra/Detection_vis/testCentre_res.jpg
progra/Detection_vis/testCentre_res.jpg
progra/Detection_vis/testCentre_res.jpg
progra/Detection_vis/testCentre_res.jpg
  • 2-up
  • Swipe
  • Onion skin
......@@ -13,6 +13,7 @@ https://docs.opencv.org/master/d9/d61/tutorial_py_morphological_ops.html
import numpy as np
import cv2
from math import sqrt
import matplotlib.pyplot as plt
#Rouge
BALL_LOWER = np.array([136, 50, 50], np.uint8)
......@@ -28,6 +29,9 @@ FINISH_UPPER = np.array([102, 255, 255], np.uint8)
CONTOURS_THICKNESS = 40#Epaisseur des contours de securite
MASK1 = cv2.imread("Images/mask1.png")
MASK2 = cv2.imread("Images/mask2.png")
def centroid(img):#Detection du barycentre d'une image binaire
M = cv2.moments(img)
if M["m00"] != 0:
......@@ -47,29 +51,29 @@ def detect_ball_centroid(img):
return centroid(img_ball)
#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
def detect_tray_center(img):
mask = cv2.cvtColor(MASK1, cv2.COLOR_BGR2GRAY)
w, h = mask.shape
img_masked = cv2.matchTemplate(img, mask, cv2.TM_CCOEFF_NORMED)
_,_,_,(Ax,Ay) = cv2.minMaxLoc(img_masked)
(Ax,Ay) = (Ax+h//2,Ay+w//2)
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
mask = cv2.cvtColor(MASK2, cv2.COLOR_BGR2GRAY)
w, h = mask.shape
img_masked = cv2.matchTemplate(img, mask, cv2.TM_CCOEFF_NORMED)
_,_,_,(Bx,By) = cv2.minMaxLoc(img_masked)
(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)))
Cx,Cy = (Ax,By)
elif(By<Ay and Ax>Bx):
C = (int(By+AB/sqrt(2)),int(Ax-AB/sqrt(2)))
Cx,Cy = (Bx, Ay)
else:
print("Erreur lors de la détection des vis")
raise
return (Ax,Ay), (Bx,By), C
return (Ax,Ay), (Bx,By), (Cx,Cy)
def clean_maze_img(img, cx, cy):
......
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