谁能描述我如何使用 opencv 或 simplecv 在 python 中实现 SWT?
3 回答
好的,这里是:
底部有代码下载链接的实现细节链接:SWT
为了完整起见,还提到 SWT 或 Stroke Width Transform 是 Epshtein 等人在 2010 年设计的,并且已证明是迄今为止最成功的文本检测方法之一。它不使用机器学习或复杂的测试。基本上在对输入图像进行 Canny 边缘检测之后,它会计算构成图像中对象的每个笔划的粗细。由于文本具有统一粗笔划,这可能是一个强大的识别功能。
在计算 SWT 步骤之后,链接中给出的实现是使用 C++、OpenCV 和它们用于连接图遍历等的Boost库。就我个人而言,我已经在 Ubuntu 上对其进行了测试,它工作得很好(而且效率很高),尽管准确性并不准确。
我实现了类似于“ ROBUST TEXT DETECTION IN NATURAL-ENHANCED MAXIMALLY STABLE EXTREMAL REGIONS by Huizhong Chen, Sam S. Tsai, Georg Schroth, David M. Chen, Radek Grzeszczuk, Bernd Girod ”中描述的基于距离变换的 SWT .
它与论文中描述的不同,但粗略的近似值符合我的目的。认为我应该分享它,以便有人可能会发现它有用(并指出任何错误/改进)。它用 C++ 实现并使用 OpenCV。
// 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
这里有一个完整的库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()
完全披露:我是这个库的作者