chessboard.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import numpy as np
  2. import cv2
  3. # # Define the size of the image
  4. # width, height = 2360, 1640
  5. # # Define the size of the squares
  6. # square_size = 40
  7. # square_cols = 25
  8. # square_rows = 16
  9. # # Calculate the total size of the checkerboard
  10. # checkerboard_width = square_cols * square_size
  11. # checkerboard_height = square_rows * square_size
  12. # # Create an empty image with white background
  13. # image = np.ones((height, width, 3), dtype=np.uint8) * 255
  14. # # Calculate the offsets to center the checkerboard
  15. # x_offset = (width - checkerboard_width) // 2
  16. # y_offset = (height - checkerboard_height) // 2
  17. # # Create the checkerboard pattern within the offsets
  18. # for y in range(square_rows):
  19. # for x in range(square_cols):
  20. # if (x + y) % 2 == 0:
  21. # top_left_y = y_offset + y * square_size
  22. # top_left_x = x_offset + x * square_size
  23. # bottom_right_y = y_offset + (y + 1) * square_size
  24. # bottom_right_x = x_offset + (x + 1) * square_size
  25. # cv2.rectangle(image, (top_left_x, top_left_y), (bottom_right_x, bottom_right_y), (0, 0, 0), -1)
  26. # # Determine the coordinates for the circle just right of the bottom-right white square
  27. # right_most_white_x = x_offset + square_cols * square_size # Rightmost column
  28. # right_most_white_y = y_offset + square_rows * square_size # Bottommost row
  29. # # Check if the bottom-right square is white, if not adjust the x coordinate
  30. # if (square_cols + square_rows - 2) % 2 == 1: # If it's a black square, move one square to the left
  31. # right_most_white_x -= square_size
  32. # # Draw a circle to the right of the bottom-right white square
  33. # cv2.circle(image, (right_most_white_x + square_size + square_size // 2, right_most_white_y + square_size // 2), 20, (255, 0, 0), -1)
  34. # # Save the image with the circle but without coordinates
  35. # cv2.imwrite('checkerboard_with_circle_no_coords.png', image)
  36. # # Create a copy of the image to draw the coordinates
  37. # # image_with_coords = image.copy()
  38. # # # Calculate and mark all the corners
  39. # # for y in range(9): # Include one extra row for bottom edge points
  40. # # for x in range(12): # Include one extra column for right edge points
  41. # # corner_x = x_offset + x * square_size
  42. # # corner_y = y_offset + y * square_size
  43. # # if corner_x < width and corner_y < height: # Ensure points are within image bounds
  44. # # cv2.circle(image_with_coords, (corner_x, corner_y), 5, (0, 255, 0), -1) # Green dot
  45. # # cv2.putText(image_with_coords, f"({corner_x}, {corner_y})", (corner_x + 5, corner_y - 10),
  46. # # cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) # Red color, thickness = 2 for bold
  47. # # # Save the image with both the circle and coordinates
  48. # # cv2.imwrite('checkerboard_with_circle_and_coords.png', image_with_coords)
  49. # 图像大小
  50. image_width = 1920
  51. image_height = 1200
  52. # 棋盘格大小和数量
  53. grid_size = 30
  54. num_cols = 25
  55. num_rows = 16
  56. # 计算棋盘格区域的总大小
  57. board_width = grid_size * num_cols
  58. board_height = grid_size * num_rows
  59. # 计算棋盘格左上角的起始坐标,使其居中
  60. start_x = (image_width - board_width) // 2
  61. start_y = (image_height - board_height) // 2
  62. # 创建白色背景的图像
  63. image = np.ones((image_height, image_width, 3), dtype=np.uint8) * 255 # 白色背景
  64. # 绘制棋盘格
  65. for row in range(num_rows):
  66. for col in range(num_cols):
  67. # 计算每个格子的左上角和右下角坐标
  68. top_left_x = start_x + col * grid_size
  69. top_left_y = start_y + row * grid_size
  70. bottom_right_x = top_left_x + grid_size - 1
  71. bottom_right_y = top_left_y + grid_size - 1
  72. # 棋盘格的颜色交替为黑白
  73. if (row + col) % 2 == 0:
  74. cv2.rectangle(image, (top_left_x, top_left_y), (bottom_right_x, bottom_right_y), (0, 0, 0), -1)
  75. cv2.circle(image, (top_left_x + 2 * grid_size, top_left_y + 2 * grid_size), 20, (255, 0, 0), -1)
  76. # 显示图像
  77. cv2.imshow("Chessboard", image)
  78. cv2.waitKey(0)
  79. cv2.destroyAllWindows()
  80. # 或者保存图像
  81. cv2.imwrite('chessboard_' + str(image_width) + '_' + str(image_height) + '.png', image)