当然!要使用 OpenCV 和 Python 保持黑白图像的中心形状,您可以使用图像处理技术。这是使用圆形蒙版的基本示例:
import cv2
import numpy as np
# Load the image in grayscale
image = cv2.imread('your_image.jpg', cv2.IMREAD_GRAYSCALE)
# Get image dimensions
height, width = image.shape
# Create a circular mask
mask = np.zeros((height, width), dtype=np.uint8)
center = (width // 2, height // 2)
radius = min(width, height) // 4 # You can adjust the radius as needed
cv2.circle(mask, center, radius, 255, thickness=cv2.FILLED)
# Apply the mask to the image
result = cv2.bitwise_and(image, image, mask=mask)
# Display the result
cv2.imshow('Center Shape', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
确保将“your_image.jpg”替换为实际图像文件的路径。根据您的具体需求调整半径和其他参数。