Browse Source

melhorando

lego 6 years ago
parent
commit
442bb8f195
5 changed files with 68 additions and 53 deletions
  1. 52 40
      cmarkers.py
  2. 2 2
      player-camera.py
  3. 7 5
      player-lejos.py
  4. 2 2
      player-virtual.py
  5. 5 4
      server.py

+ 52 - 40
cmarkers.py

@@ -8,7 +8,7 @@ def getCMarkers (img):
     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
     img3 = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 125, 10)
 
-    kernel = np.ones((3,3),np.uint8)
+    kernel = np.ones((6,6),np.uint8)
     img2 = cv2.morphologyEx(img3, cv2.MORPH_CLOSE, kernel)
 
     # kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
@@ -16,7 +16,6 @@ def getCMarkers (img):
 
     kernel = np.ones((3,3),np.uint8)
     img2 = cv2.dilate(img2,kernel, 2)
-    cv2.imshow("m2", img2)
 
     # Setup SimpleBlobDetector parameters.
     params = cv2.SimpleBlobDetector_Params()
@@ -51,12 +50,12 @@ def getCMarkers (img):
 
     # Detect blobs.
     keypoints = detector.detect(img2)
-    # img2 = cv2.drawKeypoints(img2, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
 
     # Draw detected blobs as red circles.
     # cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures
     # the size of the circle corresponds to the size of blob
     k = []
+    kk = [] 
     for point  in keypoints:
         count = []
         for p  in keypoints:
@@ -66,11 +65,16 @@ def getCMarkers (img):
                 count.append(p.pt)
         if len(count) >= 2:
             k.append((point.pt, count))
