代码之家  ›  专栏  ›  技术社区  ›  lalithkumar

需要边缘点作为图像

  •  -2
  • lalithkumar  · 技术社区  · 7 年前

    cannyJS . 问题是cannyJS,它将输出作为带有边缘点的矩形图像。我只想要边缘作为图像。这可能吗?

    例如,如果我上传下面的图片

    我得到了这个输出:

    预期结果如下:

    或者有没有其他方法可以从图像中获取svg路径,从而得到这样的结果?

    1 回复  |  直到 7 年前
        1
  •  3
  •   lalithkumar    7 年前

    img {
      width: 400px;
    }
    
    .blob {
      background-color: white;
      padding: 40px;
      filter: url(#blobby);
    }
    
    /* Hide the SVG element */
    svg {
      width: 0px;
      height: 0px;
      position: absolute;
      left: -999px;
    }
    <img src="https://i.stack.imgur.com/dreFV.jpg"/>
    <br>
    <div class="blob">
      <img src="https://i.stack.imgur.com/dreFV.jpg"/>
    </div>
    
    <svg>
      <defs>
        <filter id="blobby" color-interpolation-filters="sRGB">
          <!-- Convert to greyscale -->
          <feColorMatrix type="saturate" values="0"/>
          <!-- Threshhold to black or white -->
          <feComponentTransfer>
            <feFuncR type="discrete" tableValues="0 0 0 0 0 1"/>
            <feFuncG type="discrete" tableValues="0 0 0 0 0 1"/>
            <feFuncB type="discrete" tableValues="0 0 0 0 0 1"/>
          </feComponentTransfer>
          <!-- Morphology filter to "erode" (shrink) the white areas -->
          <feMorphology operator="erode" radius="8"/>
          <!-- Blur to cause image to "spread" -->
          <feGaussianBlur stdDeviation="10"/>
          <!-- High contrast to threshhold again -->
          <feComponentTransfer>
            <feFuncR type="discrete" tableValues="0 0 0 0 0 1"/>
            <feFuncG type="discrete" tableValues="0 0 0 0 0 1"/>
            <feFuncB type="discrete" tableValues="0 0 0 0 0 1"/>
          </feComponentTransfer>
        </filter>
      </defs>
    </svg>

    过滤器中的步骤包括:

    • 步骤1&2: 将彩色图像转换为灰度,然后仅将阈值转换为黑色或白色。
    • 第3步:使用“形态学”滤芯扩大黑色区域。这使得图像的薄部分(如文本)更加粗体。
    • 第四步&5: 模糊黑色区域并再次阈值。这使最终结果看起来更圆润。

    该滤波器中的值在某种程度上调整为该特定图像。如果需要对其他图像执行此操作,则可能需要稍微调整过滤器元素值。

    还要注意,我把图像放在了一个白色的 <div> ,并将过滤器应用于div,而不是图像。这是因为过滤器会导致图像内容“扩展”,我们需要避免将其剪裁到(较小的)图像边界。