我一直试图想出一种简单的方法来使用Core Image过滤器来键出幻灯片的背景并键出某种颜色的文本。我当前的项目有效,但有一点需要注意:我想删除的文本已经消失,但光环仍然存在。
以下是显示问题的屏幕截图:
据我所知,“主要幻灯片内容”周围有一个蓝色光环,但这可能还不错,因为主要内容仍在屏幕上。我想删除“测试演讲者笔记”周围的光环,因为那是打算完全键入的。
在我脑海中,我觉得解决方案是获取绿色文本的掩码,然后稍微扩展该掩码,然后在乘法过滤器中使用它来键入光晕,但我不知道知道如何使用Core Image处理此问题。我将不胜感激任何指导。这是我的applicationDidFinishLaunching方法和键控和混合方法,它们是完成这项工作的两种方法。所有代码都在GitHub的项目文件中,找到[here]: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;
}