I’d rather not recreate the wheel if I don’t have to and this must have been done before. Are there any implementations of a Sobel filter using OpenGL ES?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If Objective-C is acceptable, you could look at my GPUImage framework and its GPUImageSobelEdgeDetectionFilter. This applies Sobel edge detection using OpenGL ES 2.0 fragment shaders. You can see the output from this in the “sketch” example in this answer.
If you don’t want to dig into the Objective-C code, the critical work here is performed by two sets of shaders. In a first pass, I reduce the image to its luminance and store that value in the red, green, and blue channels. I do this using the following vertex shader:
and fragment shader:
After that, I actually perform the Sobel edge detection (with lighter pixels being edges in this case) using this vertex shader:
and this fragment shader:
The
imageWidthFactorandimageHeightFactorare merely reciprocals of the input image size in pixels.You might notice that this two-pass approach is more complex than the one in the above-linked answer. That’s because the original implementation wasn’t the most efficient when running on mobile GPUs (at least those of the PowerVR variety in iOS devices). By removing all dependent texture reads and precalculating the luminance so that I only have to sample from the red channel in the final shader, this tuned edge detection method is 20X faster in my benchmarks than the naive one that does all this in one pass.