【问题标题】:How to Implement a grayscale filter using CoreImage filters?如何使用 CoreImage 滤镜实现灰度滤镜?
【发布时间】:2016-01-06 20:04:56
【问题描述】:

我使用CIColorMonochromeFilter 调整为白色创建了我认为应该是适当的灰度CIFilter。这对我来说看起来很灰度,但我也不是专家,所以我只是想知道这是否是灰度过滤器的准确实现。

我还没有编写代码来转储和测试像素,但是基于图像的格式已经是 RBG,我很好奇输出图像的格式。例如,我将如何知道每个像素是一个组件还是三个组件?

我知道CIImage 只是图像的配方,我必须使用CGImageRef 最终渲染到。我只是不明白我应该期望数据是什么像素格式。

CIFilter * monoChromeFilter = [CIFilter filterWithName:kCIColorMonochromeFilterName];    
[monoChromeFilter setValue:self.inputImage forKey:kCIInputImageKey];

CIColor * white = [CIColor colorWithRed:1.0 green:1.0 blue:1.0];
[monoChromeFilter setValue:white forKey:kCIInputColorKey];

【问题讨论】:

  • 那么问题是什么?只是你不知道如何读取图像的像素值吗?这在 Stack Overflow 上有很好的记录。
  • 我所做的或多或少实际上是一个适当的灰度过滤器,例如单色白色 == 灰度。我对输出格式很好奇,尽管这只是次要问题。

标签: ios objective-c core-image grayscale cifilter


【解决方案1】:

我希望这对你有帮助(它很快。)

static func convertToBlackAndWhite(image:UIImage) -> UIImage?
{
        //first do color controls
        let ciImage = CIImage(image:image)
        let filter = CIFilter(name: "CIColorControls")
        filter.setValue(ciImage, forKey: kCIInputImageKey)
        filter.setValue(0.0, forKey: kCIInputBrightnessKey)
        filter.setValue(0.0, forKey: kCIInputSaturationKey)
        filter.setValue(1.1, forKey: kCIInputContrastKey)

        let intermediateImage = filter.outputImage

        let filter1 = CIFilter(name:"CIExposureAdjust")
        filter1.setValue(intermediateImage, forKey: kCIInputImageKey)
        filter1.setValue(0.7, forKey: kCIInputEVKey)
        let output = filter1.outputImage

        let context = CIContext(options: nil)
        let cgImage = context.createCGImage(output, fromRect: output.extent())

        return UIImage(CGImage: cgImage, scale: image.scale, orientation: image.imageOrientation)
}

【讨论】:

  • 感谢您的回答。您能否详细说明这种方法与 CIColorMonochrome 过滤器方法有何不同以及为什么需要 CIExposureAdjust?
猜你喜欢
  • 1970-01-01
  • 2014-02-11
  • 2013-12-31
  • 1970-01-01
  • 1970-01-01
  • 2018-02-18
  • 1970-01-01
  • 2018-09-19
  • 2012-12-01
相关资源
最近更新 更多