ios - 图像过滤

标签 ios cocoa-touch image-processing filter uiimage

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

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

  1. 怀旧
  2. 复古

原始图像

enter image description here

怀旧

enter image description here

复古

enter image description here

enter image description here

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

提前致谢。

最佳答案

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

这可能不适合你..请根据你自己的要求玩这个..从内存中复制,可能有错误..

关于ios - 图像过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7885089/

相关文章:

c++ - Kuwahara 过滤器的奇怪结果

image-processing - 带掩码的聚类

ios - 如何使用标签在表格 View 中显示单元格数

ios - 未调用controllerDidChangeContent

ios - UICollectionView 单元格对齐方式

ios - 在完成之前调用 Dispatch.main.asyncAfter() 会导致应用程序崩溃

objective-c - 在调用 gesturerecognizers 方法之前是否存在我可以调用的任何方法?

objective-c - View 激活时要覆盖什么方法?

cocoa-touch - 为什么表格 View 单元格中的自动布局会导致表格的内容大小错误地更改?

python - 在这种情况下我应该使用哪种图像质量指标(二值分割图像)?