Hassaan Ahmad
Hassaan Ahmad

Reputation: 93

Change 8-connectivity of pixels in an image to 4-connectivity

I have a BW image containing a 8-Connected path

The image needs to be changed such that there is a 4-connectivity between the pixels, i.e. one can traverse the whole path without going diagonally.

This process needs to be done using morphological operations.

One possible output is this image. 4-Connected path

Upvotes: 3

Views: 1160

Answers (1)

FiReTiTi
FiReTiTi

Reputation: 5898

Hit-or-miss operations are part of mathematical morphology, so I would do the opposite of a skeletonize operation.

So I would use this hit-or-miss filter:

X 0 1

X 1 0

X X X

X being any values. With such a mask, you connect black pixels (value 0) that touch each others by a corner Don't forget to build the three other rotations (90°, 180° and 270°) of the filter.

Here is the pseudocode:

Input: Input Image In, Output Image Out, the four hit-or-miss filters F0, F1, F2, F3
Copy In into Out
For each pixel p in In
    if F0 is true for p, or F1 is true for p, F2 is true for p, F3 is true for p
        Out(p) becomes black.

Upvotes: 2

Related Questions