我正在尝试使用核心图像进行图像缩放,使用 Lanczos Scale Transform 过滤器。当我进行放大时很好。但是在按比例缩小并保存为 JPEG 时,我发现了一类会产生奇怪的噪声伪影和行为的图像。
对于 inputScale 倍数为 0.5:0.5、0.25、0.125 等,它总是没问题。对于其他 inputScale 值,它已损坏。
当我保存到 TIFF、PNG、JPEG2000 或在屏幕上绘图时 - 很好。当我保存为 JPG 或 BMP 时 - 它坏了。
我上传了示例图片:(原始,4272x2848,3.4Mb)[ http://www.zoomfoot.com/get/package/517e2423-7795-4239-a166-03d507ec51d8]
(按噪声缩放,1920x1280,2.2Mb)[ http://www.zoomfoot.com/get/package/6eb64d33-3a30-4e8d-9953-67ce1e7d7ef9]
它在“日落”图像上的重现性也很好。
我尝试了更多方法来缩放图像。使用 CIAffineTransform 并使用 NSImage 本身进行绘图。两者都提供没有任何噪音的结果。
这是我用于缩放和保存的代码:
=================兰佐斯==============
- (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];
}
如果有人可以在这里解释或提出建议,我真的很感激。
谢谢。