1
$\begingroup$

I'm trying to write a perona malik filter in HLSL

texture2D Input0;
sampler2D Input0Sampler = sampler_state
{
    Texture = <Input0>;
    MinFilter = Point;
    MagFilter = Point;
    MipFilter = Point;
    AddressU = Clamp;
    AddressV = Clamp;
};

struct VertexShaderInput
{
    float4 Position : POSITION0;
    float2 TextureCoordinate : TEXCOORD0;
};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
    float2 TextureCoordinate : TEXCOORD0;
};

struct PixelShaderOutput
{
    float4 Index0 : COLOR0;
};

// input texture dimensions
static float w = 1920 - 8;
static float h = 1080 - 8;

static const float2 pixel = float2(1.0 / w, 1.0 / h);
static const float2 halfPixel = float2(pixel.x / 2, pixel.y / 2);

static const float3x3 hN =
{
    0,  1, 0,
    0, -1, 0,
    0,  0, 0
};
static const float3x3 hS =
{
    0,  0, 0,
    0, -1, 0,
    0,  1, 0
};
static const float3x3 hE =
{
    0,  0, 0,
    0, -1, 1,
    0,  0, 0
};
static const float3x3 hW =
{
    0,  0, 0,
    1, -1, 0,
    0,  0, 0
};
static const float3x3 hNE =
{
    0,  0, 1,
    0, -1, 0,
    0,  0, 0
};
static const float3x3 hSE =
{
    0,  0, 0,
    0, -1, 0,
    0,  0, 1
};
static const float3x3 hSW =
{
    0,  0, 0,
    0, -1, 0,
    1,  0, 0
};
static const float3x3 hNW =
{
    1,  0, 0,
    0, -1, 0,
    0,  0, 0
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput vsInput)
{
    VertexShaderOutput output;
    vsInput.Position.x =  vsInput.Position.x - 2*halfPixel.x;
    vsInput.Position.y =  vsInput.Position.y + 2*halfPixel.y;
    output.Position = vsInput.Position;
    output.TextureCoordinate = vsInput.TextureCoordinate ;
    return output;
}

float4 Convolution(VertexShaderOutput input, float3x3 kernel)
{
    float4 pixel = float4(0.0f, 0.0f, 0.0f, 0.0f);

    for (int i = -1; i <= 1; ++i)
    {
        for (int j = -1; j <= 1; ++j)
        {
            pixel += kernel[i+1][j+1] * tex2D(Input0Sampler, input.TextureCoordinate + float2(i,j));
        };
    };

    return pixel;
}

PixelShaderOutput PixelShaderFunction(VertexShaderOutput psInput)
{       
    PixelShaderOutput output;
    output.Index0 = tex2D(Input0Sampler, psInput.TextureCoordinate);

    float dx, dy, dd;
    dx = 1; dy = 1; dd = pow(2, 0.5);
    float delta_t = 1/7;
    float k = 30;

    float4 nablaN = Convolution(psInput, hN);
    float4 nablaS = Convolution(psInput, hS);
    float4 nablaW = Convolution(psInput, hW);
    float4 nablaE = Convolution(psInput, hE);
    float4 nablaNE = Convolution(psInput, hNE);
    float4 nablaSE = Convolution(psInput, hSE);
    float4 nablaSW = Convolution(psInput, hSW);
    float4 nablaNW = Convolution(psInput, hNW);

    /*
    float4 cN  = 1 / pow( 1 + (nablaN / k), 2);
    float4 cS  = 1 / pow( 1 + (nablaS / k), 2);
    float4 cW  = 1 / pow( 1 + (nablaW / k), 2);
    float4 cE  = 1 / pow( 1 + (nablaE / k), 2);
    float4 cNE = 1 / pow( 1 + (nablaNE / k), 2);
    float4 cSE = 1 / pow( 1 + (nablaSE / k), 2);
    float4 cSW = 1 / pow( 1 + (nablaSW / k), 2);
    float4 cNW = 1 / pow( 1 + (nablaNW / k), 2);
    */
    float4 cN  = exp(-(nablaN/k)*(nablaN/k));
    float4 cS  = exp(-(nablaS/k)*(nablaS/k));
    float4 cW  = exp(-(nablaW/k)*(nablaW/k));
    float4 cE  = exp(-(nablaE/k)*(nablaE/k));
    float4 cNE = exp(-(nablaNE/k)*(nablaNE/k));
    float4 cSE = exp(-(nablaSE/k)*(nablaSE/k));
    float4 cSW = exp(-(nablaSW/k)*(nablaSW/k));
    float4 cNW = exp(-(nablaNW/k)*(nablaNW/k));

    output.Index0 += delta_t * 
    (
        (mul(cN, nablaN)/(dy*dy)) + (mul(cS, nablaS)/(dy*dy)) + (mul(cW, nablaW)/(dx*dx)) + (mul(cE, nablaE)/(dx*dx)) + (dd*dd)*(mul(cNE, nablaNE) + mul(cSE, nablaSE) + mul(cSW, nablaSW) + mul(cNW, nablaNW))
    );

    return output;
}

technique PeronaMalik
{
    pass pass1
    {
        VertexShader = compile vs_3_0 VertexShaderFunction();
        PixelShader  = compile ps_3_0 PixelShaderFunction();
    }
}

The problem is that when I apply this filter, I end up with the same image I had before the filter was applied. The problem seems to be that for some reason cN, cS, cW, cE, cNE, cSE, cSW, cNW all contain zeros. I've addapted the code from here.

$\endgroup$
0
$\begingroup$

It turned out the problem had nothing to do with the shader. There was a problem with the way I was scaling the image data before it reached the shader.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.