核心图像滤镜背景

时间:2015-06-21 18:11:20

标签: ios background core-image cifilter

我正在尝试使用Core Image创建直方图。我成功地让它显示(代码如下)。然而,histrogram的背景是灰色的。我怎样才能明白它?

    //Show Histogram
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(kCFAllocatorDefault, sampleBuffer, kCMAttachmentMode_ShouldPropagate);
CIImage *ciImage = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer options:(__bridge NSDictionary *)attachments];


NSUInteger count = 256;
count = count <= 256 ? count : 256;
count = count >= 1 ? count : 1;


NSDictionary *params = @{kCIInputImageKey: ciImage,
                         kCIInputExtentKey: [CIVector vectorWithCGRect:[ciImage extent]],
                         @"inputCount": @(count), @"inputScale": @(100),
                         };

CIFilter *filter = [CIFilter filterWithName:@"CIAreaHistogram"
                        withInputParameters:params];


CIImage *outImage = [filter outputImage];
//---------------------------------------------

CIContext *context = [CIContext contextWithOptions:nil];

NSDictionary *params2 = @{
                          kCIInputImageKey: outImage,
                          };
CIFilter *filter2 = [CIFilter filterWithName:@"CIHistogramDisplayFilter"
                         withInputParameters:params2];

CIImage *outputImage = [filter2 outputImage];
CGRect outExtent = [outputImage extent];
CGImageRef cgImage = [context createCGImage:outputImage
                                   fromRect:outExtent];


UIImage *outImage2 = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);

// resize
UIImage *resized = [self resizeImage:outImage2
                         withQuality:kCGInterpolationNone
                                rate:2.5];

2 个答案:

答案 0 :(得分:1)

据我所知,没有参数可以自定义此滤镜产生的直方图的背景颜色。它就是这样的。相同的颜色甚至出现在Apple的documentation of the filter上(查看示例输出)。

我可以想到两种可能的解决方案:

  1. 编写自己的代码或寻找另一个库来生成直方图;一个可以让你更好地控制输出的那个。
  2. 如果您希望保留CoreImage代码,则需要对直方图图像执行其他处理。一种可能的解决方案是遍历其像素并检查直方图背景(137,137,137)的特定灰色,然后将它们设置为透明。我不知道这是否是最佳解决方案,但它确实有效。
  3. 使用第二种解决方案,这是一种将灰色变为透明的方法:

    -(UIImage*)removeColorFromImage:(UIImage*)sourceImage grayLevel:(int)grayLevel
    {
        int width = sourceImage.size.width * sourceImage.scale;
        int height = sourceImage.size.height * sourceImage.scale;
        CGFloat scale = sourceImage.scale;
    
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, width * 4, colorSpace, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedFirst);
        CGColorSpaceRelease(colorSpace);
    
        CGContextDrawImage(context, CGRectMake(0, 0, width, height), sourceImage.CGImage);
    
        unsigned int *colorData = CGBitmapContextGetData(context);
    
        for (int i = 0; i < width * height; i++)
        {
            unsigned int color = *colorData;
    
            short a = color & 0xFF;
            short r = (color >> 8) & 0xFF;
            short g = (color >> 16) & 0xFF;
            short b = (color >> 24) & 0xFF;
    
            if ((r == grayLevel) && (g == grayLevel) && (b == grayLevel))
            {
                a = r = g = b = 0;
                *colorData = (unsigned int)(r << 8) + ((unsigned int)(g) << 16) + ((unsigned int)(b) << 24) + ((unsigned int)(a));
            }
    
            colorData++;
        }
    
        CGImageRef output = CGBitmapContextCreateImage(context);
        UIImage* retImage = [UIImage imageWithCGImage:output scale:scale orientation:UIImageOrientationUp];
    
        CGImageRelease(output);
        CGContextRelease(context);
    
        return retImage;
    }
    

    现在在最终图像上调用此方法:

    resized = [self removeColorFromImage:resized grayLevel:137]; 
    

答案 1 :(得分:0)

顺便说一下,创建直方图图像需要一行:

CIImage *histogramImage = [CIFilter filterWithName:@"CIHistogramDisplayFilter" keysAndValues:kCIInputImageKey, self.inputImage, @"inputHeight", @100.0, @"inputHighLimit", @1.0, @"inputLowLimit", @0.0, nil].outputImage;