0

我一直在尝试想出一种简单的方法来使用 Core Image 过滤器来抠出幻灯片的背景并抠出某种颜色的文本。我当前的项目有效,但有一个警告:我试图删除的文本已经消失,但光环仍然存在。

这是显示问题的屏幕截图:

在此处输入图像描述

据我所知,“主要幻灯片内容”周围有一个蓝色光环,但这可能没问题,因为主要内容仍在屏幕上。不过,我想移除“测试演讲者笔记”周围的光环,因为它打算完全关闭。

在我的脑海中,我觉得解决方案是获取绿色文本的蒙版,然后稍微扩展该蒙版,然后在乘法过滤器中使用它来抠出光晕,但我不知道如何用 Core Image 来解决这个问题。我将不胜感激有关在这里采取哪条道路的任何指导。这是我的 applicationDidFinishLaunching 方法以及 keying 和 blending 方法,这是完成工作的两种方法。所有代码都在 GitHub 上的项目文件中,在 [此处] 找到:https ://github.com/nspaul/chromakeytest

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSImage *img = [NSImage imageNamed:@"testSlide.png"];
    NSImage *actsBG = [NSImage imageNamed:@"background.png"];
    NSImage *keyed = img;

    //Key out the green
    keyed = [self keyImage:keyed minHue:0.25 maxHue:0.4];

    //Key out the blue background
    keyed = [self keyImage:keyed minHue:0.6 maxHue:0.7];

    //Put the test slide over the background slide
    keyed = [self blendImage:keyed withBackgroundImage:actsBG];

    [_topImageView setImage:img];
    [_backgroundImageView setImage:actsBG];
    [_keyedImageView setImage:keyed];
}




-(NSImage *)blendImage:(NSImage *)img withBackgroundImage:(NSImage *)bgimg {
    CIImage *inputImage = [[CIImage alloc] initWithData:[img TIFFRepresentation]];
    CIImage *bgInputImage = [[CIImage alloc] initWithData:[bgimg TIFFRepresentation]];
    CIFilter *blendFilter = [CIFilter filterWithName:@"CISourceOverCompositing"];

    [blendFilter setValue:inputImage forKey:@"inputImage"];
    [blendFilter setValue:bgInputImage forKey:@"inputBackgroundImage"];

    CIImage *outputImage = [blendFilter valueForKey:@"outputImage"];
    NSImage *resultImage = [[NSImage alloc] initWithSize:[outputImage extent].size];
    NSCIImageRep *rep = [NSCIImageRep imageRepWithCIImage:outputImage];
    [resultImage addRepresentation:rep];

    return resultImage;
}



-(NSImage *) keyImage:(NSImage *)imageToKey minHue:(float)minHueAngle maxHue:(float)maxHueAngle {
    CIImage *ciImage = [[CIImage alloc] initWithData:[imageToKey TIFFRepresentation]];


    // Allocate memory
    const unsigned int size = 64;
    float *cubeData = (float *)malloc (size * size * size * sizeof (float) * 4);
    float rgb[3], hsv[3], *c = cubeData;

    // Populate cube with a simple gradient going from 0 to 1
    for (int z = 0; z < size; z++){
        rgb[2] = ((double)z)/(size-1); // Blue value
        for (int y = 0; y < size; y++){
            rgb[1] = ((double)y)/(size-1); // Green value
            for (int x = 0; x < size; x ++){
                rgb[0] = ((double)x)/(size-1); // Red value
                // Convert RGB to HSV
                // You can find publicly available rgbToHSV functions on the Internet
                rgbToHSV(rgb, hsv);
                // Use the hue value to determine which to make transparent
                // The minimum and maximum hue angle depends on
                // the color you want to remove
                float alpha = (hsv[0] > minHueAngle && hsv[0] < maxHueAngle) ? 0.0f: 1.0f;
                // Calculate premultiplied alpha values for the cube
                c[0] = rgb[0] * alpha;
                c[1] = rgb[1] * alpha;
                c[2] = rgb[2] * alpha;
                c[3] = alpha;
                c += 4; // advance our pointer into memory for the next color value
            }
        }
    }

    NSLog(@"cubeData: %p",cubeData);

    // Create memory with the cube data
    NSData *data = [NSData dataWithBytesNoCopy:cubeData
                                        length:(64 * 64 * 64 * sizeof (float) * 4)
                                  freeWhenDone:YES];
    CIFilter *colorCube = [CIFilter filterWithName:@"CIColorCube"];
    [colorCube setValue:[NSNumber numberWithInt:size] forKey:@"inputCubeDimension"];
    // Set data for cube
    [colorCube setValue:data forKey:@"inputCubeData"];


    [colorCube setValue:ciImage forKey:kCIInputImageKey];

    CIImage *outImage = [colorCube valueForKey:@"outputImage"];

    NSImage *resultImage = [[NSImage alloc] initWithSize:[outImage extent].size];
    NSCIImageRep *rep = [NSCIImageRep imageRepWithCIImage:outImage];
    [resultImage addRepresentation:rep];

    return resultImage;
}
4

0 回答 0