【发布时间】:2011-12-14 15:59:22
【问题描述】:
我有一个应用程序需要在图像过滤器上做一些工作,所以我刚开始研究过滤器,我实现了一些过滤器,如黑白、Hud、苔藓、亮度、对比度等。
这里有几个过滤器我面临的问题
- 怀旧
- 复古
- 旧
原图
怀旧
复古
老
如果有人能指导我正确的方向,那就太好了。
提前致谢。
【问题讨论】:
标签: ios cocoa-touch image-processing filter uiimage
我有一个应用程序需要在图像过滤器上做一些工作,所以我刚开始研究过滤器,我实现了一些过滤器,如黑白、Hud、苔藓、亮度、对比度等。
这里有几个过滤器我面临的问题
原图
怀旧
复古
老
如果有人能指导我正确的方向,那就太好了。
提前致谢。
【问题讨论】:
标签: ios cocoa-touch image-processing filter uiimage
This 线程对 iPhone 中的图像过滤非常有用...
这是我为制作复古图像所做的(在我的情况下,复古图像要求略有不同..边缘没有阴影)
//first increase contrast a bit...
float contrastValue = 1.45;
CGImageRef originalImage = [myImage CGImage];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext = CGBitmapContextCreate(NULL,CGImageGetWidth(originalImage),CGImageGetHeight(originalImage),8,CGImageGetWidth(originalImage)*4,colorSpace,kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(bitmapContext, CGRectMake(0, 0, CGBitmapContextGetWidth(bitmapContext), CGBitmapContextGetHeight(bitmapContext)), originalImage);
UInt8* data = CGBitmapContextGetData(bitmapContext);
int numComponents = 4;
int bytesInContext = CGBitmapContextGetHeight(bitmapContext) * CGBitmapContextGetBytesPerRow(bitmapContext);
double redIn, greenIn, blueIn;
for (int i = 0; i < bytesInContext; i += numComponents) {
redIn = (double)data[i]/255.0;
greenIn = (double)data[i+1]/255.0;
blueIn = (double)data[i+2]/255.0;
redIn -= 0.5;
redIn *= contrastValue;
redIn += 0.5;
redIn *= 255.0;
if (redIn < 0) {
redIn = 0;
}
if (redIn > 255) {
redIn = 255;
}
greenIn -= 0.5;
greenIn *= contrastValue;
greenIn += 0.5;
greenIn *= 255.0;
if (greenIn < 0) {
greenIn = 0;
}
if (greenIn > 255) {
greenIn = 255;
}
blueIn -= 0.5;
blueIn *= contrastValue;
blueIn += 0.5;
blueIn *= 255.0;
if (blueIn < 0) {
blueIn = 0;
}
if (blueIn > 255) {
blueIn = 255;
}
data[i] = (redIn);
data[i+1] = (greenIn);
data[i+2] = (blueIn);
}
CGImageRef outImage = CGBitmapContextCreateImage(bitmapContext);
myImage = [UIImage imageWithCGImage:outImage];
CGImageRelease(outImage);
//Then blend it with a yellowish color
UIGraphicsBeginImageContext(myImage.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect area = CGRectMake(0, 0, myImage.size.width, myImage.size.height);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -area.size.height);
CGContextSaveGState(ctx);
CGContextClipToMask(ctx, area, myImage.CGImage);
UIColor *color = [UIColor colorWithRed:248.0/255.0 green:254.0/255.0 blue:186.0/255.0 alpha:1.0]; //[UIColor colorWithRed:248.0/255.0 green:254.0/255.0 blue:186.0/255.0 alpha:1.0]
[color set];
CGContextFillRect(ctx, area);
CGContextRestoreGState(ctx);
CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
CGContextDrawImage(ctx, area, myImage.CGImage);
myImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIGraphicsBeginImageContext(myImage.size);
ctx = UIGraphicsGetCurrentContext();
area = CGRectMake(0, 0, myImage.size.width, myImage.size.height);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -area.size.height);
CGContextSaveGState(ctx);
CGContextClipToMask(ctx, area, myImage.CGImage);
color = [UIColor colorWithRed:27.0/255.0 green:50.0/255.0 blue:224.0/255.0 alpha:0.2]; //[UIColor colorWithRed:248.0/255.0 green:254.0/255.0 blue:186.0/255.0 alpha:1.0]
[color set];
CGContextFillRect(ctx, area);
CGContextRestoreGState(ctx);
CGContextSetBlendMode(ctx, kCGBlendModeLighten);
CGContextDrawImage(ctx, area, myImage.CGImage);
processedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
这可能不适合您..请根据您自己的要求使用它..从内存中复制,可能有错误..
【讨论】:
首先查看Core Image documentation。 Core Image 随 iOS 5.0 发布,是强大的图像处理 api。
您也可以使用 Quartz 绘制图像并使用不同的混合选项 (CGContextSetBlendMode) 覆盖图层以实现一些不错的效果。
如果这些技术还不够,您可以尝试逐像素更改颜色值。 试试这个url,也许你会找到适合你的东西。
【讨论】:
看来你需要确定一个RGB转换的参数。如果您已经有一个工具来产生“复古”、“旧”和“怀旧”效果,那么您可以处理包含所有 256^3 RGB 颜色的示例图像(如this one),然后观察,例如颜色逐个颜色通道开始简单,像素值是如何修改的。这可能足以推导出 RGB 变换函数。而“旧”则需要在边框上加上深色阴影。
【讨论】: