123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- import numpy as np
- import cv2
- # # Define the size of the image
- # width, height = 2360, 1640
- # # Define the size of the squares
- # square_size = 40
- # square_cols = 25
- # square_rows = 16
- # # Calculate the total size of the checkerboard
- # checkerboard_width = square_cols * square_size
- # checkerboard_height = square_rows * square_size
- # # Create an empty image with white background
- # image = np.ones((height, width, 3), dtype=np.uint8) * 255
- # # Calculate the offsets to center the checkerboard
- # x_offset = (width - checkerboard_width) // 2
- # y_offset = (height - checkerboard_height) // 2
- # # Create the checkerboard pattern within the offsets
- # for y in range(square_rows):
- # for x in range(square_cols):
- # if (x + y) % 2 == 0:
- # top_left_y = y_offset + y * square_size
- # top_left_x = x_offset + x * square_size
- # bottom_right_y = y_offset + (y + 1) * square_size
- # bottom_right_x = x_offset + (x + 1) * square_size
- # cv2.rectangle(image, (top_left_x, top_left_y), (bottom_right_x, bottom_right_y), (0, 0, 0), -1)
- # # Determine the coordinates for the circle just right of the bottom-right white square
- # right_most_white_x = x_offset + square_cols * square_size # Rightmost column
- # right_most_white_y = y_offset + square_rows * square_size # Bottommost row
- # # Check if the bottom-right square is white, if not adjust the x coordinate
- # if (square_cols + square_rows - 2) % 2 == 1: # If it's a black square, move one square to the left
- # right_most_white_x -= square_size
- # # Draw a circle to the right of the bottom-right white square
- # cv2.circle(image, (right_most_white_x + square_size + square_size // 2, right_most_white_y + square_size // 2), 20, (255, 0, 0), -1)
- # # Save the image with the circle but without coordinates
- # cv2.imwrite('checkerboard_with_circle_no_coords.png', image)
- # # Create a copy of the image to draw the coordinates
- # # image_with_coords = image.copy()
- # # # Calculate and mark all the corners
- # # for y in range(9): # Include one extra row for bottom edge points
- # # for x in range(12): # Include one extra column for right edge points
- # # corner_x = x_offset + x * square_size
- # # corner_y = y_offset + y * square_size
- # # if corner_x < width and corner_y < height: # Ensure points are within image bounds
- # # cv2.circle(image_with_coords, (corner_x, corner_y), 5, (0, 255, 0), -1) # Green dot
- # # cv2.putText(image_with_coords, f"({corner_x}, {corner_y})", (corner_x + 5, corner_y - 10),
- # # cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) # Red color, thickness = 2 for bold
- # # # Save the image with both the circle and coordinates
- # # cv2.imwrite('checkerboard_with_circle_and_coords.png', image_with_coords)
- # 图像大小
- image_width = 1920
- image_height = 1200
- # 棋盘格大小和数量
- grid_size = 30
- num_cols = 25
- num_rows = 16
- # 计算棋盘格区域的总大小
- board_width = grid_size * num_cols
- board_height = grid_size * num_rows
- # 计算棋盘格左上角的起始坐标,使其居中
- start_x = (image_width - board_width) // 2
- start_y = (image_height - board_height) // 2
- # 创建白色背景的图像
- image = np.ones((image_height, image_width, 3), dtype=np.uint8) * 255 # 白色背景
- # 绘制棋盘格
- for row in range(num_rows):
- for col in range(num_cols):
- # 计算每个格子的左上角和右下角坐标
- top_left_x = start_x + col * grid_size
- top_left_y = start_y + row * grid_size
- bottom_right_x = top_left_x + grid_size - 1
- bottom_right_y = top_left_y + grid_size - 1
-
- # 棋盘格的颜色交替为黑白
- if (row + col) % 2 == 0:
- cv2.rectangle(image, (top_left_x, top_left_y), (bottom_right_x, bottom_right_y), (0, 0, 0), -1)
- cv2.circle(image, (top_left_x + 2 * grid_size, top_left_y + 2 * grid_size), 20, (255, 0, 0), -1)
-
- # 显示图像
- cv2.imshow("Chessboard", image)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
- # 或者保存图像
- cv2.imwrite('chessboard_' + str(image_width) + '_' + str(image_height) + '.png', image)
|