import numpy as np import cv2 import math print("version openCV:"+str( cv2.__version__ )) debug_roi=True # Load a color image in color img = cv2.imread('imgs/symboles_s6.png',1) #img = cv2.imread('cartes2.png',1) print("img.dtype : "+ str(img.dtype)) print("img.shape : "+ str(img.shape)) #le premier scalaire est la hauteur de l'image if img is None: print("Fichier image foireux") exit() taille=20 orgv=6 orgu=88 hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) lower_black = np.array([0,0,0]) upper_black = np.array([180,255,90]) mask_black = cv2.inRange(hsv, lower_black, upper_black) cv2.imwrite('mask_black.png',mask_black) lower_red1 = np.array([0,150,150]) upper_red1 = np.array([15,255,255]) mask_red1 = cv2.inRange(hsv, lower_red1, upper_red1) lower_red2 = np.array([180-15,150,150]) upper_red2 = np.array([180,255,255]) mask_red2 = cv2.inRange(hsv, lower_red2, upper_red2) mask_red = cv2.bitwise_or(mask_red1,mask_red2) cv2.imwrite('mask_red.png',mask_red) mask=mask_red contours, hierarchy = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE) for indice,contour in enumerate(contours): print("The contours have this data: "+ str(contour) ) M = cv2.moments(contour) #print("M:" +str(M)) area = cv2.contourArea(contour) if area >15: #objet assez grand # Calcul du centroïde et de l'angle d'orientation (en degrés) cx = M['m10'] / M['m00'] cy = M['m01'] / M['m00'] print("cx: "+str(cx)) print("cy: "+str(cy)) angle_rad = 0.5 * np.arctan2(2 * M['mu11'], M['mu20'] - M['mu02']) angle_deg = np.degrees(angle_rad) # 3. Redressement de l'objet autour de son centroïde h, w = mask.shape rot_mat = cv2.getRotationMatrix2D((cx, cy), angle_deg, 1.0) #génère une image noire et y dessine l'objet correspondant uniquement au contours en cours mask_objet= np.zeros((h,w), dtype=np.uint8) cv2.drawContours(mask_objet,[contour],-1,255,-1) #cv2.imshow('image',mask_objet) #cv2.waitKey(0) aligned = cv2.warpAffine(mask_objet, rot_mat, (w, h), flags=cv2.INTER_NEAREST) # 4. Recadrage centré sur le centroïde redressé x, y, bw, bh = cv2.boundingRect(aligned) roi = aligned[y:y+bh, x:x+bw] if debug_roi: cv2.imshow('image',roi) cv2.waitKey(0) # 5. Comparaison avec les réflexions (1 = horizontal, 0 = vertical) flip_h = cv2.flip(roi, 1) flip_v = cv2.flip(roi, 0) def calculer_score_symetrie(img1, img2): #intersection = cv2.bitwise_and(img1, img2) #union = cv2.bitwise_or(img1, img2) difference = cv2.bitwise_xor(img1, img2) #cv2.imshow('image',difference) #cv2.waitKey(0) return np.sum(difference > 0) / np.sum(img1 > 0) score_h = calculer_score_symetrie(roi, flip_h) score_v = calculer_score_symetrie(roi, flip_v) print(f"Symétrie axe vertical (gauche/droite) : {score_h:.2%}") print(f"Symétrie axe horizontal (haut/bas) : {score_v:.2%}") if score_h>score_v: max_score=score_h min_score=score_v else: max_score=score_v min_score=score_h if max_score <0.60 : cv2.drawContours(img,[contour],-1,(0,255,255),1)#jaune else: if max_score/min_score<1.3: #double symetrie cv2.drawContours(img,[contour],-1,(0,255,0),1) #vert else: #simple symetrie cv2.drawContours(img,[contour],-1,(255,255,0),1) #cyan cv2.imwrite('imout.png',img) #cv2.imshow('image',img) #cv2.waitKey(0) cv2.destroyAllWindows() print("FINI!")