拉普拉斯图像滤镜

时间:2013-01-28 21:12:27

标签: html5 canvas

我从“Making image filters with Canvas”中获取了拉普拉斯的例子,但我无法理解在以下几行中使用Math.min()函数。谁能向我解释拉普拉斯是怎么回事?

var weights = [-1,-1,-1,
   -1, 8,-1,
   -1,-1,-1];
        var opaque = true;
        var side = Math.round(Math.sqrt(weights.length));
        var halfSide = Math.floor(side/2);
    var imgd = context.getImageData(0, 0, canvas.width, canvas.height);

    var src = imgd.data;
    var sw = canvas.width;
    var sh = canvas.height;

    var w = sw;
    var h = sh;
    var output = contextNew.createImageData(w, h);
    var dst = output.data;

    var alphaFac = opaque ? 1 : 0;

    for (var y=0; y<h; y++) {
        for (var x=0; x<w; x++) {
            var sy = y;
            var sx = x;
            var dstOff = (y*w+x)*4;
            var r=0, g=0, b=0, a=0;
            for (var cy=0; cy<side; cy++) {
                for (var cx=0; cx<side; cx++) {
                    var scy = Math.min(sh-1, Math.max(0, sy + cy - halfSide));
                    var scx = Math.min(sw-1, Math.max(0, sx + cx - halfSide));
                    var srcOff = (scy*sw+scx)*4;
                    var wt = weights[cy*side+cx];
                    r += src[srcOff] * wt;
                    g += src[srcOff+1] * wt;
                    b += src[srcOff+2] * wt;
                    a += src[srcOff+3] * wt;
                }
            }
            dst[dstOff] = r;
            dst[dstOff+1] = g;
            dst[dstOff+2] = b;
            dst[dstOff+3] = a + alphaFac*(255-a);
        }
    }

1 个答案:

答案 0 :(得分:0)

其算法类似于

for y = 0 to imageHeight 
for x = 0 to imageWidth
sum = 0
for i = -h to h
for j = -w to w 
sum = sum + k(j, i) * f(x – j, y – i) 
end for j 
end for i 
g(x, y) = sum end for x end for y