如何使用Python OpenCV绘制螺旋正方形
Got it, let's tackle this spiral square problem with OpenCV and NumPy! You already have a solid foundation with checkerboards and nested squares, so this should be a natural next step.
The core idea behind drawing a spiral square is to iterate through four directions (right → down → left → up) in a loop, drawing each segment of the spiral and gradually shrinking our drawing boundaries each time we finish a direction. This way, we work our way inward until we can't draw any more segments.
Here's a complete implementation that aligns with your existing code patterns:
import cv2 import numpy as np # Configuration parameters mySize = 256 # Size of the central spiral area myOffset = 16 # Padding around the spiral lineThickness = 8 # Thickness of each spiral segment bgColor = 127 # Background gray value spiralColor = 0 # Initial spiral color (we'll toggle this for contrast) # Create base image with background color img = np.ones((mySize + 2 * myOffset, mySize + 2 * myOffset), dtype=np.uint8) * bgColor # Define initial boundaries for the spiral top = myOffset bottom = myOffset + mySize left = myOffset right = myOffset + mySize # Direction order: right, down, left, up (dy, dx) directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] dir_idx = 0 # Start by moving right while top < bottom and left < right: # Get current direction dy, dx = directions[dir_idx] if dir_idx == 0: # Draw right along the top edge img[top:top+lineThickness, left:right] = spiralColor top += lineThickness elif dir_idx == 1: # Draw down along the right edge img[top:bottom, right-lineThickness:right] = spiralColor right -= lineThickness elif dir_idx == 2: # Draw left along the bottom edge img[bottom-lineThickness:bottom, left:right] = spiralColor bottom -= lineThickness elif dir_idx == 3: # Draw up along the left edge img[top:bottom, left:left+lineThickness] = spiralColor left += lineThickness # Toggle color after completing a full spiral loop spiralColor = 255 if spiralColor == 0 else 0 # Switch to next direction dir_idx = (dir_idx + 1) % 4 # Display the result cv2.imshow('Spiral Square', img) cv2.waitKey(0) cv2.destroyAllWindows()
Key Details to Note:
- Boundary Adjustment: We use
top,bottom,left, andrightto track the current area we're drawing in. Each time we finish a segment, we shift the corresponding boundary inward by the line thickness to avoid redrawing over existing segments. - Direction Cycling: The modulo operation ensures we loop through the four directions repeatedly until the spiral can't shrink any further.
- Color Toggling: Just like your nested squares, we switch the spiral color every time we complete a full loop of directions (right → down → left → up) to create that alternating contrast.
- Efficient Drawing: Instead of pixel-by-pixel loops, we use NumPy slicing to draw entire segments at once—this is way faster, especially for larger images.
You can tweak parameters like lineThickness, mySize, or the color values to get exactly the spiral look you're aiming for.
内容的提问来源于stack exchange,提问作者Mark

