Skip to content

Instantly share code, notes, and snippets.

@butterw

butterw/Sobel.hook

Created Oct 10, 2020
Embed
What would you like to do?
Sobel Edge detection for mpv
//!HOOK OUTPUT
//!BIND HOOKED
//!DESC Sobel Edge
/*
Sobel Edge Detection (RGB >> grayscale output)
original source code (sobel.frag): http://rastergrid.com/blog/2011/01/frei-chen-edge-detector/
glsl-shader adapted to mpv .hook by butterw:
MAIN: pre-resize (RGB)
OUTPUT: post-resize (RGB)
*/
vec4 hook() {
const mat3 G[2] = mat3[](
mat3( 1., 2., 1., 0., 0., 0.,-1., -2., -1.),
mat3( 1., 0., -1., 2., 0., -2., 1., 0., -1.));
mat3 I;
float cnv[2];
vec3 sample0;
/* fetch the 3x3 neighbourhood and use the RGB vector's length as intensity value */
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
sample0 = HOOKED_texOff(vec2(i-1, j-1)).rgb;
I[i][j] = length(sample0);
}
}
/* calculate the convolution values for all the masks */
for (int i=0; i<2; i++) {
float dp3 = dot(G[i][0], I[0]) + dot(G[i][1], I[1]) + dot(G[i][2], I[2]);
cnv[i] = dp3 * dp3;
}
return vec4(0.5 * sqrt(cnv[0]*cnv[0] + cnv[1]*cnv[1]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.