Figure 2.10 - Filtering salt-and-pepper noise
When there is both union and subtractive noise, one strategy is to open to eliminate union noise in the background and then close to fill subtractive noise in the foreground. The resulting filter is called an open-close.
Open-close is illustrated in Fig. 2.10, which shows a salt-and-pepper degraded realization of the original text image of Fig. 2.9 (a) and the result of open-close with a 2 x 2 structuring element applied to the noisy image. One can also close and then open, the filter then being called a close-open. These two filters are not dual, and they tend to give similar but different results.
1 import numpy as np 2 import ia870 as MT 3 from handson.lib import iabggmodel 4 5 f = adreadgray('MVBook/text1.png') > 0 6 7 n_salt = iabggmodel(shape(f),MT.iasecross(),1,0.1) 8 9 n_pepper = iabggmodel(shape(f),MT.iasecross(),1,0.1) 10 11 fbin = MT.iaintersec(MT.iaunion(f,n_salt),MT.ianeg(n_pepper)) 12 13 B = MT.iabinary([[1, 1], 14 [1, 1]]) 15 16 g = MT.iaasf(fbin,'oc',B,1) 17 adshow(MT.iapad(fbin), '(a) corrupted image') 18 adshow(MT.iapad(g), '(b) restoration by open-close')