【发布时间】:2014-07-14 03:22:34
【问题描述】:
请注意:这是针对在 Mac OSX 上运行的 Cocoa 命令行应用程序,不是 iOS 应用程序
我在尝试理解 Apple 为 CISpotColor 过滤器(使用 CIFilter )提供的Limited Documentation 时遇到了一些麻烦。
TL 博士;
1) 我在某处缺少关于 CIFilter 的更多文档,特别是 CISpotColor?
2) 鉴于我想要实现的目标(如下图所示,但简要描述:用白色替换所有不“看起来红色”的东西,并将所有“看起来红色(ish)”的东西强制为纯红色,或只是黑色),CISpotColor 是我应该使用的正确滤镜吗?
3) 如果没有,您建议使用什么过滤器(或者我应该尝试编写自定义过滤器吗?)
4) 如果 CISSpotColor 是正确的过滤器,我应该使用什么参数来实现我想要实现的目标。如果我需要使用 CISpotColor CIFilter 的多次传递,那很好,我不希望您为我编写代码,只需为我指明正确的方向即可。
上述问题的更多细节和背景:
link above 给出了参数列表、一些默认值和图片前后的示例,但没有生成后图片示例的示例代码,也没有解释参数的实际含义或它们的有效性范围是。
说实话,我不完全确定 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;
}
【问题讨论】:
-
我找到了一个似乎可行的解决方法,但不清楚为什么 - 如果您设置为所有 inputCenterColor1、inputCenterColor2 和 inputCenterColor1 定义相同的设置(使用 color=[1.0,0.0,0.0 ],closeness = 0.5 and contrast=1.0),它有效。不知道为什么你需要做所有 3,如果你有任何见解,会有所帮助。它还可以让您对绿色和蓝色执行相同的操作,并且它似乎可以根据需要正确过滤(供我使用)。我将把这个问题留给其他搜索者/发帖者。
标签: objective-c xcode macos image-processing cifilter