如何使用OpenCV平滑和缩小这些非常粗糙的图像?

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

我有一些黑白图像,只有一位。我正在使用在MNIST上训练的NN模型对它们进行分类。但是,与MNIST数据集相比,这些数字太粗糙且太粗。例如:

enter image description here

TLDR:我需要使用OpenCV来平滑图像并可能使整体形状更薄。

python opencv smoothing
2个回答
1
投票

您很可能会从morphological operations中受益。具体来说,这听起来像您要侵蚀。

虽然您确实有些吵闹。您应该尝试使用OpenCV的smoothing operations。根据我的经验,我认为您需要使用内核区域大约为9的中间模糊(尽管这取决于您想要的)。然后您需要使用侵蚀。


0
投票

您可以在Python / OpenCV中使用以下两种形态的组合:关闭,打开和腐蚀(以及可选地进行骨架化和扩张):

输入:

enter image description here

import cv2
import numpy as np
from skimage.morphology import skeletonize

# load image
img = cv2.imread("5.png")

# convert to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# threshold image
thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY)[1]

# apply morphology close
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11,11))
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)

# apply morphology open
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11,11))
thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)

# apply morphology erode
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (21,21))
thresh = cv2.morphologyEx(thresh, cv2.MORPH_ERODE, kernel)

# write result to disk
cv2.imwrite("5_thinned.png", thresh)

# skeletonize image and dilate
skeleton = cv2.threshold(thresh,0,1,cv2.THRESH_BINARY)[1]
skeleton = (255*skeletonize(skeleton)).astype(np.uint8)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15,15))
skeleton_dilated = cv2.morphologyEx(skeleton, cv2.MORPH_DILATE, kernel)

# write result to disk
cv2.imwrite("5_skeleton_dilated.png", skeleton_dilated)

cv2.imshow("IMAGE", img)
cv2.imshow("RESULT1", thresh)
cv2.imshow("RESULT2", skeleton_dilated)
cv2.waitKey(0)
cv2.destroyAllWindows()

结果1(关闭,打开,腐蚀):

enter image description here

Result2(关闭,打开,腐蚀,骨骼化,扩张):

enter image description here

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