#!/usr/bin/python3 #B. Vandeportaele 31/12/2019 import cv2 as cv import numpy as np #https://docs.opencv.org/master/db/d5b/tutorial_py_mouse_handling.html #pour afficher la liste des évenements possibles #events = [i for i in dir(cv) if 'EVENT' in i] #print( events ) #homography: # https://docs.opencv.org/2.4/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html?highlight=findhomography # cv2.findHomography(srcPoints, dstPoints[, method[, ransacReprojThreshold[, mask]]]) → retval, mask #You can use only inliers (i.e. points with corresponding mask value equals to 1) when doing your match. ''' #https://stackoverflow.com/questions/50945385/python-opencv-findhomography-inputs if len(good) > MIN_MATCH_COUNT: src_pts = np.float32([kp1[m.queryIdx].pt for m in good]).reshape(-1, 1, 2) dst_pts = np.float32([kp2[m.trainIdx].pt for m in good]).reshape(-1, 1, 2) ''' #enregistrement et lecture fichiers #https://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html #https://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html if False: savePoints = np.float32((46, 82, 201, 30, 340, 85, 182, 144)).reshape(4,2) np.savetxt('test.out', savePoints, delimiter=',', fmt='%10.4f') filedata = np.genfromtxt('test.out', delimiter=',').astype('int32') print(filedata) exit() if False: #test pour estimer une homographie unité: srcPoints=np.float32( (1,2,1,4,5,9,7,8)).reshape(-1, 1, 2) dstPoints=srcPoints (retval, mask)= cv.findHomography(srcPoints, dstPoints) print("srcPoints") print(srcPoints) print("retval") print(retval) print("mask") print(mask) #rubikscube # pour l'apprentissage, donner les 4 coins de la face supérieure + 6 points pour apprendre les 6 couleurs de faces #éventuellement traiter les images en live pour pouvoir tourner le cube pendant l'apprentissage des couleurs image = cv.imread("rubiks.jpg", cv.IMREAD_UNCHANGED) if image is None: # ne pas utiliser == print("image non trouvee"); listeColors=["Blue","Red","White","Green","Orange","Yellow"] #listePositions=[(88,80),(188,125),(137,101),(198,44),(298,82),(192,84)] #apprentissage des couleurs sur la face top (pour le rouge) listePositions=[[88,80],[188,125],[137,101],[198,44],[298,82],[192,84]] #coordonée U puis V données par geeqie #apprentissage des couleurs sur la face front (pour le rouge) #listePositions=[[88,80],[210,243],[137,101],[198,44],[298,82],[192,84]] #coordonée U puis V données par geeqie listeRGB=[] print(listeColors) i=0 for color in listeColors: print(color) listeRGB.append( image[ listePositions[i][1],listePositions[i][0] ]) #coordonée V puis U i=i+1 print(listeRGB) #exit() #coordonnées obtenues avec geeqie,Affichage,infos sur le pixel #points dans le sens horaires for numface in range(0, 3): #trois faces pour débug #for numface in range(0, 1): #une seule face pour débug if numface==0: #https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html #srcPoints = np.float32((46,82,201,30,340,85,182,144)).reshape(-1, 1, 2) srcPoints = np.float32((46, 82, 201, 30, 340, 85, 182, 144)).reshape(4,2) elif numface==1: #srcPoints = np.float32((40,90,190,154,180,352,49,279)).reshape(-1, 1, 2) srcPoints = np.float32((40, 90, 190, 154, 180, 352, 49, 279)).reshape(4, 2) elif numface==2: #srcPoints = np.float32((186,152,347,92, 346,282,190,351)).reshape(-1, 1, 2) srcPoints = np.float32((186, 152, 347, 92, 346, 282, 190, 351)).reshape(4, 2) sizedest=30 dstPoints = np.float32((0,0,sizedest-1,0,sizedest-1,sizedest-1,0,sizedest-1)).reshape(-1, 1, 2) (H, mask) = cv.findHomography(srcPoints, dstPoints) print("srcPoints") print(srcPoints) print("retval") print(H) print("mask") print(mask) #fonction warpPespective: #https://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html?highlight=warpperspective#void%20warpPerspective(InputArray%20src,%20OutputArray%20dst,%20InputArray%20M,%20Size%20dsize,%20int%20flags,%20int%20borderMode,%20const%20Scalar&%20borderValue dst=cv.warpPerspective(image, H, (sizedest,sizedest)) cv.imwrite('rubiksrect'+str(numface)+'.png', dst) #recherche pour chaque pixel la couleur la plus proche for u in range(0,sizedest): for v in range(0, sizedest): val=dst[v,u] listfdiff=[] for c in range(0,6): #pour chaque couleur à tester diff=0 for comp in range(0,3): #pour chacune des composantes de la couleur #print(listeRGB[c][comp]) #print(val[comp]) #print((listeRGB[c][comp]) - (val[comp])) # RuntimeWarning: overflow encountered in ubyte_scalars #print(abs( listeRGB[c][comp]-val[comp]) ) diff = diff+abs( int(listeRGB[c][comp])-int(val[comp])) #cast en int #print("diff:" +str(diff)) listfdiff.append(diff) #print("listeRGB[c]" + str(listeRGB[c])+ str(val)+" diff= "+str(diff)) l=listfdiff.index(min(listfdiff)) #indice du plus petit élément de la liste de différence #print(u,v,val,listfdiff,l) #if listfdiff[l]<150: dst[v,u]=listeRGB[l] #else: # dst[v, u] = [0,0,0] cv.imwrite('rubiksrect'+str(numface)+'_label.png', dst) #https: // docs.opencv.org / trunk / df / d9d / tutorial_py_colorspaces.html if False: # Convert BGR to HSV hsv = cv.cvtColor(dst, cv.COLOR_BGR2HSV) cv.imwrite('rubiksrect_hsv'+str(numface)+'.png', hsv) #en HSV pour calculer distance entre couleur il faut tenir compte du fait qu'il y a modulo 2Pi sur la teinte (codé modulo 180) ''' ####################################################### # mouse callback function def draw_circle(event,x,y,flags,param): if event == cv.EVENT_LBUTTONDBLCLK: cv.circle(img,(x,y),100,(255,0,0),-1) ####################################################### def nothing(x): pass ####################################################### if False: # Create a black image, a window and bind the function to window img = np.zeros((512,512,3), np.uint8) cv.namedWindow('image') cv.setMouseCallback('image',draw_circle) while(1): cv.imshow('image',img) if cv.waitKey(20) & 0xFF == 32 : # barre espace pour sortir ? 27: #escape? break cv.destroyAllWindows() else: drawing = False # true if mouse is pressed mode = True # if True, draw rectangle. Press 'm' to toggle to curve ix,iy = -1,-1 # mouse callback function def draw_circle2(event,x,y,flags,param): global ix,iy,drawing,mode # get current positions of four trackbars r = cv.getTrackbarPos('R', 'image') g = cv.getTrackbarPos('G', 'image') b = cv.getTrackbarPos('B', 'image') #s = cv.getTrackbarPos(switch, 'image') if event == cv.EVENT_LBUTTONDOWN: drawing = True ix,iy = x,y elif event == cv.EVENT_MOUSEMOVE: if drawing == True: if mode == True: #cv.rectangle(img,(ix,iy),(x,y),(0,255,0),-1) cv.rectangle(img, (ix, iy), (x, y), (r,g,b), -1) else: cv.circle(img,(x,y),5,(0,0,255),-1) elif event == cv.EVENT_LBUTTONUP: drawing = False if mode == True: #cv.rectangle(img,(ix,iy),(x,y),(0,255,0),-1) cv.rectangle(img, (ix, iy), (x, y), (r, g, b), -1) else: cv.circle(img,(x,y),5,(0,0,255),-1) cv.line(img, (ix,iy), (x, y), (255, 255, 0), 5) img = np.zeros((512,512,3), np.uint8) cv.namedWindow('image') # create trackbars for color change cv.createTrackbar('R', 'image', 0, 255, nothing) cv.createTrackbar('G', 'image', 0, 255, nothing) cv.createTrackbar('B', 'image', 0, 255, nothing) cv.setTrackbarPos('R', 'image',255) cv.setMouseCallback('image',draw_circle2) while(1): cv.imshow('image',img) k = cv.waitKey(1) & 0xFF if k == ord('m'): mode = not mode elif k == ord('s'): cv.imwrite('dessin.png', img) elif k == 27: #escape? break cv.destroyAllWindows() '''