Search code examples
iosswiftimage-processinguiimagepixel

How to extract specific pixels and make them more large?


Introduction: explanation of the situation

I have a UIImage like this:

Input


The image represents a "circuit" (running track).



Problem

I need to detect the "circuit" (black pixels) and enlarge it (increase track width). Something like this:

enter image description here


I'm open to every suggestion / algorithm / other useful to get the expected result.

Thank you :)


Solution

  • Ok, as @CrisLuengo said, Dilation is the solution.

    I worked on it and I got a good initial result.

    An example:

    image

    I need to improve the algorithm to enlarge the size of the shape more and more.

    In my case I convert the image from RGB to Grayscale (pixel values range [0-1]. Then I process it in the following way:

    1. Scan each pixel of the image
    2. Check if the pixel is dark (pixel value < 0.45).
    3. If true => I fill in black this pixel and all the pixels around it (considering the structuring element).

    In the example the structuring element is the following:

    0 0 1 0 0
    0 1 1 1 0
    1 1 1 1 1
    0 1 1 1 0
    0 0 1 0 0