有人能描述一下如何使用OpenCV或SimpleCV在Python中实现SWT吗?
有人能描述一下如何使用OpenCV或SimpleCV在Python中实现SWT吗?
好的,下面开始:
链接中包含实现细节和代码下载链接:SWT
为了完整起见,还要提到 SWT 或 Stroke Width Transform,它是由 Epshtein 等人于 2010 年设计的,并且已经成为迄今为止最成功的文本检测方法之一。它不使用机器学习或复杂的测试。基本上,在输入图像上执行 Canny 边缘检测后,它计算组成图像对象的每个笔画的粗细。由于文本具有均匀粗细的笔画,因此这可以成为一个强大的识别特征。
链接中提供的实现使用 C++、OpenCV 和 Boost 库,在计算 SWT 步骤后用于连接图遍历等操作。就个人而言,我在 Ubuntu 上进行了测试,它表现得相当不错(而且效率很高),尽管准确性不是完全精确。
// bw8u : we want to calculate the SWT of this. NOTE: Its background pixels are 0 and forground pixels are 1 (not 255!)
Mat bw32f, swt32f, kernel;
double min, max;
int strokeRadius;
bw8u.convertTo(bw32f, CV_32F); // format conversion for multiplication
distanceTransform(bw8u, swt32f, CV_DIST_L2, 5); // distance transform
minMaxLoc(swt32f, NULL, &max); // find max
strokeRadius = (int)ceil(max); // half the max stroke width
kernel = getStructuringElement(MORPH_RECT, Size(3, 3)); // 3x3 kernel used to select 8-connected neighbors
for (int j = 0; j < strokeRadius; j++)
{
dilate(swt32f, swt32f, kernel); // assign the max in 3x3 neighborhood to each center pixel
swt32f = swt32f.mul(bw32f); // apply mask to restore original shape and to avoid unnecessary max propogation
}
// swt32f : resulting SWT image
maxval
设置为 1
来创建此二进制图像。这只是一个例子。 - dhanushka这里有一个完整的库SWTloc,它是该算法的Python 3实现。
v2.0.0及以上版本
pip install swtloc
import swtloc as swt
imgpath = 'images/path_to_image.jpeg'
swtl = swt.SWTLocalizer(image_paths=imgpath)
swtImgObj = swtl.swtimages[0]
swt_mat = swtImgObj.transformImage(text_mode='lb_df',
auto_canny_sigma=1.0,
maximum_stroke_width=20)
localized_letters = swtImgObj.localizeLetters(minimum_pixels_per_cc=100,
maximum_pixels_per_cc=10_000,
acceptable_aspect_ratio=0.2)
localized_words = swtImgObj.localizeWords()
完整披露:我是这个库的作者。