ios - CIImage(IOS): Adding 3x3 convolution after a monochrome filter somehow restores color

标签 ios edge-detection ciimage

我正在将 ciimage 转换为单色,使用 CICrop 进行裁剪 并运行 sobel 来检测边缘,底部的 #if 部分是 用于显示结果

CIImage *ci = [[CIImage alloc] initWithCGImage:uiImage.CGImage];

CIImage *gray = [CIFilter filterWithName:@"CIColorMonochrome" keysAndValues:
      @"inputImage", ci, @"inputColor", [[CIColor alloc] initWithColor:[UIColor whiteColor]],
      nil].outputImage;



CGRect rect = [ci extent];
rect.origin = CGPointZero;

CGRect cropRectLeft = CGRectMake(0, 0, rect.size.width * 0.2, rect.size.height);
CIVector *cropRect = [CIVector vectorWithX:rect.origin.x Y:rect.origin.y Z:rect.size.width* 0.2 W:rect.size.height];
CIImage *left = [gray imageByCroppingToRect:cropRectLeft];

CIFilter *cropFilter = [CIFilter filterWithName:@"CICrop"];

[cropFilter setValue:left forKey:@"inputImage"];
[cropFilter setValue:cropRect forKey:@"inputRectangle"];

// The sobel convoloution will produce an image that is 0.5,0.5,0.5,0.5 whereever the image is flat
// On edges the image will contain values that deviate from that based on the strength and
// direction of the edge
const double g = 1.;
const CGFloat weights[] = { 1*g, 0, -1*g,
    2*g, 0, -2*g,
    1*g, 0, -1*g};
left = [CIFilter filterWithName:@"CIConvolution3X3" keysAndValues:
      @"inputImage", cropFilter.outputImage,
      @"inputWeights", [CIVector vectorWithValues:weights count:9],
      @"inputBias", @0.5,
      nil].outputImage;

#define VISUALHELP 1
#if VISUALHELP
CGImageRef imageRefLeft = [gcicontext createCGImage:left fromRect:cropRectLeft];
CGContextDrawImage(context, cropRectLeft, imageRefLeft);
CGImageRelease(imageRefLeft);
#endif

现在只要 3x3 卷积不是 ciimage 管道的一部分 我运行边缘检测的图像部分显示为灰色, 但只要 CIConvolution3X3 后缀是处理管道的一部分 颜色神奇地出现了。无论如何都会发生这种情况 如果我使用 CIColorMonochrome 或 CIPhotoEffectMono 前缀来删除颜色。 任何想法如何将颜色一直保持到管道底部? 发送

UPD:毫不奇怪地运行一个粗糙的自定义单色内核,例如这个内核

kernel vec4 gray(sampler image)
{
    vec4 s = sample(image, samplerCoord(image));
    float r = (s.r * .299 + s.g * .587 + s.b * 0.114) * s.a;
    s = vec4(r, r, r, 1);
    return s;
}

而不是使用 apple 的标准单声道过滤器,结果完全相同 当 3x3 卷积是我的 ci 管道的一部分时,颜色返回的问题

最佳答案

这个问题是 CI 卷积运算(例如 CIConvolution3X3、CIConvolution5X5 和 CIGaussianBlur)在输入图像的所有四个 channel 上运行。这意味着,在您的代码示例中,生成的 alpha channel 将为 0.5,而您可能希望它为 1.0。尝试在卷积后添加一个简单的内核,将 alpha 设置回 1。

关于ios - CIImage(IOS): Adding 3x3 convolution after a monochrome filter somehow restores color,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26604162/

相关文章:

ios - UIImageView 立即更改图像

ios - Xcode 与 uncrustify : how to align function declaration to colons?

ios - 可变高度 UITableViewCell 在滚动到列表末尾并旋转后展开

c - OpenCV cvCanny 内存异常

matlab - 检测图像的边缘在 Matlab 中不起作用

swift - CIImage 像素的 RGB 值

ios - 合成两个 CIImages 并在 UIImageView 问题中显示

javascript - Font Awesome 图标未在 Phonegap iOS 应用程序中呈现

Python上升/下降沿示波器式触发器

CIHistogramDisplayFilter 上的 Objective-C 到 Swift 转换问题