import cv2 import numpy as np LOWER_GREEN = np.array([35, 40, 30]) UPPER_GREEN = np.array([90, 255, 255]) class ZoneExtractor: """ Klasa odpowiadajaca za wyciecie z obrazu poszczegolnych miejsc zainteresowania. Zwraca Liste o kluczach: community, player_top, playter_right, player_bottom. """ def __init__(self): pass def get_green_mask(self, image): """ Funkcja znajduje plansze do gry. Poprzez progowanie obrazu na podstawie koloru zielonego. Zwraca sama plansze. """ image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) mask = cv2.inRange(image_hsv, LOWER_GREEN, UPPER_GREEN) return cv2.morphologyEx(mask, cv2.MORPH_CLOSE, np.ones((7, 7), np.uint8)) def findTable(self, image): clean_mask = self.get_green_mask(image) ys, xs = np.where(clean_mask == 255) y_min, y_max = np.percentile(ys, [4, 97]).astype(int) x_min, x_max = np.percentile(xs, [4, 97]).astype(int) table = image[y_min:y_max, x_min:x_max] return table def extract(self, table): """ Korzystajac z segmentacji obrazu otrzymujemy obiekty ktore pozniej na podstawie wspolrzednych przypisujemy do konkretnego gracza. :param table: wycieta plansza gry :return zones: zwraca slownik z obrazami kart dla danego gracza. """ h, w, _ = table.shape zones: dict[str, list[np.ndarray]] = { "community_cards": [], "player_one": [], "player_two": [], "player_three": [], } green_mask = self.get_green_mask(table) objects_mask = cv2.bitwise_not(green_mask) num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(objects_mask) for i in range(1, num_labels): x, y, bw, bh, area = stats[i] cx, cy = centroids[i] if area < 1500: continue crop = table[max(0, y - 5):min(h, y + bh + 5), max(0, x - 5):min(w, x + bw + 5)] if cx < w * 0.30 and cy > h * 0.22: zones["community_cards"].append(crop) elif w * 0.32 < cx < w * 0.7 and cy < h * 0.3: zones["player_one"].append(crop) elif cx > w * 0.58 and cy < h * 0.70: zones["player_two"].append(crop) elif cy > h * 0.62: zones["player_three"].append(crop) return zones zoneE = ZoneExtractor() if __name__ == '__main__': frame = cv2.imread('images/test_table_01.jpg') table = zoneE.findTable(frame) zones = zoneE.extract(table) for zone_name, crops in zones.items(): if zone_name == 'player_three': for idx, crop in enumerate(crops): cv2.imshow(f"{zone_name}_{idx}", crop) cv2.waitKey(0) cv2.destroyAllWindows()