How to fill the gaps in letters after Canny edge detection

一笑奈何 提交于 2019-12-04 17:00:19

In this case, because the text is black, it is best to simply find all the black pixels.

One very simple way to accomplish this using NumPy is as follows:

import matplotlib.pyplot as pp
import numpy as np

image = pp.imread(r'/home/cris/tmp/Zuv3p.jpg')
bin = np.all(image<100, axis=2)

What this does is find all pixels where all three channels are below a value of 100. I picked the threshold of 100 sort of randomly, there probably are better ways to pick a threshold. :)


Notes:

1- When working with color input, converting to gray-value image as first step is usually a bad idea. This throws away a lot of information. Sometimes it's appropriate, but in this case it is better not to.

2- Edge detection is really nice, but is usually the wrong approach. Use edge detection when you need to find edges. Use something else when you don't want just the edges.


Edit: If for some reason np.all complains about the data type (it doesn't for me), you should be able to convert its input to the right type:

bin = np.all(np.array(image<100, dtype=np.bool), axis=2)

or maybe

bin = np.all(np.array(image<100, dtype=np.uint8), axis=2)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!