Gradient-like buffers for lines in images

username: ayoubft
cooked: <p>Hi everyone,</p>
<p>I’m working on a project involving geological fractures. My ground truth data is in the form of shapefiles (vector format), which I convert to raster images. The usual approach is to colorize the pixels that overlap with the fracture lines.</p>
<p>DATA:</p>
<div class="d-image-grid">
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/c/bcc39b604f84752d479b43cbfa41683ef50dd1c4.png" data-download-href="https://discuss.pytorch.org/uploads/default/bcc39b604f84752d479b43cbfa41683ef50dd1c4" title="OG1_23_74"><img src="https://discuss.pytorch.org/uploads/default/original/3X/b/c/bcc39b604f84752d479b43cbfa41683ef50dd1c4.png" alt="OG1_23_74" data-base62-sha1="qVSQIQEf3zEJfIRIu0Ak5efV5Jy" width="256" height="256"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">OG1_23_74</span><span class="informations">256×256 125 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/a/fa989ecaa8092116530cf847f22b26e836e96373.png" data-download-href="https://discuss.pytorch.org/uploads/default/fa989ecaa8092116530cf847f22b26e836e96373" title="gtOG1_23_74"><img src="https://discuss.pytorch.org/uploads/default/original/3X/f/a/fa989ecaa8092116530cf847f22b26e836e96373.png" alt="gtOG1_23_74" data-base62-sha1="zKSm0PegjcnjNH3VgjEBKHAQKHh" width="256" height="256"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">gtOG1_23_74</span><span class="informations">256×256 173 Bytes</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
</div>
<p>However, I’m exploring a different idea: creating a buffer around the fractures where pixel values increase as they approach the fracture line and then decrease outward (like a normal distribution around the line).</p>
<p>What I am trying to do:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/5/c582fcce616a6f6d84f4c365becdf7dfce604cae.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/c582fcce616a6f6d84f4c365becdf7dfce604cae" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/5/c582fcce616a6f6d84f4c365becdf7dfce604cae_2_690x466.jpeg" alt="image" data-base62-sha1="sbgHteLMZ4wa6dA05uzn1lsSKrY" width="690" height="466" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/5/c582fcce616a6f6d84f4c365becdf7dfce604cae_2_690x466.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/c/5/c582fcce616a6f6d84f4c365becdf7dfce604cae_2_1035x699.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/c/5/c582fcce616a6f6d84f4c365becdf7dfce604cae.jpeg 2x" data-dominant-color="424241"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1172×793 107 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I have two main questions:</p>
<ol>
<li>Is there a better way to implement this?</li>
<li>Is it worth doing, or are there better alternatives for this kind of problem?</li>
</ol>
<p>I’ve been experimenting with morphological operations, and here’s the <a href="https://gist.github.com/ayoubft/a16ee70f6e99a54e170c11b14b978bef" rel="noopener nofollow ugc">code</a> I used to generate the attached figure:</p>
<pre data-code-wrap="python"><code class="lang-python">import numpy as np
from skimage import morphology as mp
from skimage import io
from skimage.util import img_as_ubyte, dtype_limits
import matplotlib.pyplot as plt

# Load the image
image_path = 'OG1_23_74.png'
mask_path = 'gtOG1_23_74.png'
img = io.imread(image_path)
msk = io.imread(mask_path)[:, :, 0]  # Load and take the first channel
msk = msk.astype(np.uint8)
# Apply morphological operations
dil_msk = mp.dilation(msk, mp.square(9))
dil_msk_1 = mp.dilation(msk, mp.square(9)) ^ msk
dil_msk_2 = mp.dilation(msk, mp.square(19)) ^ dil_msk_1  ^ msk
dil_msk_3 = mp.dilation(msk, mp.square(29)) ^ dil_msk_2  ^ dil_msk_1 ^ msk

# Create composite output with weighted contributions
out = msk + dil_msk_1 / 5 + dil_msk_2 / 9 + dil_msk_3 / 19
</code></pre>
<p>I’d appreciate any insights or suggestions!</p>
<p>Thanks,</p>