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

时间:2022-02-23 21:18:29

I can convert an image to grayscale as I wish, but what I was wondering is about how to go back to original image after converting to grayscale?

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

Here is my javascript filter image to grayscale

这是我的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 个解决方案

#1


Going to grayscale involves throwing away the color data. The only way to get that back is to have saved a copy of it elsewhere.

去灰度涉及丢弃颜色数据。获得回报的唯一方法是将其副本保存在别处。

#1


Going to grayscale involves throwing away the color data. The only way to get that back is to have saved a copy of it elsewhere.

去灰度涉及丢弃颜色数据。获得回报的唯一方法是将其副本保存在别处。