如何在JavaScript中对画布图像进行灰度处理?

时间:2018-11-18 18:25:14

标签: javascript html5 canvas grayscale imagedata

我在HTML5画布中有一个图像,尺寸为28x28像素。我使用以下代码将画布的imageData作为RGBA(红色,绿色,蓝色,alpha)值的数组获取:

canvas = document.getElementById('canvas');
ctx = canvas.getContext("2d");
imgData = ctx.getImageData(0, 0, 28, 28);

现在,我想对图像进行灰度处理,以便获得784个值(28x28像素)的数组,其中每个像素都有一个值(而不是四个)。

我发现了很多不同的灰阶公式,有些正在乘以rgb值,有些只是计算平均值-我真的不知道要使用哪个...

我也一直坚持要获得784个值-始终为3136(因为有4个通道)...

谢谢!

2 个答案:

答案 0 :(得分:1)

主要思想是使颜色的红色,绿色和蓝色分量具有相同的值。为此,您需要计算每个像素的亮度。有几种计算亮度的方法。这就是其中之一。

window.onload = function() {
let canvas = document.getElementById("c");

	  let ctx = canvas.getContext("2d");
    canvas.width=50;
    canvas.height=50;

		let srcImg = document.getElementById("sof");
		ctx.drawImage(srcImg, 0, 0, ctx.canvas.width, ctx.canvas.height);
		let imgData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
		let pixels = imgData.data;
		for (var i = 0; i < pixels.length; i += 4) {
		  
		let lightness = parseInt((pixels[i] + pixels[i + 1] + pixels[i + 2])/3);
    
		  pixels[i] = lightness; 
		  pixels[i + 1] = lightness; 
		  pixels[i + 2] = lightness; 
		}
		ctx.putImageData(imgData, 0, 0);
	  }
<canvas id="c"></canvas>
<img src="data:image/jpeg;base64,/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNreQABAAQAAAAZAAD/4QMtaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wLwA8P3hwYWNrZXQgYmVnaW49Iu+7vyIgaWQ9Ilc1TTBNcENlaGlIenJlU3pOVGN6a2M5ZCI/PiA8eDp4bXBtZXRhIHhtbG5zOng9ImFkb2JlOm5zOm1ldGEvIiB4OnhtcHRrPSJBZG9iZSBYTVAgQ29yZ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goIyMjIyg4MjUtLS01Mj09ODg9PUFBQUFBQUFBQUFBQUFBQf/AABEIADQAMgMBIgACEQEDEQH/xACCAAEAAgMBAAAAAAAAAAAAAAAAAwQBAgUGAQEAAwEAAAAAAAAAAAAAAAAAAgMEARAAAQMCAgYHBwUAAAAAAAAAAQACAxEEIRIxQYHREwXwUWGRoSIVcbHBMkJSY2JyIxQ1EQACAgEEAgMBAAAAAAAAAAAAARECITFRYQMSQkGBE3H/2gAMAwEAAhEDEQA/APcIiIAijnnit4zLK7Kwd9TqAVC/fMwxcwtpOJCweZgPkynXt8FC91VN6xlpaxuTp1uzS0nCb0nY6aKOGaOeJssZq1w2g6wfYpFJNNSvki004eGgiIunAiLUvBa4xkPc0HAGvm6kBRn5haiZ9pdRkR6MzhVrvjtVUsksKzWxFzYSfOyuagPXv71vJfyFmS/sSW66YjZXeqrDbcSthcPglcQOFICWuJ1VFfFYr3lzKb9fS64h6m6lIUQ0vZT50fMrNS3y0PZOf638ljKMxJwMbh9J/V7wuso4WRRNEMeUFuJa2gxOk0Ui1ddPCsffC/hl7b+dp+uXy+QiIplZkaVzrQS8G64FBLmOSujN2roa1S9OFSRM8VNcMNKPKOpw5IcvPfuj8NyR23MzPG+dsLmtcC4hozAdhopvT/zv6bU9P/O/ptVX4rE27HG9i39nmKdanapmL/Rm/buVxVoLQQSGTiF5IpirKtKQiIgCIiAIiIAiIgCIiA//2Q==" id="sof" />

更新

这是wikipedia

中亮度的另一个公式
let lightness = parseInt(pixels[i]*.299 + pixels[i + 1]*.587 + pixels[i + 2]*.114);

这是我在Google中找到的另一个公式:

let lightness = parseInt(3*pixels[i] + 4*pixels[i + 1] + pixels[i + 2] >>> 3);

答案 1 :(得分:1)

您可以直接为渲染上下文设置过滤器。它会比计算每个像素颜色更简单。

为此,您应该使用 ctx.filter = 'grayscale(1)';

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

img.onload = function() {
  canvas.width = img.width;
  canvas.height = img.height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