As shown in the attached image [For clarification only], there are three rows of lines with random length.
I would like to delete lines which are shorter than a constant N pixels.
Assumptions:
- Lines are all in rows
- Lines are 1 pixel width
- Lines may be connected to borders.
I’m looking for any idea/guide which can help me with this. Thanks.
Skipping the possibility of directly handling the binary representation of the underlying image, and thus permitting a pretty fast implementation for this specific task, let us use Mathematical Morphology to (efficiently) solve it.
All you need here is a erosion with a horizontal line of length
nfollowed by a morphological reconstruction. The former will completely remove lines shorter of length <n. The later will completely recover the eroded lines that were not removed.Here is an example with
n = 25. I usedones(1, 25)instead ofstrel('line', 25, 0)as the structuring element because the later doesn’t guarantee that the actual line length is the one specified.Eroded and reconstructed images: