【发布时间】:2023-01-31 01:20:47
【问题描述】:
但是无法让它检测到正确的部分。这件作品是 59x83。它应该被检测到,但没有。 我想我在这里遗漏了什么?
import cv2
import numpy as np
# Load the chess board and chess piece images
img_board = cv2.imread('ccom.png')
img_piece = cv2.imread('bbis.png')
# Convert both images to grayscale
img_board_gray = cv2.cvtColor(img_board, cv2.COLOR_BGR2GRAY)
img_piece_gray = cv2.cvtColor(img_piece, cv2.COLOR_BGR2GRAY)
# Apply morphological operations to extract the chess piece from the board
kernel = np.ones((5, 5), np.uint8)
img_piece_mask = cv2.erode(img_piece_gray, kernel, iterations=1)
img_piece_mask = cv2.dilate(img_piece_mask, kernel, iterations=1)
# Find the matching location on the board
result = cv2.matchTemplate(img_board_gray, img_piece_mask, cv2.TM_SQDIFF)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
# Draw a rectangle around the matching location
top_left = min_loc
bottom_right = (top_left[0] + img_piece.shape[1], top_left[1] + img_piece.shape[0])
cv2.rectangle(img_board, top_left, bottom_right, (0, 0, 255), 2)
# Show the result
cv2.imshow('Result', img_board)
cv2.waitKey(0)
cv2.destroyAllWindows()
【问题讨论】:
-
相同颜色的棋子应该只匹配还是两种颜色的棋子都匹配?
-
只有相同的颜色