+            kk.append(point)
+
+
+    img2 = cv2.drawKeypoints(img2, kk, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
+    cv2.imshow("m3", img2)
 
     for point in k:
         p, near = point
         # distance open the angre and 90 degree
-        midistance = math.pi/12.5
+        midistance = math.pi/10.0
         bottom = [] 
         rigth = [] 
         for p1 in near:
@@ -81,16 +85,18 @@ def getCMarkers (img):
                 v = np.array([p2[0]-p[0], p2[1]-p[1]])
                 c = np.dot(u,v)/norm(u)/norm(v)
                 angle = np.arccos(c)
-                if abs(angle-math.pi/2.0) < midistance:
+                if abs(angle) > midistance:
                     bottom = p1
                     rigth = p2
                     midistance = abs(angle-math.pi/2.0)
 
-        if midistance == math.pi/12.5:
+        if midistance == math.pi/10:
             continue
 
         u = np.array([bottom[0]-p[0], bottom[1]-p[1]])
         v = np.array([rigth[0]-p[0], rigth[1]-p[1]])
+        if u[1] > v[1]:
+            u,v = v,u
         conner = rigth+u
         addu = u*1.0/6.0
         addv = v*1.0/6.0
@@ -98,41 +104,11 @@ def getCMarkers (img):
         trans = get_transform_matrix_points(conners, [10, 10], 10)
         code = cv2.warpPerspective(gray, trans, dsize=(100, 100))
         # code = cv2.erode(code, kernel,iterations = 1)
-        # cv2.imshow("m2", code)
-
-        vsplit = np.vsplit(code, 4)
-        mean = []
-        for vs in vsplit:
-            m = []
-            hsplit = np.hsplit(vs, 4)
-            for hs in hsplit:
-                m.append(np.mean(hs))
-            mean.append(m)
-        # print(np.array(mean).astype(np.uint8))
-        mean = np.array(mean) >= 100.0
-        valid = mean[0, 0] == False
-        valid = valid and mean[0, 3] == False
-        valid = valid and mean[0, 3] == False
-        valid = valid and mean[1, 0] == True
-        valid = valid and mean[0, 1] == True
-        valid = valid and mean[2, 0] == True
-        valid = valid and mean[0, 2] == True
-        valid = valid and mean[3, 3] == True
-        valid = valid and mean[1, 3] == True
-        valid = valid and mean[3, 1] == True
-        valid = valid and mean[2, 3] == True
-        valid = valid and mean[3, 2] == True
-        if valid == False:
+        cv2.imshow("m2", code)
+
+        number = getNumber(code, 150)
+        if number == False:
             continue
-        number  = 0
-        if not mean[1, 1]:
-            number += 1
-        if not mean[1, 2]:
-            number += 2
-        if not mean[2, 1]:
-            number += 4
-        if not mean[2, 2]:
-            number += 8
 
         uu = np.array([0, 1])
         vv = np.array([p[0]-bottom[0], p[1]-bottom[1]])
@@ -143,6 +119,42 @@ def getCMarkers (img):
             markers.append([number, mid, angle])
     return markers
 
+def getNumber(img, threshold):
+    vsplit = np.vsplit(img, 4)
+    mean = []
+    for vs in vsplit:
+        m = []
+        hsplit = np.hsplit(vs, 4)
+        for hs in hsplit:
+            m.append(np.mean(hs))
+        mean.append(m)
+    # print(np.array(mean).astype(np.uint8))
+    # print(mean)
+    mean = np.array(mean) >= threshold
+    valid = mean[0, 0] == False
+    valid = valid and mean[0, 3] == False
+    valid = valid and mean[0, 3] == False
+    valid = valid and mean[1, 0] == True
+    valid = valid and mean[0, 1] == True
+    valid = valid and mean[2, 0] == True
+    valid = valid and mean[0, 2] == True
+    valid = valid and mean[3, 3] == True
+    valid = valid and mean[1, 3] == True
+    valid = valid and mean[3, 1] == True
+    valid = valid and mean[2, 3] == True
+    valid = valid and mean[3, 2] == True
+    if valid == False:
+        return False
+    number  = 0
+    if not mean[1, 1]:
+        number += 1
+    if not mean[1, 2]:
+        number += 2
+    if not mean[2, 1]:
+        number += 4
+    if not mean[2, 2]:
+        number += 8
+    return number
 
 def get_transform_matrix_points(corners, board_size, dpi):
     # Read a frame from the video device

+ 2 - 2
player-camera.py

@@ -47,12 +47,12 @@ while not client.quit:
     # verifica se ha posicoes novas
     newposs = client.getPosition()
     for info in newposs:
-        posx, posy, course, idd, xsize, ysize, xtag, ytag = info
+        posx, posy, courser, idd, xsize, ysize, xtag, ytag = info
         if idd != rid:
             players[idd] = [posx, posy]
         else:
             # atualiza posicao
-            px, py, course = posx, posy, course
+            px, py, course = posx, posy, courser
 
     tag, interval = client.getTagInfo()  
     pos = [players[p] for p in players]

+ 7 - 5
player-lejos.py

@@ -19,7 +19,7 @@ s.connect((serverMACAddress, port))
 #sock.close()
 
 
-rid = randint(100, 1000)
+rid = 2
 run = False
 board = None
 tagplayer = -1
@@ -61,9 +61,11 @@ while not client.quit:
     # verifica se ha posicoes novas
     newposs = client.getPosition()
     for info in newposs:
-        posx, posy, course, idd, xsize, ysize, xtag, ytag = info
+        posx, posy, courser, idd, xsize, ysize, xtag, ytag = info
         if idd != rid:
             players[idd] = [posx, posy]
+        else:
+            px, py, course = posx, posy, courser
 
     tag, interval = client.getTagInfo()  
     pos = [players[p] for p in players]
@@ -80,7 +82,7 @@ while not client.quit:
             dot = x1*x2 + y1*y2      # dot product between [x1, y1] and [x2, y2]
             det = x1*y2 - y1*x2      # determinant
             angle = math.atan2(det, dot)
-	    if angle < 0.3 or angle > math.pi-0.3:
+            if angle < 0.3 or angle > math.pi-0.3:
                 s.send(bytes([1]))
             elif angle > math.pi:
                 s.send(bytes([3]))
@@ -98,7 +100,7 @@ while not client.quit:
             dot = x1*x2 + y1*y2      # dot product between [x1, y1] and [x2, y2]
             det = x1*y2 - y1*x2      # determinant
             angle = math.atan2(det, dot)
-	    if angle < 0.3 or angle > math.pi-0.3:
+            if (angle < 0.3) or (angle > math.pi-0.3):
                 s.send(bytes([1]))
             elif angle > math.pi:
                 s.send(bytes([3]))
@@ -114,7 +116,7 @@ while not client.quit:
             dot = x1*x2 + y1*y2      # dot product between [x1, y1] and [x2, y2]
             det = x1*y2 - y1*x2      # determinant
             angle = math.atan2(det, dot)
-	    if angle < 0.3 or angle > math.pi-0.3:
+            if angle < 0.3 or angle > math.pi-0.3:
                 s.send(bytes([1]))
             elif angle > math.pi:
                 s.send(bytes([3]))

+ 2 - 2
player-virtual.py

@@ -61,8 +61,8 @@ while not client.quit:
         # comportamento de fuga
         if tag in players and tag != rid:
             # foge se nao for pegador 
-            px = px + (px-players[tag][0])/90.0
-            py = py + (py-players[tag][1])/90.0
+            px = px + (px-players[tag][0])/900.0
+            py = py + (py-players[tag][1])/900.0
 
             course = course+(random()-0.5)/5.0
             client.setPosition(px, py, course)

+ 5 - 4
server.py

@@ -152,7 +152,7 @@ def get_green_dots(img_orig):
 ### Calibration Example ###
 if __name__ == "__main__":
     parser = argparse.ArgumentParser()
-    cam_id = 1
+    cam_id = 0
     dev = None
     
     parser.add_argument('-a',
@@ -176,7 +176,7 @@ if __name__ == "__main__":
  
     # The size of the board in inches, measured between the two
     # robot boundaries:
-    board_size = [84, 84]
+    board_size = [90, 90]
     point = []
  
     # Number of pixels to display per inch in the final transformed image. This
@@ -189,7 +189,8 @@ if __name__ == "__main__":
     # Calculate the perspective transform matrix
     transform = None
     if manual and camera:
-        transform = get_transform_matrix(dev, board_size, dpi)
+        pass
+        #transform = get_transform_matrix(dev, board_size, dpi)
         # transform = get_transform_matrix_points([(327, 75), (280, 475), (805, 141), (805, 568)], board_size, dpi)
 
     server = tag.TagServer(10318, board_size)
@@ -209,7 +210,7 @@ if __name__ == "__main__":
                     centers = get_green_dots(img_orig)
                     transform = get_transform_matrix_points(centers, board_size, dpi)
 
-                img = cv2.warpPerspective(img_orig, transform, dsize=transform_size)
+                img = img_orig
                 markers = getCMarkers(img)
 
                 for i in markers: