如何使用Python获取黄色框标记正方形的颜色序列?
Hey there! Let's work through your two questions about extracting color sequences from squares using Python. I'll use OpenCV (a staple for image processing tasks) since it's ideal for this kind of problem. Here's how to tackle each scenario:
1. How to Get the Color Sequence of All Squares
Since all squares are the same size, move from top to middle, and only use red/yellow/blue, we can follow this workflow:
Step-by-Step Approach
- Load and preprocess the image to detect square contours.
- Filter out non-square shapes using contour approximation and aspect ratio checks.
- Sort the squares by their vertical position (top to middle, so smallest y-coordinate first).
- Extract the color of each square's center (to avoid border interference) and map it to the defined color labels.
Example Code
import cv2 import numpy as np # Load your image (replace with your file path) img = cv2.imread('square_image.jpg') # Convert to HSV color space (easier to distinguish colors than RGB) hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # Define helper function to map HSV pixel values to color names def get_square_color(hsv_pixel): h, s, v = hsv_pixel # Red spans two ranges in HSV if (0 <= h <= 10 or 170 <= h <= 180) and s >= 120 and v >= 70: return 'red' elif 20 <= h <= 30 and s >= 100 and v >= 100: return 'yellow' elif 90 <= h <= 130 and s >= 50 and v >= 50: return 'blue' return None # Preprocess to find contours gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (5, 5), 0) edges = cv2.Canny(blurred, 50, 150) # Extract contours contours, _ = cv2.findContours(edges.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Filter contours to keep only squares squares = [] for cnt in contours: perimeter = cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(cnt, 0.04 * perimeter, True) # A square has 4 vertices and near 1:1 aspect ratio if len(approx) == 4: x, y, w, h = cv2.boundingRect(approx) aspect_ratio = w / float(h) if 0.95 <= aspect_ratio <= 1.05: squares.append((x, y, w, h)) # Sort squares from top to middle (smallest y-coordinate first) squares.sort(key=lambda square: square[1]) # Build the color sequence full_color_sequence = [] for x, y, w, h in squares: # Get center pixel to avoid border color interference center_x = x + w // 2 center_y = y + h // 2 color = get_square_color(hsv_img[center_y, center_x]) if color: full_color_sequence.append(color) print("Full square color sequence:", ','.join(full_color_sequence))
2. How to Get the Color Sequence of Squares Marked with Yellow Boxes
For this, we first detect the yellow bounding boxes, then extract the color of the square inside each box.
Step-by-Step Approach
- Isolate yellow regions in the image using HSV color masking.
- Detect contours of these yellow regions to find the boxes.
- Sort the boxes by vertical position (same top-to-middle order).
- Extract the color of the square inside each box (using the box's center, since the square is centered within the box).
Example Code
# Define HSV range for yellow boxes (adjust values if needed for your image) yellow_box_lower = np.array([20, 150, 150]) yellow_box_upper = np.array([30, 255, 255]) yellow_mask = cv2.inRange(hsv_img, yellow_box_lower, yellow_box_upper) # Extract yellow box contours box_contours, _ = cv2.findContours(yellow_mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Filter to keep only square-shaped yellow boxes yellow_boxes = [] for cnt in box_contours: perimeter = cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(cnt, 0.04 * perimeter, True) if len(approx) == 4: x, y, w, h = cv2.boundingRect(approx) aspect_ratio = w / float(h) if 0.95 <= aspect_ratio <= 1.05: yellow_boxes.append((x, y, w, h)) # Sort boxes from top to middle yellow_boxes.sort(key=lambda box: box[1]) # Build the marked square color sequence marked_color_sequence = [] for x, y, w, h in yellow_boxes: # Get center of the box (where the square's center lies) center_x = x + w // 2 center_y = y + h // 2 color = get_square_color(hsv_img[center_y, center_x]) if color: marked_color_sequence.append(color) print("Marked square color sequence:", ','.join(marked_color_sequence)) # Expected output: red,yellow,blue,yellow,yellow
Quick Notes
- Adjust the HSV color ranges if your image's red/yellow/blue tones differ from the defaults.
- If squares have thick borders, you can tweak the center pixel position (e.g.,
center_x = x + w//2 + 5) to avoid border colors. - For consistent square detection, you can add an area threshold (e.g., only keep contours with area between
min_areaandmax_area) since all squares are the same size.
内容的提问来源于stack exchange,提问作者Vito
相关产品推荐
相关产品推荐

