【问题标题】:Image Filtering图像过滤
【发布时间】:2011-12-14 15:59:22
【问题描述】:

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

这里有几个过滤器我面临的问题

  1. 怀旧
  2. 复古

原图

怀旧

复古

如果有人能指导我正确的方向,那就太好了。

提前致谢。

【问题讨论】:

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


    【解决方案1】:

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

    这可能不适合您..请根据您自己的要求使用它..从内存中复制,可能有错误..

    【讨论】:

    • 通过更改 RGB 值,我已经实现了该线程中解释的所有这些效果。我已经尽了全力,但我无法获得这 3 个滤镜。
    • 问题是像'old'这样的图像过滤器名称不是像'gray scale'这样的标准......所以很难获得有关其底层逻辑的信息......
    • 看你的复古照片,如果你有时间,试试这个。 1. 稍微增加图像的对比度。 2. 在图像上绘制径向渐变,中心有光,然后圆周上变暗..(以获得复古图像的暗边缘).. 3.然后将其与淡黄色混合...只是玩它...当我有这样的要求时,我就是这样做的。 .
    • @Krishnabhadra:嗨……我也需要使用核心图形的图像过滤器。如果你有任何关于过滤器的源代码,你可以把它们发给我吗?
    【解决方案2】:

    首先查看Core Image documentation。 Core Image 随 iOS 5.0 发布,是强大的图像处理 api。

    您也可以使用 Quartz 绘制图像并使用不同的混合选项 (CGContextSetBlendMode) 覆盖图层以实现一些不错的效果。

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

    【讨论】:

    • 感谢您的回答。我的应用程序不支持 iOS 5.0。这就是我面临问题的原因。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-11
      • 2012-01-02
      • 1970-01-01
      • 2013-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多