Preserving “connectedness” of lines in an image with OpenCV

↘锁芯ラ 提交于 2020-05-16 08:57:06

问题


I'm working on a handwritten digit recognition problem, using OpenCV for preprocessing and Keras/Tensorflow for inference. I'm having an issue with losing certain features in the pre-processing which roughly consists of:

  1. Otsu Threshold
  2. Dilation (to get the digit thickness to be the same as another dataset that I have used to pre-train my neural network)
  3. Downsizing to 28x28 pixels using cv2.INTER_AREA (again to match the pre-training data)

Here's an example of feature loss:

That's meant to be a two, and the tiny gap in the top loop is important in helping differentiate it from a 9 or an 8. But my preprocessed version loses the gap, so the loop looks closed.

I've already asked a similar question for preserving holes and got a great answer, although I think I need to generalise it more.

Just as one of my own ideas: Maybe there's a way to add a second larger kernel to the dilation operation. This kernel would have the same shape as the dilation kernel but only have the outline (not be filled). Then if that outline touches any white pixels in the original image, nullify the action of dilation for that position.

Note: Please feel free to drop a comment with a better way to pose that title ("connectedness" is all I could come up with).


回答1:


Skeletonize first image background (see https://medium.com/analytics-vidhya/skeletonization-in-python-using-opencv-b7fa16867331 or is there any build-in function can do skeletonization in openCV?). Use logical operation between skeleton and second image.



来源:https://stackoverflow.com/questions/59840210/preserving-connectedness-of-lines-in-an-image-with-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!