출처 : http://igortrindade.wordpress.com/2010/04/23/fun-with-opengl-and-shaders/
이미지 필터에 대한 공부 중 가장 기본 중에 기본 sharpening 과 embossing
두 필터의 내용은 방법이 비슷하니까 같이 묶어서 정리 !
sharpening matrix
0 -1 0
-1 5 -1
0 -1 0
비슷한 행렬로는 edge detection이 있습니다.
edge detection
-1 -1 -1
-1 8 -1
-1 -1 -1
이제 저 행렬을 fragment shader에 적용합니다.
sharpening
uniform sampler2D sampler;
uniform vec2 offset[9];
uniform int kernel[9];
void main()
{
vec4 sum = vec4(0.0);
int i;
for (i = 0; i < 9; i++) {
vec4 color = texture2D(sampler, gl_TexCoord[0].st + offset[i]);
sum += color * kernel[i];
}
gl_FragColor = sum;
}
결과물
원본 이미지
sharpening 이미지
비슷한 행렬을 사용하는 edge detection을 적용한 fragment shader
uniform sampler2D sampler;
uniform vec2 offset[9];
uniform int kernel[9];
void main()
{
vec4 sum = vec4(0.0);
int i;
for (i = 0; i < 9; i++) {
vec4 color = texture2D(sampler, gl_TexCoord[0].st + offset[i]);
sum += color * kernel[i];
}
sum += 0.51;
gl_FragColor = sum;
}
결과물
edge detection 이미지
마지막으로 embossing을 위한 fragment shader
uniform sampler2D sampler;
uniform vec2 offset[9];
uniform int kernel[9];
void main()
{
vec4 sum = vec4(0.0);
int i = 0;
for( i = 0; i < 9; i++) {
vec4 color_tmp = texture2D(sampler, gl_TexCoord[0].st + offset[i]);
float h = (color_tmp.x + color_tmp.y + color_tmp.z + color_tmp.w)/4.0;
if (h > 1)
h = 1.0;
if (h < 0)
h = 0.0;
vec4 color = vec4(h, h, h, h);
sum.x += color.x * kernel[i];
}
sum.x /= 1.0;
sum.x += 0.51;
if (sum.x > 1)
sum.x = 1;
if (sum.x < 0)
sum.x = 0;
gl_FragColor = vec4(sum.x, sum.x, sum.x, 1);
}
결과물
embossing 이미지