javascript - 使用 Javascript 或 WebAssembly 进行图像插值

标签 javascript image-processing canvas interpolation webassembly

我有一张在 Canvas 上逐像素绘制的图片。图像的尺寸:512 * 256。当我将这张图片设为1536 * 768时,图像看起来很糟糕。我想我可以使用插值来解决这个问题。但我找不到好的 javascript 库。它在我找到的库中不起作用。

我的代码如下。我根据值找到等效颜色并将该颜色反射(reflect)在屏幕上。

function ImageData(ctx, points) {

    var data = ctx.createImageData(512, 256);

    for (var i = 0; i < data.data.length; i += 4)
    {
        var colorIndex = GetColorCode(points[i/4], minColorIndex, maxColorIndex);
        var color = colormap[colorIndex];

        data.data[i+0] = color[0];
        data.data[i+1] = color[1];
        data.data[i+2] = color[2];
        data.data[i+3] = 255;
    }

    return data;
}

function Draw (points)
{
    var canvas = document.getElementById("myCanvas");
    canvas.width = width;
    canvas.height = height;
    var ctx = canvas.getContext("2d");
    var data = ImageData(ctx, points);

    ctx.putImageData(data, 0, 0);
}

有谁可以帮我解决这个问题吗?

最佳答案

重新采样 ImageData 的简单方法是将其置于实际大小,然后在其自身上绘制目标 Canvas ,重新缩放,只需使用 drawImage

const canvas = document.getElementById( 'canvas' );
const ctx = canvas.getContext( '2d' );
// prepare our 50*50 ImageData
const data = new Uint32Array( 50 * 50 );
crypto.getRandomValues( data ); // make some noise
const imgData = new ImageData( new Uint8ClampedArray( data.buffer ), 50, 50 );
// put it on canvas @scale(1)
ctx.putImageData( imgData, 0, 0 );
// at this step, our canvas only contains this 50*50 ImageData

// resize using the default interpolation

// we first change the composite mode to be able to clear while drawing
ctx.globalCompositeOperation = "copy";
ctx.scale( 10, 10 ); // so 500x500
// newest browsers can control interpolation quality
ctx.imageSmoothingQuality = "high";
ctx.drawImage( canvas, 0, 0 );

// clean
ctx.globalCompositeOperation = "source-over";
ctx.setTransform( 1, 0, 0, 1, 0, 0);
<canvas id="canvas" width="500" height="500"></canvas>

为了避免插值(最近邻),请执行相同的操作,但将 imageSmoothingEnabled 设置为 false:

const canvas = document.getElementById( 'canvas' );
const ctx = canvas.getContext( '2d' );

const data = new Uint32Array( 50 * 50 );
crypto.getRandomValues( data ); // make some noise
const imgData = new ImageData( new Uint8ClampedArray( data.buffer ), 50, 50 );

ctx.putImageData( imgData, 0, 0 );

ctx.globalCompositeOperation = "copy";
ctx.scale( 10, 10 ); // so 500x500

ctx.imageSmoothingEnabled = false;
ctx.drawImage( canvas, 0, 0 );

// clean
ctx.imageSmoothingEnabled = true;
ctx.globalCompositeOperation = "source-over";
ctx.setTransform( 1, 0, 0, 1, 0, 0);
<canvas id="canvas" width="500" height="500"></canvas>

关于javascript - 使用 Javascript 或 WebAssembly 进行图像插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59151508/

相关文章:

javascript - 将canvas图片插入MySQL数据库

javascript - Highcharts 中的力导向图失去了线条

python - 使用Python OpenCV在图像路径(中文,日文,韩文)中读取/加载带有Unicode字符的图像

javascript - Circle - Canvas 获取 lineWidth 的像素颜色

c++ - 使用 OpenCV 从图像中检测最大矩形

image-processing - 图像分类 - 检测图像类似于卡通

javascript 和 html canvas 动画图像发射器

javascript - Summernote 非引导版本滚动问题

javascript - Lodash。通过按键智能合并两个对象

javascript - jQuery - 水平滚动时输出scrollLeft