7 uniform vec2 inv_image_size;
11 // There are two common Sobel filters, horizontal and vertical
12 // (see e.g. Wikipedia, or the OpenCV documentation):
14 // [1 0 -1] [-1 -2 -1]
17 // Horizontal Vertical
19 // Note that Wikipedia and OpenCV gives entirely opposite definitions
20 // with regards to sign! This appears to be an error in the OpenCV
21 // documentation, forgetting that for convolution, the filters must be
22 // flipped. We have to flip the vertical matrix again comparing to
23 // Wikipedia, though, since we have bottom-left origin (y = up)
24 // and they define y as pointing downwards.
26 // Computing both directions at once allows us to get away with eight
27 // texture samples instead of twelve.
29 float x_left = tc.x - inv_image_size.x;
31 float x_right = tc.x + inv_image_size.x;
33 float y_top = tc.y + inv_image_size.y; // Note the bottom-left coordinate system.
35 float y_bottom = tc.y - inv_image_size.y;
37 float top_left = texture(tex, vec2(x_left, y_top)).x;
38 float left = texture(tex, vec2(x_left, y_mid)).x;
39 float bottom_left = texture(tex, vec2(x_left, y_bottom)).x;
41 float top = texture(tex, vec2(x_mid, y_top)).x;
42 float bottom = texture(tex, vec2(x_mid, y_bottom)).x;
44 float top_right = texture(tex, vec2(x_right, y_top)).x;
45 float right = texture(tex, vec2(x_right, y_mid)).x;
46 float bottom_right = texture(tex, vec2(x_right, y_bottom)).x;
48 gradients.x = (top_right + 2.0f * right + bottom_right) - (top_left + 2.0f * left + bottom_left);
49 gradients.y = (top_left + 2.0 * top + top_right) - (bottom_left + 2.0f * bottom + bottom_right);
51 // Normalize so that we have a normalized unit of intensity levels per pixel.