【问题标题】:Understanding how canvas converts an image to black and white了解画布如何将图像转换为黑白
【发布时间】:2011-11-22 01:39:50
【问题描述】:

我发现了这个用于将图像转换为黑白的脚本,效果很好,但我希望能更好地理解代码。我将我的问题以 cmets 的形式放在代码中。

谁能更详细地解释一下这里发生了什么:

function grayscale(src){ //Creates a canvas element with a grayscale version of the color image
    var canvas = document.createElement('canvas');
    var ctx = canvas.getContext('2d');
    var imgObj = new Image();
    imgObj.src = src;
    canvas.width = imgObj.width;
    canvas.height = imgObj.height; 
    ctx.drawImage(imgObj, 0, 0); //Are these CTX functions documented somewhere where I can see what parameters they require / what those parameters mean?
    var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
    for(var y = 0; y < imgPixels.height; y++){
        for(var x = 0; x < imgPixels.width; x++){
            var i = (y * 4) * imgPixels.width + x * 4; //Why is this multiplied by 4?
            var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3; //Is this getting the average of the values of each channel R G and B, and converting them to BW(?)
            imgPixels.data[i] = avg; 
            imgPixels.data[i + 1] = avg; 
            imgPixels.data[i + 2] = avg;
        }
    }
    ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height); 
    return canvas.toDataURL();
}

【问题讨论】:

    标签: javascript image-processing html5-canvas


    【解决方案1】:
    1. 与大多数函数一样,画布函数在an official specification 中进行了描述。此外,MDC 有助于更多“非正式”文章。例如。 MDC 上的drawImage 函数是here

    2. getImageData 函数返回一个对象,该对象包含一个数组,其中包含所有像素的字节数据。每个像素由 4 个字节描述:rgba

      rgb 是颜色分量(红色、绿色和蓝色),alpha 是不透明度。所以每个像素使用4个字节,因此一个像素的数据从pixel_index * 4开始。

    3. 是的,它正在平均这些值。因为在接下来的 3 行中,rgb 都设置为相同的值,您将获得每个像素的灰色(因为所有 3 个分量的数量相同)。

      所以基本上,对于所有像素,这将保持:r === gg === br === b。保持此状态的颜色为灰度(0, 0, 0 为黑色,255, 255, 255 为白色)。

    【讨论】:

      【解决方案2】:
      function grayscale(src){ //Creates a canvas element with a grayscale version of the color image
          //create canvas
          var canvas = document.createElement('canvas');
          //get its context
          var ctx = canvas.getContext('2d');
          //create empty image
          var imgObj = new Image();
          //start to load image from src url
          imgObj.src = src;
          //resize canvas up to size image size
          canvas.width = imgObj.width;
          canvas.height = imgObj.height;
          //draw image on canvas, full canvas API is described here http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html
          ctx.drawImage(imgObj, 0, 0);
          //get array of image pixels
          var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
          //run through all the pixels
          for(var y = 0; y < imgPixels.height; y++){
              for(var x = 0; x < imgPixels.width; x++){
                  //here is x and y are multiplied by 4 because every pixel is four bytes: red, green, blue, alpha
                  var i = (y * 4) * imgPixels.width + x * 4; //Why is this multiplied by 4?
                  //compute average value for colors, this will convert it to bw
                  var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
                  //set values to array
                  imgPixels.data[i] = avg; 
                  imgPixels.data[i + 1] = avg; 
                  imgPixels.data[i + 2] = avg;
              }
          }
          //draw pixels according to computed colors
          ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height); 
          return canvas.toDataURL();
      }
      

      在此函数中使用等于 1/3 的系数,但通常使用的是:0.3R + 0.59G + 0.11B (http://gimp-savvy.com/BOOK/index.html?node54.html) .

      【讨论】:

      • 图片不是还是全彩存储的吗,即使是黑白/灰度?如果我想要将纯黑白画布保存为黑白图像(文件大小要小得多)怎么办?
      猜你喜欢
      • 2011-11-29
      • 1970-01-01
      • 1970-01-01
      • 2019-04-11
      • 2013-01-08
      • 1970-01-01
      • 2013-04-21
      • 2021-02-27
      • 2021-02-26
      相关资源
      最近更新 更多