图像过滤

时间:2011-10-25 05:36:17

标签: ios cocoa-touch image-processing filter uiimage

我有一个应用程序,需要在图像过滤器上做一些工作,所以我刚开始研究过滤器,我实现了很少的过滤器,如Black&白色,Hud,苔藓,亮度,对比度等。

以下是我要面对的问题的几个过滤器

  1. Nostalgia
  2. Vintage
  3. 原始图片

    enter image description here

    怀旧

    enter image description here

    老式

    enter image description here

    enter image description here

    如果有人能指引我朝着正确的方向前进,那就太好了。

    提前致谢。

3 个答案:

答案 0 :(得分:3)

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();

这可能对你不起作用。请根据自己的要求玩这个。从内存中复制,可能出错...

答案 1 :(得分:0)

首先查看Core Image documentation。核心图像通过iOS 5.0到达,是强大的图像处理API。

此外,您可以使用Quartz使用不同的混合选项(CGContextSetBlendMode)绘制图像和图层,以实现一些不错的效果。

如果此技术不足,您可以尝试逐个像素地更改颜色值。 试试这个url,也许你会找到适合自己的东西。

答案 2 :(得分:0)

您似乎需要确定RGB转换的参数。如果您已经有一个工具来产生“复古”,“旧”和“怀旧”效果,那么您可以处理包含所有256 ^ 3 RGB颜色(如this one)的样本图像,然后观察,示例颜色通道按颜色通道简单地开始,如何修改像素值。这可能足以推断RGB变换函数。而“旧”则需要在边界上留下阴影。