在 CIImage 的边缘上绘制线条覆盖

问题描述 投票:0回答:1

我有一个自定义的 Metal CIKernel,它以红色显示图像上的边缘。在着色器中,我进行卷积来计算索贝尔梯度,然后将颜色返回为红色或原始颜色。但我相信应该有一种方法可以使用内置 CIFilter 或 MPS 着色器来完成相同的操作。但如何呢?

extern "C" half4 drawEdges (coreimage::sampler inputImage, 
                             coreimage::destination dest)
 {
  float2 destCoord = dest.coord();
  float2 srcCoord = inputImage.coord();
  half4 color =  half4(inputImage.sample(srcCoord)); 

  float2 m11Coord = inputImage.transform(destCoord + float2(-1.0,+1.0));

  half3 m11 = half3(inputImage.sample(m11Coord).rgb);   

  float2 m12Coord = inputImage.transform(destCoord + float2(0.0,+1.0));
  half3 m12 = half3(inputImage.sample(m12Coord).rgb); 
  float2 m13Coord = inputImage.transform(destCoord + float2(+1.0,+1.0));
  half3 m13 = half3(inputImage.sample(m13Coord).rgb);  

  float2 m21Coord = inputImage.transform(destCoord + float2(-1.0,0.0));
  half3 m21 = half3(inputImage.sample(m21Coord).rgb); 

  float2 m23Coord = inputImage.transform(destCoord + float2(+1.0,0.0));
  half3 m23 = half3(inputImage.sample(m23Coord).rgb);

  float2 m31Coord = inputImage.transform(destCoord + float2(-1.0,-1.0));
  half3 m31 = half3(inputImage.sample(m31Coord).rgb);

  float2 m32Coord = inputImage.transform(destCoord + float2(0.0,-1.0));
  half3 m32 = half3(inputImage.sample(m32Coord).rgb); 

  float2 m33Coord = inputImage.transform(destCoord + float2(+1.0,+1.0));
  half3 m33 = half3(inputImage.sample(m33Coord).rgb);

  half3 m31m13 = m31 - m13;
  half3 m11m33 = m33 - m11;
  half3 m32m12 = m32 - m12;
  half3 m21m23 = m21 - m23;
  half3 H = m32m12 + m32m12 + m11m33 + m31m13;
  half3 V = m21m23 + m21m23 - m11m33 + m31m13;

  half3 sobel = sqrt(H*H+V*V);
  half sobelLength = length(sobel);

  if (sobelLength > 0.9) {
    half4 redColor = half4(0.5h, 0.h, 0.0, 1.0);
    color = redColor;
 }

  return half4(color);
}


    
ios metal core-image cifilter metal-performance-shaders
1个回答
0
投票

也许像这样的过滤器链大致会做同样的事情:

  ┌───────────────────────────────────────────────────────────────────────────┐
  │                                                                           ▼
input ───► CIPhotoEffectNoire ───► CIEdges ───► CIBlendWithMask ───► CISourceOverCompositing ───► output
                                                       ▲
                      CIImage(color: .red) ────────────┘

请注意,我在这里使用

CIPhotoEffectNoire
将输入转换为灰度,因为
CIEdges
实际上返回彩色的索贝尔边缘,这更难用红色着色。

© www.soinside.com 2019 - 2024. All rights reserved.