平滑手写数字中的极端点

发布于 2025-01-25 07:45:27 字数 1176 浏览 2 评论 0原文

我试图识别手写数字。说我有以下图像:

​net/p4h8t.png“ rel =“ nofollow noreferrer”> “

我首先应用cv2。 thresh_binary_inv删除噪声。

现在我尝试使用cv2.erode() np.ones(((5,5))作为内核,但是结果图仍然具有极端点。

我认为应用cv2.findContours()可能有助于获得所需的形状,但我将最终得到两个轮廓,一个用于内部,另一个用于外部。任何想法都将不胜感激!

编辑: 多亏了@statemachine,我设法获得了数字的骨架。我应用了cv2.ximgproc.thinning(),然后是cv2.gaussianblur()cv2.morph_close。如果该图像的极端点可以稍微平滑一点,那么它将是完美的。我仍然对任何想法开放:)

“

I am trying to recognize hand written digits. Say that I have the following image:

Original

My target is to smooth the extremal features of the contours, and keep only the shape of the white trace like below:

mod

I first applied cv2.THRESH_BINARY_INV to remove the noise.

TBI

Now I tried applying cv2.erode() with np.ones((5,5)) as the kernel, but the resulting figure still had the extremal points.

eroded

I think applying cv2.findContours() may help to get the desired shape, but I am going to end up with two contours, one for the inner and another for the outer part. Any ideas will be much appreciated!

Edit:
Thanks to @stateMachine, I managed to get a skeleton of the digit. I applied cv2.ximgproc.thinning(), followed by cv2.GaussianBlur() and cv2.MORPH_CLOSE. If the extremal points of this image can be smoothened a bit then it would be perfect. I am still open to any ideas :)

skel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

任谁 2025-02-01 07:45:27

也许您正在寻找的是形状的骨架。骨架是OpenCV的扩展图像处理模块的一部分(pip install opencv-contrib-python)。您可以这样计算图像的骨架:

# Imports:
import cv2

# Image path
path = "D://opencvImages//"
fileName = "OKwfZ.png"

# Reading an image in default mode:
inputImage = cv2.imread(path + fileName)

# To Grayscale:
grayscaleImage = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)

# Compute the skeleton:
skeleton = cv2.ximgproc.thinning(grayscaleImage, None, 1)

cv2.imshow("Skeleton", skeleton)
cv2.waitKey(0)

这是结果:

“在此处输入图像说明”

骨架将图像的厚度归一化为1 Pixel。如果需要较厚的线,则可以应用一些扩张

Maybe what you are looking for is the shape's skeleton. The skeleton is part of OpenCV's extended image processing module (pip install opencv-contrib-python). You can compute the skeleton of your image like this:

# Imports:
import cv2

# Image path
path = "D://opencvImages//"
fileName = "OKwfZ.png"

# Reading an image in default mode:
inputImage = cv2.imread(path + fileName)

# To Grayscale:
grayscaleImage = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)

# Compute the skeleton:
skeleton = cv2.ximgproc.thinning(grayscaleImage, None, 1)

cv2.imshow("Skeleton", skeleton)
cv2.waitKey(0)

This is the result:

enter image description here

The skeleton normalizes the thickness of the image to 1 pixel. If you need a thicker line you can apply some dilations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文