如何将灰度图像转换为原始RGB图像?

时间:2015-05-11 02:18:36

标签: javascript

我可以按照自己的意愿将图像转换为灰度,但我想知道的是如何在转换为灰度后再回到原始图像?

这是我的javascript滤镜图片到灰度

Filters = {};
var img = document.getElementById("background");
var c = document.getElementById('canvas');
var ctx = c.getContext("2d");

Filters.getPixels = function() {
    return ctx.getImageData(0, 0, c.width, c.height);
};

Filters.filterImage = function(filter, var_args) {
    var args = [this.getPixels()];
    args.push(var_args);
    return filter.apply(null, args);
};

function runFilter(filter, arg1, arg2, arg3) {
    var idata = Filters.filterImage(filter, arg1, arg2, arg3);
    c.width = idata.width;
    c.height = idata.height;
    var ctx = c.getContext('2d');
    ctx.putImageData(idata, 0, 0);
}

Filters.grayscale = function(pixels, args) {
    var d = pixels.data;
    for (var i = 0; i < d.length; i += 4) {
        var r = d[i];
        var g = d[i + 1];
        var b = d[i + 2];
        var v = 0.2126 * r + 0.7152 * g + 0.0722 * b;
        d[i] = d[i + 1] = d[i + 2] = v;
        }
    return pixels;
};

1 个答案:

答案 0 :(得分:3)

进入灰度包括丢弃颜色数据。获得回报的唯一方法是将其副本保存在其他地方。