瀏覽代碼

end first day

jezvgg 11 月之前
父節點
當前提交
e48634413b
共有 5 個文件被更改,包括 92 次插入0 次删除
  1. 二進制
      project/board1.png
  2. 二進制
      project/board2.png
  3. 31 0
      project/cv.py
  4. 二進制
      project/image.png
  5. 61 0
      project/main.py

二進制
project/board1.png


二進制
project/board2.png


+ 31 - 0
project/cv.py

@@ -0,0 +1,31 @@
+import cv2
+import numpy as np
+
+def recognize(image):
+    board1 = cv2.imread("project/board1.png")
+    board2 = cv2.imread("project/board2.png")
+    _, thrash, t_min_loc1, t_max_loc = cv2.minMaxLoc(
+            cv2.matchTemplate(image, board1, cv2.TM_SQDIFF_NORMED)
+        )
+    _, thrash, t_min_loc2, t_max_loc = cv2.minMaxLoc(
+            cv2.matchTemplate(image, board2, cv2.TM_SQDIFF_NORMED)
+        )
+
+    image = image[t_min_loc1[1] + 20:t_min_loc2[1], t_min_loc1[0] + 20:t_min_loc2[0]]
+
+    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
+    hsv = cv2.GaussianBlur(hsv, (9, 9), 0)
+    _, thresh = cv2.threshold(hsv[:, :, 1], 27, 255, cv2.THRESH_BINARY)
+    cnts, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
+
+    polys = []
+    for cont in cnts:
+        eps = 0.044 * cv2.arcLength(cont, True)
+        rect = cv2.minAreaRect(cont)
+        box = cv2.boxPoints(rect)
+        box = np.intp(box)
+        approx = cv2.approxPolyDP(box, eps, True)
+        approx = approx[:, 0, :].tolist()
+        polys.append(approx)
+
+    return polys, (t_min_loc2[0] + 20, t_min_loc2[1] + 20)

二進制
project/image.png


+ 61 - 0
project/main.py

@@ -0,0 +1,61 @@
+import pygame
+import pymunk.pygame_util
+from cv import recognize
+import cv2
+
+image = cv2.imread('project/image.png')
+polys, shape = recognize(image)
+
+print(shape)
+RES = WIDTH, HEIGHT = shape[0], shape[1]
+FPS = 60
+
+pygame.init()
+
+surface = pygame.display.set_mode(RES)
+clock = pygame.time.Clock()
+
+draw_options = pymunk.pygame_util.DrawOptions(surface)
+space = pymunk.Space()
+space.gravity = 0, 2000
+
+def create_ball(space, pos: tuple[int ,int]):
+    ball_mass, ball_radius = 10, 5
+    ball_moment = pymunk.moment_for_circle(ball_mass, 0, ball_radius)
+    ball_body = pymunk.Body(ball_mass, ball_moment)
+    ball_body.position = pos
+    ball_shape = pymunk.Circle(ball_body, ball_radius)
+    ball_shape.elasticity = 0.2
+    space.add(ball_body, ball_shape)
+
+def draw_by_lines(space, lines=list[tuple[int, int]]):
+    for line_i in range(len(lines)):
+        line = pymunk.Segment(space.static_body, lines[line_i], lines[(line_i+1)%4], 2)
+        line.elasticity = 0.1
+        space.add(line)
+
+for poly in polys:
+    draw_by_lines(space, poly)
+
+circle_position = (350, 20)
+
+while True:
+    surface.fill(pygame.Color('white'))
+
+    pygame.draw.circle(surface, pygame.Color('BLUE'), circle_position, 5)
+
+    for i in pygame.event.get():
+        if i.type == pygame.KEYDOWN:
+            if i.key == pygame.K_SPACE:
+                create_ball(space, circle_position)
+            if i.key == pygame.K_LEFT:
+                circle_position = (circle_position[0] + 10, circle_position[1])
+            if i.key == pygame.K_RIGHT:
+                circle_position = (circle_position[0] - 10, circle_position[1])
+        if i.type == pygame.QUIT:
+            exit()
+
+    space.step(1 / FPS)
+    space.debug_draw(draw_options)
+    pygame.display.flip()
+    clock.tick(FPS)