{"id":1486,"date":"2020-09-13T02:06:56","date_gmt":"2020-09-13T02:06:56","guid":{"rendered":"https:\/\/muthu.co\/?p=1486"},"modified":"2020-12-27T13:25:04","modified_gmt":"2020-12-27T13:25:04","slug":"stroke-width-identification-algorithm-for-text-like-regions","status":"publish","type":"post","link":"http:\/\/write.muthu.co\/stroke-width-identification-algorithm-for-text-like-regions\/","title":{"rendered":"Stroke width identification algorithm for text like regions"},"content":{"rendered":"\n

In character recognition, the distance between two edges of a stroke, measured perpendicular to the stroke centerline is called the stroke width. Take a look at the below image. The length of lines marked in Red gives the stroke width of the character. <\/p>\n\n\n\n

\"\"\/
stroke width lines<\/figcaption><\/figure><\/div>\n\n\n\n

There are quite a few ways to identify the stroke width, In this article I will talk about the one based on distance transform. Distance transform when applied to a binary image gives us an image where each pixel represents it’s distance from the nearest 0 value pixel. <\/p>\n\n\n\n

\"\"\/
Distance Transform<\/figcaption><\/figure><\/div>\n\n\n\n

As you can visually calculate, the stroke width in case of the above image is 6. The value at the center in the DT image is 3 that’s because the pixel is 3 units away from the closest 0 value pixel. So we can conclude that given a binary image, the stroke width is the mean of center pixels multipied by 2. And now to find the center pixels we can skeletonize the image which will give use a boolean mask with center pixles marked as True. <\/p>\n\n\n\n

\"\"\/<\/figure>\n\n\n\n

Once we have boolean mask, we can easily pick the values of center pixels from the DT image. Python code that does all of this is given below:<\/p>\n\n\n\n

from skimage.io import imread\nfrom skimage.color import rgb2gray\nfrom skimage.filters import threshold_otsu\nfrom scipy.ndimage.morphology import distance_transform_edt\nfrom skimage.morphology import skeletonize\nimport numpy as np\n\n\n# load the image\nimg = imread(\"https:\/\/muthu.co\/wp-content\/uploads\/2020\/09\/char.png\")\n\n\n# convert the image to grayscale\ngray = rgb2gray(img)\n\n\n# thresholding the image to binary\nthreshold = threshold_otsu(gray)\nbinary_image = gray > threshold\n\n\n# add some padding to the image to \n# avoid wrong distance calculations for corner pixels\npadded_image = np.pad(binary_image, 2)\n\n\n# find the distance transform\ndistances = distance_transform_edt(padded_image)\n\n\n# skeletonize the image to find the center pixels\nskeleton = skeletonize(padded_image)\n\n\n# find the center pixel distances\ncenter_pixel_distances = distances[skeleton]\n\n# get the stroke width, twice the average of center pixel distances\nstroke_width = np.mean(center_pixel_distances) * 2\n\nprint(stroke_width)\n# show the image\nplt.figure(figsize=(10,10))\nplt.imshow(padded_image, cmap=\"gray\")<\/code><\/pre>\n\n\n\n

References:<\/p>\n\n\n\n

https:\/\/homepages.inf.ed.ac.uk\/rbf\/HIPR2\/distance.htm<\/a><\/p>\n\n\n\n

<\/p>\n","protected":false},"excerpt":{"rendered":"

In character recognition, the distance between two edges of a stroke, measured perpendicular to the stroke centerline is called the stroke width. Take a look at the below image. The length of lines marked in Red gives the stroke width of the character. There are quite a few ways to identify the stroke width, In […]<\/p>\n","protected":false},"author":1,"featured_media":1491,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41,38],"tags":[],"_links":{"self":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/1486"}],"collection":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/comments?post=1486"}],"version-history":[{"count":3,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/1486\/revisions"}],"predecessor-version":[{"id":1520,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/1486\/revisions\/1520"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/media\/1491"}],"wp:attachment":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/media?parent=1486"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/categories?post=1486"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/tags?post=1486"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}