我正在尝试使用Lanczos Scale Transform滤镜使用Core Image进行图像缩放。
当我进行放大时很好。但是在按比例缩小并保存为JPEG时,我发现了一类图像,这些图像会产生奇怪的噪声伪像和行为。
对于inputScale的倍数至0.5:0.5、0.25、0.125等,总是很好的。
对于其他inputScale值,它已损坏。
当我保存为TIFF,PNG,JPEG2000或在屏幕上绘制时-很好。
当我保存为JPG或BMP时-它坏了。
我上传了示例图片:
(original, 4272x2848, 3.4Mb)
[http://www.zoomfoot.com/get/package/517e2423-7795-4239-a166-03d507ec51d8]
(scaled with noise, 1920x1280, 2.2Mb)
[http://www.zoomfoot.com/get/package/6eb64d33-3a30-4e8d-9953-67ce1e7d7ef9]
同样,在“日落”图像上也可以很好地重现。
我尝试了几种其他方法来缩放图像。使用CIAffineTransform并使用NSImage本身进行绘制。两者都提供结果而没有任何噪音。
这是我用于缩放和保存的代码:
================= Lanczos ===============
- (NSImage *) scaleLanczosImage:(NSString *)path
Width:(int) desiredWidth
Height:(int) desiredHeight
{
CIImage *image=[CIImage imageWithContentsOfURL:
[NSURL fileURLWithPath:path]];
CIFilter *scaleFilter = [CIFilter filterWithName:@"CILanczosScaleTransform"];
int originalHeight=[image extent].size.height;
int originalWidth=[image extent].size.width;
float yScale=(float)desiredHeight / (float)originalHeight;
float xScale=(float)desiredWidth / (float)originalWidth;
float scale=fminf(yScale, xScale);
[scaleFilter setValue:[NSNumber numberWithFloat:scale]
forKey:@"inputScale"];
[scaleFilter setValue:[NSNumber numberWithFloat:1.0]
forKey:@"inputAspectRatio"];
[scaleFilter setValue: image
forKey:@"inputImage"];
image = [scaleFilter valueForKey:@"outputImage"];
NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] initWithCIImage:image];
NSMutableDictionary *options=[NSMutableDictionary dictionaryWithObject:[NSDecimalNumber numberWithFloat:1.0]
forKey:NSImageCompressionFactor];
[options setValue:[NSNumber numberWithBool:YES] forKey:NSImageProgressive];
NSData* jpegData = [rep representationUsingType:NSJPEGFileType
properties:options];
[jpegData writeToFile:@"/Users/dmitry/tmp/img_4389-800x600.jpg" atomically:YES];
}
================= NSImage ===============
- (void) scaleNSImage:(NSString *)path
Width:(int) desiredWidth
Height:(int) desiredHeight
{
NSImage *sourceImage = [[NSImage alloc] initWithContentsOfFile:path];
NSImage *resizedImage = [[NSImage alloc] initWithSize:
NSMakeSize(desiredWidth, desiredHeight)];
NSSize originalSize = [sourceImage size];
[resizedImage lockFocus];
[sourceImage drawInRect: NSMakeRect(0, 0, desiredWidth, desiredHeight)
fromRect: NSMakeRect(0, 0, originalSize.width, originalSize.height)
operation: NSCompositeSourceOver fraction: 1.0];
[resizedImage unlockFocus];
NSMutableDictionary *options=[NSMutableDictionary dictionaryWithObject:[NSDecimalNumber numberWithFloat:1.0]
forKey:NSImageCompressionFactor];
[options setValue:[NSNumber numberWithBool:YES] forKey:NSImageProgressive];
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]
initWithData:[resizedImage TIFFRepresentation]];
NSData* jpegData = [rep representationUsingType:NSJPEGFileType
properties:options];
[jpegData writeToFile:@"~/nsimg_4389-800x600.jpg" atomically:YES];
}
如果有人可以在这里解释或提出建议,我真的很感激。
谢谢。
最佳答案
有一些使用CIImage缩放的怪癖。 Dan Wood的this帖子对您有帮助吗?