Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在UIImage/CIImage中隔离单一颜色?_Ios_Image_Cifilter_Ciimage - Fatal编程技术网

Ios 如何在UIImage/CIImage中隔离单一颜色?

Ios 如何在UIImage/CIImage中隔离单一颜色?,ios,image,cifilter,ciimage,Ios,Image,Cifilter,Ciimage,我想在UIImage中分离出某种颜色的元素,这样我就可以在另一个工具中处理该图像 假设我有下图,我想分离所有红色元素 输入: 所需输出:所有红色元素 我可以采取什么步骤来实现这一点?我想这可能是CIFilter中的某个东西,但还没有找到合适的过滤器。有没有一个简单的方法可以实现这一点呢?好的,得到一些帮助并解决它!以下是如何做到这一点: // First, turn our UIImage into a CIImage let ciImage = CIImage(image: image)!

我想在UIImage中分离出某种颜色的元素,这样我就可以在另一个工具中处理该图像

假设我有下图,我想分离所有红色元素

输入:

所需输出:所有红色元素


我可以采取什么步骤来实现这一点?我想这可能是CIFilter中的某个东西,但还没有找到合适的过滤器。有没有一个简单的方法可以实现这一点呢?

好的,得到一些帮助并解决它!以下是如何做到这一点:

// First, turn our UIImage into a CIImage
let ciImage = CIImage(image: image)!

// Next, let's make a CIVector for the color we want.
let colorToIsolate = CIVector(x: 0.239, y: 0.951, z: 0.937) // normalized RGB

// OK, here's where it gets interesting. 
// We're going to write a CIColorKernel that maps every pixel in the image. 
// If it's the color we want, then we turn it white, otherwise, black.
// This is iOS-10 friendly; in iOS 11 there's a non-string-based way,
// which helps with compiler type-checking and non-runtime error detection.
// NOTE: The 0.5 value is a tolerance; adjust and adapt it to your needs for your task. 
// I was surprised that we needed it that large to get the desired effect; 
// had hoped that I could use a tighter value like 0.01

let kernelString = "kernel vec4 filterColorMatch(sampler image, vec3 match) {" +
        "vec2 dc = destCoord();" +
        "vec3 color = sample(image, samplerTransform(image, dc)).rgb; " +
        "return distance(color, match) < 0.5 ? vec4(1,1,1,1) : vec4(0,0,0,1); }"

let colorIsolate = CIColorKernel(string: kernelString)

// Now we can run our kernel, and extract the image!
let after = colorIsolate?.apply(
  withExtent: ciImage.extent, 
  arguments: [ciImage, colorToIsolate]
)

let result = UIImage(ciImage: after)