<p>I've done this 2 different ways in the past:</p>

<ol>
<li>Apply a pre-blur to the image before running the Sobel operator on it. This will have the side effect of getting rid of any noise that's smaller than the blur kernel, but it will also thicken the lines you end up drawing, too.</li>
<li>Apply a post-process where you thicken the lines by either stamping a shape at every point that's an edge, or using some sort of <a href="https://en.wikipedia.org/wiki/Mathematical_morphology" rel="nofollow">dilate process</a>.</li>
</ol>

CC BY-SA 3.0
2016-05-31T04:50:41.500Z
2016-05-31T04:50:41.500Z
user1118321
<p>I'm using a Sobel filter to draw an outline in a shader. How do I control its thickness? Here's my outlining code:</p>

<pre><code>float3x3 Kx = {
-1, 0, 1,
-2, 0, 2,
-1, 0, 1};

float3x3 Ky = {
1, 2, 1,
0, 0, 0,
-1,-2,-1};

float Lx = 0;
float Ly = 0;    

for (int y = -1; y &lt;= 1; ++y) {
    for (int x = -1; x&lt;= 1; ++x) {
        float2 offset = float2( x, y );
        float3 tex = tex2D(sampler, uv + offset).rgb;
        float luminance = dot(tex, float3(0.3, 0.59, 0.11));

        Lx += luminance * Kx[y+1][x+1];
        Ly += luminance * Ky[y+1][x+1];
    }
}
float color = sqrt(Lx*Lx + Ly*Ly);
return float4( color, color, color, color );
</code></pre>

<shader><edge-detection>
Controlling Sobel edge thickness
SurvivalMachine
# Controlling Sobel edge thickness

(Asked by: username_0 on 2016-05-30)

I'm using a Sobel filter to draw an outline in a shader. How do I control its thickness? Here's my outlining code:

```
float3x3 Kx = {
-1, 0, 1,
-2, 0, 2,
-1, 0, 1};

float3x3 Ky = {
1, 2, 1,
0, 0, 0,
-1,-2,-1};

float Lx = 0;
float Ly = 0;    

for (int y = -1; y <= 1; ++y) {
    for (int x = -1; x<= 1; ++x) {
        float2 offset = float2( x, y );
        float3 tex = tex2D(sampler, uv + offset).rgb;
        float luminance = dot(tex, float3(0.3, 0.59, 0.11));

        Lx += luminance * Kx[y+1][x+1];
        Ly += luminance * Ky[y+1][x+1];
    }
}
float color = sqrt(Lx*Lx + Ly*Ly);
return float4( color, color, color, color );

```

**Tags:** shader, edge-detection


---

## 1 answers

### Answer by username_1 on 2016-05-31. Score: 4 | Accepted answer

I've done this 2 different ways in the past:

1. Apply a pre-blur to the image before running the Sobel operator on it. This will have the side effect of getting rid of any noise that's smaller than the blur kernel, but it will also thicken the lines you end up drawing, too.
2. Apply a post-process where you thicken the lines by either stamping a shape at every point that's an edge, or using some sort of [dilate process](https://en.wikipedia.org/wiki/Mathematical_morphology).


---

