使用Opencv和python保持黑白图像的中心形状

问题描述 投票:0回答:1

我是图像处理新手,如何在该图像中保留中心形状,然后单独在中心形状上画一条线?

谢谢!

去除背景并保持中心形状

python opencv image-processing
1个回答
0
投票

当然!要使用 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”替换为实际图像文件的路径。根据您的具体需求调整半径和其他参数。

© www.soinside.com 2019 - 2024. All rights reserved.