originalBW = imread('text.png');
originalBW = logical(originalBW);
figure;
imshow(originalBW);
se1 = strel('square',9);
B1 = imerode(originalBW, se1);
B2 = imreconstruct(B1, originalBW);
C1 = logical(originalBW - B2);
C2 = bwareafilt(originalBW,[10,15]);
figure
imshow(C2);
SE1 = strel('rectangle', [15,1]);
dilateBW = imdilate(C1, SE1);
reconBW = imreconstruct(dilateBW, originalBW);
BW2 = logical(reconBW-C1);
dilateBW2 = imdilate(BW2, SE1);
C2 = imreconstruct(dilateBW2, originalBW);
figure;
imshow(C2);
BW3 = imfill(originalBW, 'holes');
SE2 = strel('disk', 4);
BW4 = imerode(BW3, SE2);
C3 = imreconstruct(logical(BW4),originalBW);
figure;
imshow(C4);
C4 = bwareafilt(C3,12);
figure;
imshow(C4);