29

我正在使用 Core Image 并希望在所选图像上产生黑白效果。

理想情况下,我希望能够访问 Photoshop 上可用的相同类型的选项,即红色、青色、绿色、蓝色和洋红色。目标是创建不同类型的黑白效果。

有谁知道什么过滤器最适合操纵这些选项?如果没有,是否有人知道使用其他滤镜创建黑白效果的好方法?

谢谢

奥利弗

4

8 回答 8

57
- (UIImage *)imageBlackAndWhite
{
    CIImage *beginImage = [CIImage imageWithCGImage:self.CGImage];

    CIImage *blackAndWhite = [CIFilter filterWithName:@"CIColorControls" keysAndValues:kCIInputImageKey, beginImage, @"inputBrightness", [NSNumber numberWithFloat:0.0], @"inputContrast", [NSNumber numberWithFloat:1.1], @"inputSaturation", [NSNumber numberWithFloat:0.0], nil].outputImage;
    CIImage *output = [CIFilter filterWithName:@"CIExposureAdjust" keysAndValues:kCIInputImageKey, blackAndWhite, @"inputEV", [NSNumber numberWithFloat:0.7], nil].outputImage; 

    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef cgiimage = [context createCGImage:output fromRect:output.extent];
    //UIImage *newImage = [UIImage imageWithCGImage:cgiimage];
    UIImage *newImage = [UIImage imageWithCGImage:cgiimage scale:image.scale orientation:image.imageOrientation];
    CGImageRelease(cgiimage);

    return newImage;
}

Upd.:因为iOS6CIColorMonochrome滤镜,但我玩了它,发现它不如我的好。

于 2012-04-05T18:13:48.817 回答
14

这是 CIColorMonochrome 的示例

- (UIImage *)imageBlackAndWhite
{
    CIImage *beginImage = [CIImage imageWithCGImage:self.CGImage];

    CIImage *output = [CIFilter filterWithName:@"CIColorMonochrome" keysAndValues:kCIInputImageKey, beginImage, @"inputIntensity", [NSNumber numberWithFloat:1.0], @"inputColor", [[CIColor alloc] initWithColor:[UIColor whiteColor]], nil].outputImage;

    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef cgiimage = [context createCGImage:output fromRect:output.extent];
    UIImage *newImage = [UIImage imageWithCGImage:cgiimage scale:self.scale orientation:self.imageOrientation];

    CGImageRelease(cgiimage);

    return newImage;
}
于 2012-12-03T17:30:00.067 回答
8

这是转换为 Swift(iOS 7 及更高版本)的最受好评的解决方案:

func blackAndWhiteImage(image: UIImage) -> UIImage {
    let context = CIContext(options: nil)
    let ciImage = CoreImage.CIImage(image: image)!

    // Set image color to b/w
    let bwFilter = CIFilter(name: "CIColorControls")!
    bwFilter.setValuesForKeysWithDictionary([kCIInputImageKey:ciImage, kCIInputBrightnessKey:NSNumber(float: 0.0), kCIInputContrastKey:NSNumber(float: 1.1), kCIInputSaturationKey:NSNumber(float: 0.0)])
    let bwFilterOutput = (bwFilter.outputImage)!

    // Adjust exposure
    let exposureFilter = CIFilter(name: "CIExposureAdjust")!
    exposureFilter.setValuesForKeysWithDictionary([kCIInputImageKey:bwFilterOutput, kCIInputEVKey:NSNumber(float: 0.7)])
    let exposureFilterOutput = (exposureFilter.outputImage)!

    // Create UIImage from context
    let bwCGIImage = context.createCGImage(exposureFilterOutput, fromRect: ciImage.extent)
    let resultImage = UIImage(CGImage: bwCGIImage, scale: 1.0, orientation: image.imageOrientation)

    return resultImage
}
于 2015-12-15T10:24:01.463 回答
7

为了创建纯单色效果,我CIColorMatrix将 R、G 和 B 矢量参数全部设置为 (0.2125, 0.7154, 0.0721, 0),并将 alpha 和偏置矢量保留为默认值。

