Figure 7.13 - Segmentation of keys using watershed transform with inner and outer markers
For detecting the keys in a gray-scale image of a calculator, the internal markers are the characters on the keys and the external markers are obtained by the SKIZ of the key characters. The full algorithm is illustrated in Fig. 7.13: (a) the input grayscale image; (b) the opening top-hat to enhance the characters of the keyboard; (c) the threshold of the dilated top-hat to detect the characters, which are the inner markers for the watershed; (d) the Euclidean SKIZ of the inner markers to serve as the background marker; (e) the markers overlaid on the morphological gradient, which is the input to the watershed from markers; and (f) the watershed lines. Note that the dilation has been applied in part (c) to connect small objects in the same key, such as with the “AC” key. Also note that two watershed transforms are used in this segmentation, one for the SKIZ to compute the outer marker and the other to compute the final key segmentation.
1 import numpy as np 2 import ia870 as MT 3 4 a = adreadgray('keyb.tif') 5 b = MT.iagradm(a, MT.iasebox()) 6 c = MT.iaopenth(a,MT.iasebox(5)) 7 d = MT.iathreshad(c, uint8(150)) 8 e = MT.iadil(d, MT.iasebox(3)) 9 f = MT.iawatershed(MT.iadist(MT.ianeg(e),MT.iasebox(),'EUCLIDEAN'),MT.iasebox()) 10 g = MT.iaunion(e,f) 11 h = MT.iacwatershed(b,g, MT.iasebox()) 12 13 adshow(a, '(a)') 14 adshow(MT.iapad(MT.ianeg(c)), '(b)') 15 adshow(MT.iapad(MT.ianeg(e)), '(c)') 16 17 x = MT.iagshow(f,e) 18 adshow(x, '(d)') 19 20 x1 = MT.iagshow(b,g) 21 adshow(x1, '(e)') 22 23 adshow(MT.iapad(MT.ianeg(h)), '(f)')