### Question :

Can anyone describe how can i implement SWT in python using opencv or simplecv ?

##
Answer #1:

Ok so here goes:

The link that has details on the implementation with the code download link at the bottom: SWT

For the sake of completeness, also mentioning that SWT or Stroke Width Transform was devised by Epshtein and others in 2010 and has turned out to be one of the most successful text detection methods til date. It does not use machine learning or elaborate tests. Basically after Canny edge detection on the input image, it calculates the thickness of each stroke that makes up objects in the image. As text has uniformly thick strokes, this can be a robust identifying feature.

The implementation given in the link is using C++, OpenCV and the Boost library they use for the connected graph traversals etc. after the SWT step is computed. Personally I’ve tested it on Ubuntu and it works quite well (and efficiently), though the accuracy is not exact.

##
Answer #2:

I implemented something similar to the distance transform based SWT described in

‘**ROBUST TEXT DETECTION IN NATURAL IMAGES WITH EDGE-ENHANCED MAXIMALLY STABLE EXTREMAL REGIONS** by *Huizhong Chen, Sam S. Tsai, Georg Schroth, David M. Chen, Radek Grzeszczuk, Bernd Girod*‘.

It’s not the same as described in the paper but a rough approximation that served my purpose. Thought I should share it so somebody might find it useful (and point out any errors/improvements). It is implemented in C++ and uses 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
```

##
Answer #3:

There is a complete Library SWTloc here a Python 3 implementation of the algorithm

### Install the library

```
pip install swtloc
```

### Apply SWT Transform and grouping

```
from swtloc import SWTLocalizer
from swtloc.utils import imgshowN, imgshow
swtl = SWTLocalizer()
imgpaths = ... # Image paths, can be one image or more than one
swtl.swttransform(imgpaths=imgpath, save_results=True, save_rootpath='swtres/',
edge_func = 'ac', ac_sigma = 1.0, text_mode = 'wb_bf',
gs_blurr=True, blurr_kernel = (5,5), minrsw = 3,
maxCC_comppx = 10000, maxrsw = 200, max_angledev = np.pi/6,
acceptCC_aspectratio = 5)
```

imgshowN([swtl.orig_img, swtl.swt_mat],

[‘Original Image’, ‘Stroke Width Transform’])

### Get the Bubble Bounding Boxes

```
respacket = swtl.get_grouped(lookup_radii_multiplier=1, sw_ratio=2,
cl_deviat=[13,13,13], ht_ratio=2,
ar_ratio=3, ang_deviat=30)
grouped_labels = respacket[0]
grouped_bubblebbox = respacket[1]
grouped_annot_bubble = respacket[2]
grouped_annot = respacket[3]
maskviz = respacket[4]
maskcomb = respacket[5]
imgshowN([swtl.orig_img, swtl.swt_labelled3C, grouped_annot_bubble],
['Original', 'SWT','Bubble BBox Grouping'])
```

## Some Other Results

Full Disclosure : I am the author of this library