这些值是我在某些时候在互联网上查找的 RGB 到灰度转换系数。通过改变这些系数,您可以改变输入通道的贡献。通过缩放向量的每个副本,并可选择设置偏置向量,您可以对输出进行着色。

于 2012-04-05T15:11:26.833 回答
4

关于建议使用的答案CIColorMonochrome:iOS7(和 OS X 10.9)现在提供了一些专用的灰度过滤器:

  • CIPhotoEffectTonal

    在不显着改变对比度的情况下模仿黑白摄影胶片。

  • CIPhotoEffectNoir:

    用夸张的对比度模仿黑白摄影胶片

来源:https ://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html

于 2015-06-25T16:00:49.477 回答
3

这是@Shmidt 最喜欢的答案,它被写成一个 UIImage 扩展,并在 Swift 中进行了性能更新:

import CoreImage

extension UIImage
{
    func imageBlackAndWhite() -> UIImage?
    {
        if let beginImage = CoreImage.CIImage(image: self)
        {
            let paramsColor: [String : AnyObject] = [kCIInputBrightnessKey: NSNumber(double: 0.0),
                                                     kCIInputContrastKey:   NSNumber(double: 1.1),
                                                     kCIInputSaturationKey: NSNumber(double: 0.0)]
            let blackAndWhite = beginImage.imageByApplyingFilter("CIColorControls", withInputParameters: paramsColor)

            let paramsExposure: [String : AnyObject] = [kCIInputEVKey: NSNumber(double: 0.7)]
            let output = blackAndWhite.imageByApplyingFilter("CIExposureAdjust", withInputParameters: paramsExposure)

            let processedCGImage = CIContext().createCGImage(output, fromRect: output.extent)
            return UIImage(CGImage: processedCGImage, scale: self.scale, orientation: self.imageOrientation)
        }
        return nil
    }
}
于 2016-06-10T09:11:25.180 回答
1

macOS (NSImage) Swift 3 版本的@FBente 转换@Shmidt 的答案:

extension NSImage
{
    func imageBlackAndWhite() -> NSImage?
    {
        if let cgImage = self.cgImage(forProposedRect: nil, context: nil, hints: nil)
        {
            let beginImage = CIImage.init(cgImage: cgImage)
            let paramsColor: [String : AnyObject] = [kCIInputBrightnessKey: NSNumber(value: 0.0),
                                                     kCIInputContrastKey:   NSNumber(value: 1.1),
                                                     kCIInputSaturationKey: NSNumber(value: 0.0)]
            let blackAndWhite = beginImage.applyingFilter("CIColorControls", withInputParameters: paramsColor)

            let paramsExposure: [String : AnyObject] = [kCIInputEVKey: NSNumber(value: 0.7)]
            let output = blackAndWhite.applyingFilter("CIExposureAdjust", withInputParameters: paramsExposure)

            if let processedCGImage = CIContext().createCGImage(output, from: output.extent) {
                return NSImage(cgImage: processedCGImage, size: self.size)
            }
        }
        return nil
    }
}
于 2016-12-13T05:19:48.303 回答
0

我已经尝试过 Shmidt 解决方案,但在我看来它在 iPad Pro 上暴露得太多了。我只使用他解决方案的第一部分,没有曝光过滤器:

- (UIImage *)imageBlackAndWhite
{
    CIImage *beginImage = [CIImage imageWithCGImage:self.CGImage];

    CIImage *blackAndWhite = [CIFilter filterWithName:@"CIColorControls" keysAndValues:kCIInputImageKey, beginImage, @"inputBrightness", [NSNumber numberWithFloat:0.0], @"inputContrast", [NSNumber numberWithFloat:1.1], @"inputSaturation", [NSNumber numberWithFloat:0.0], nil].outputImage;
    CIImage *output = [blackAndWhite valueForKey:@"outputImate"]; 

    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef cgiimage = [context createCGImage:output fromRect:output.extent];
    //UIImage *newImage = [UIImage imageWithCGImage:cgiimage];
    UIImage *newImage = [UIImage imageWithCGImage:cgiimage scale:image.scale orientation:image.imageOrientation];
    CGImageRelease(cgiimage);

    return newImage;
}
于 2017-02-07T11:18:15.573 回答