简体   繁体   English

用 Javascript 去除 HTML Canvas 上的彩色点

[英]Remove Colored dots on HTML Canvas with Javascript

I have a canvas that detects an image background and removes the background or changes its color, however the background is sometimes inconsistent and has anomalies that are very small, they are shown in this image below that is the generated canvas as the purple dots, how can I remove them without effecting the rest of the image.我有一个 canvas 检测图像背景并删除背景或更改其颜色,但是背景有时不一致并且异常非常小,它们如下图所示,即生成的 canvas 为紫色点,如何我可以在不影响图像的 rest 的情况下删除它们吗? 在此处输入图像描述

Once the dots are removed I can then fill in the blanks and make the green "thicker" to cover the rest of the holes within the image.一旦点被移除,我就可以填充空白并使绿色“更厚”以覆盖图像中孔的 rest。 I need to get rid of all of the purple and the green around the purple too.我也需要去掉所有的紫色和紫色周围的绿色。

i = 0;
YNo = 1;
LineNo = 1;
for (i, n = pix.length; i < n; i += 4) 
{
  const r = pix[i],
  g = pix[i + 1],
  b = pix[i + 2];
  if (r > 240 && g < 40 && b > 240 )
  {
    pix[i] = 0;
    pix[i + 1] = 0;
    pix[i + 2] = 0;
    pix[i + 3] = 0;
    x = (i / 4) % imgWidth;
    y = YNo-1;

    ctx4.beginPath();
    ctx4.arc(x, y, 5, 0, 2 * Math.PI, false);
    ctx4.fillStyle = "#000000";
    ctx4.strokeStyle = "#000000";
    ctx4.fill();
    ctx4.stroke();
    ctx4.closePath();
  }
  if(LineNo % imgWidth == 0)
  {
    YNo++;
  }
  LineNo++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM