要保留调整大小的分割手写数字的特征,可以使用以下解决方法:
import cv2
# 灰度化
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 二值化
_, binary_image = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
# 去噪
denoised_image = cv2.medianBlur(binary_image, ksize=5)
import cv2
# 连通组件分析
num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(denoised_image)
# 轮廓检测
contours, _ = cv2.findContours(denoised_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 对分割出的数字进行可视化
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow("Segmented Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
# 调整大小为固定尺寸
resized_image = cv2.resize(image, (28, 28))
通过以上步骤,你可以实现保留调整大小的分割手写数字的特征。请注意,以上代码示例可能需要根据具体情况进行适当的调整和修改。
下一篇:保留调整画布尺寸后的图像