我正在尝试理解苹果为CISpotColor过滤器(使用CIFilter)提供的Limited Documentation,但遇到了一些困难。
简而言之;
1) 是否有更多关于CIFilter的文档,特别是关于CISpotColor的?
2) 鉴于我想要实现的目标(如下图所示,但简单来说:用白色替换所有“看起来不红”的东西,并强制所有“看起来红色(ish)”的东西变成纯红色或黑色),CISpotColor是否是我应该使用的正确滤镜?
3) 如果不是,您建议使用哪些滤镜(或者我应该尝试编写自定义的滤镜)?
4) 如果CISSpotColor是正确的滤镜,那么我应该使用什么参数来实现我的目标。如果我需要使用多个CISpotColor CIFilter的通道,那也没关系,我不希望你为我编码,只需指引我方向即可。
以上问题的更多细节和背景:
上面链接提供了一系列参数、一些默认值以及一个示例的前后图片,但没有生成示例后图像的样本代码,并且没有解释这些参数实际意义或它们的有效范围。
说实话,我不完全确定CISpotColor是我需要的过滤器,除了它的名称和句子“使用专色替换一个或多个颜色范围”之外,没有解释它是如何做到的。
由于它似乎描述了我所需要的过滤器,我选择它作为开始学习如何使用这种方式处理过滤器的起点。
输入图片(视频帧)
期望输出(选项1 - 纯红色 - 使用GIMP创建)
期望的输出(选项2-纯黑色-也是使用GIMP创建的)
我的代码输出(请参见下面的清单)
这接近我所需要的,但似乎没有考虑到原始图像中灰色或“白色”的区域将具有类似数量的红色、绿色和蓝色,而不是主要是红色,这会使它“看起来是红色的”。如果它过滤掉右下角看到的区域,那就可以使用它,因为显然只包括一些红色像素(以及一些绿色和蓝色),使其在原始图像中普遍呈灰色。
以下是用于Cocoa命令行应用程序(Mac OSX)的完整“main.m”
#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <AppKit/AppKit.h>
#import <QuartzCore/QuartzCore.h>
@interface NSImage(saveAsJpegWithName)
- (void) saveAsPNGWithName:(NSString*) fileName;
- (NSImage*) filterEverythingButRed ;
@end
@implementation NSImage(saveAsJpegWithName)
- (void) saveAsPNGWithName:(NSString*) fileName
{
NSData *imageData = [self TIFFRepresentation];
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];
NSDictionary *imageProps = nil;
imageData = [imageRep representationUsingType:NSPNGFileType properties:imageProps];
[imageData writeToFile:fileName atomically:NO];
}
-(NSImage*) filterEverythingButRed {
CIImage *inputImage = [[CIImage alloc] initWithData:[self TIFFRepresentation]];
CIFilter *hf = [CIFilter filterWithName:@"CISpotColor"];
[hf setDefaults];
[hf setValue:inputImage forKey:@"inputImage"];
[hf setValue:[CIColor colorWithRed:1.0 green:0.0 blue:0.0] forKey: @"inputCenterColor1"];
[hf setValue:[CIColor colorWithRed:1.0 green:0.0 blue:0.0] forKey: @"inputReplacementColor1"];
[hf setValue:[NSNumber numberWithFloat:0.1] forKey: @"inputCloseness1"];
[hf setValue:[NSNumber numberWithFloat:1.0] forKey: @"inputContrast1"];
CIImage *outputImage = [hf valueForKey: @"outputImage"];
NSImage *resultImage = [[NSImage alloc] initWithSize:[outputImage extent].size];
NSCIImageRep *rep = [NSCIImageRep imageRepWithCIImage:outputImage];
[resultImage addRepresentation:rep];
return resultImage;
}
@end
int main(int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
if (argc == 1) {
NSString * appname = [NSString stringWithFormat: @"%s", argv[0]];
NSLog(@"Usage: %@ filename", appname);
} else {
NSString * filename = [NSString stringWithFormat: @"%s", argv[1]];
NSFileManager *fm = [NSFileManager defaultManager];
if ([fm fileExistsAtPath:filename]) {
NSLog(@"opening file:%@", filename);
NSImage *img = [[NSImage alloc] initWithContentsOfFile:filename];
[[img filterEverythingButRed]
saveAsPNGWithName:[[filename stringByDeletingPathExtension] stringByAppendingString:@"-red.png"]];
} else {
NSLog(@"file not found:%@", filename);
}
}
[pool release];
return 0;
}