1.效果图

2.shader
"attribute vec4 position;\n" +
"attribute vec4 inputTextureCoordinate;\n" +
"\n" +
"uniform float imageWidthFactor; \n" +
"uniform float imageHeightFactor; \n" +
"uniform float sharpness;\n" +
"\n" +
"varying vec2 textureCoordinate;\n" +
"varying vec2 leftTextureCoordinate;\n" +
"varying vec2 rightTextureCoordinate; \n" +
"varying vec2 topTextureCoordinate;\n" +
"varying vec2 bottomTextureCoordinate;\n" +
"\n" +
"varying float centerMultiplier;\n" +
"varying float edgeMultiplier;\n" +
"\n" +
"void main()\n" +
"{\n" +
" gl_Position = position;\n" +
" \n" +
" mediump vec2 widthStep = vec2(imageWidthFactor, 0.0);\n" +
" mediump vec2 heightStep = vec2(0.0, imageHeightFactor);\n" +
" \n" +
" textureCoordinate = inputTextureCoordinate.xy;\n" +
" leftTextureCoordinate = inputTextureCoordinate.xy - widthStep;\n" +
" rightTextureCoordinate = inputTextureCoordinate.xy + widthStep;\n" +
" topTextureCoordinate = inputTextureCoordinate.xy + heightStep; \n" +
" bottomTextureCoordinate = inputTextureCoordinate.xy - heightStep;\n" +
" \n" +
" centerMultiplier = 1.0 + 4.0 * sharpness;\n" +
" edgeMultiplier = sharpness;\n" +
"}";
static final String SHARPEN_FRAGMENT_SHADER = "" +
"precision highp float;\n" +
"\n" +
"varying highp vec2 textureCoordinate;\n" +
"varying highp vec2 leftTextureCoordinate;\n" +
"varying highp vec2 rightTextureCoordinate; \n" +
"varying highp vec2 topTextureCoordinate;\n" +
"varying highp vec2 bottomTextureCoordinate;\n" +
"\n" +
"varying highp float centerMultiplier;\n" +
"varying highp float edgeMultiplier;\n" +
"\n" +
"uniform sampler2D inputImageTexture;\n" +
"\n" +
"void main()\n" +
"{\n" +
" mediump vec3 textureColor = texture2D(inputImageTexture, textureCoordinate).rgb;\n" +
" mediump vec3 leftTextureColor = texture2D(inputImageTexture, leftTextureCoordinate).rgb;\n" +
" mediump vec3 rightTextureColor = texture2D(inputImageTexture, rightTextureCoordinate).rgb;\n" +
" mediump vec3 topTextureColor = texture2D(inputImageTexture, topTextureCoordinate).rgb;\n" +
" mediump vec3 bottomTextureColor = texture2D(inputImageTexture, bottomTextureCoordinate).rgb;\n" +
"\n" +
" gl_FragColor = vec4((textureColor * centerMultiplier - (leftTextureColor * edgeMultiplier + rightTextureColor * edgeMultiplier + topTextureColor * edgeMultiplier + bottomTextureColor * edgeMultiplier)), texture2D(inputImageTexture, bottomTextureCoordinate).w);\n" +
"}";
3.原理
取上下左右四个点,把中心点的像素按照一定的sharpness来改变,临近的像素是比较相似的。
该代码示例展示了如何使用OpenGL着色器进行图像锐化处理。通过对中心像素和周围像素的不同权重计算,调整sharpness参数可以增强图像边缘,实现锐化效果。输入包括纹理坐标、图像宽度和高度因子以及锐度值。
284

被折叠的 条评论
为什么被折叠?



