Graphics Reference
In-Depth Information
// |G| = |Gx| + |Gy| => pow(G,2) = Gx*Gx + Gy*Gy
// |G| = |(p1 + 2 * p2 + p3) - (p7 + 2 * p8 + p9)| +
// |(p3 + 2 * p6 + p9) - (p1 + 2 * p4 + p7)|
// p5 == current pixel,
// sample neighbors to create 3x3 kernel
float p1 = LUMINANCE(input[coord + float2(-thickness,
-thickness)]);
float p2 = LUMINANCE(input[coord + float2( 0,
-thickness)]);
float p3 = LUMINANCE(input[coord + float2( thickness,
-thickness)]);
float p4 = LUMINANCE(input[coord + float2(-thickness,
0)]);
float p6 = LUMINANCE(input[coord + float2( thickness,
0)]);
float p7 = LUMINANCE(input[coord + float2(-thickness,
thickness)]);
float p8 = LUMINANCE(input[coord + float2( 0,
thickness)]);
float p9 = LUMINANCE(input[coord + float2( thickness,
thickness)]);
4.
Next we apply the Sobel formula on the values loaded for the kernel.
//float sobelX = (p1 + 2 * p2 + p3) - (p7 + 2 * p8 + p9);
//float sobelY = (p3 + 2 * p6 + p9) - (p1 + 2 * p4 + p7);
float sobelX = mad(2, p2, p1 + p3) - mad(2, p8, p7 + p9);
float sobelY = mad(2, p6, p3 + p9) - mad(2, p4, p1 + p7);
5.
And finally, return the result based on the threshold.
float edgeSqr = (sobelX * sobelX + sobelY * sobelY);
float result = 1.0 - (edgeSqr > threshold * threshold);
// if (edgeSqr > threshold * threshold) { is edge }
return result; // black (0) = edge, otherwise white (1)
} // End SobelEdge
 
Search WWH ::




Custom Search