chessboard.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import numpy as np
  2. import cv2
  3. # Define the size of the image
  4. width, height = 1920, 1080
  5. # Define the size of the squares
  6. square_size = 80
  7. # Calculate the total size of the checkerboard
  8. checkerboard_width = 11 * square_size
  9. checkerboard_height = 8 * square_size
  10. # Create an empty image with white background
  11. image = np.ones((height, width, 3), dtype=np.uint8) * 255
  12. # Calculate the offsets to center the checkerboard
  13. x_offset = (width - checkerboard_width) // 2
  14. y_offset = (height - checkerboard_height) // 2
  15. # Create the checkerboard pattern within the offsets
  16. for y in range(8):
  17. for x in range(11):
  18. if (x + y) % 2 == 0:
  19. top_left_y = y_offset + y * square_size
  20. top_left_x = x_offset + x * square_size
  21. bottom_right_y = y_offset + (y + 1) * square_size
  22. bottom_right_x = x_offset + (x + 1) * square_size
  23. cv2.rectangle(image, (top_left_x, top_left_y), (bottom_right_x, bottom_right_y), (0, 0, 0), -1)
  24. # Determine the coordinates for the circle just right of the bottom-right white square
  25. right_most_white_x = x_offset + 10 * square_size # Rightmost column
  26. right_most_white_y = y_offset + 7 * square_size # Bottommost row
  27. # Check if the bottom-right square is white, if not adjust the x coordinate
  28. if (10 + 7) % 2 == 1: # If it's a black square, move one square to the left
  29. right_most_white_x -= square_size
  30. # Draw a circle to the right of the bottom-right white square
  31. cv2.circle(image, (right_most_white_x + square_size + square_size // 2, right_most_white_y + square_size // 2), 20, (255, 0, 0), -1)
  32. # Save the image with the circle but without coordinates
  33. cv2.imwrite('checkerboard_with_circle_no_coords.png', image)
  34. # Create a copy of the image to draw the coordinates
  35. image_with_coords = image.copy()
  36. # Calculate and mark all the corners
  37. for y in range(9): # Include one extra row for bottom edge points
  38. for x in range(12): # Include one extra column for right edge points
  39. corner_x = x_offset + x * square_size
  40. corner_y = y_offset + y * square_size
  41. if corner_x < width and corner_y < height: # Ensure points are within image bounds
  42. cv2.circle(image_with_coords, (corner_x, corner_y), 5, (0, 255, 0), -1) # Green dot
  43. cv2.putText(image_with_coords, f"({corner_x}, {corner_y})", (corner_x + 5, corner_y - 10),
  44. cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) # Red color, thickness = 2 for bold
  45. # Save the image with both the circle and coordinates
  46. cv2.imwrite('checkerboard_with_circle_and_coords.png', image_with_coords)