【问题标题】:polaroid filter from UIImage来自 UIImage 的宝丽来滤镜
【发布时间】:2011-08-01 04:55:09
【问题描述】:

我正在尝试在 iphone 中实现一些图像过滤器,例如宝丽来。我搜索了如何过滤现有 UIImage 以将其转换为 polaroid 样式并遇到 this stackoverflow 链接。以那里的答案为起点,我循环遍历图像的每个像素,获取 RGB 值,并将它们转换为 HSV,到目前为止我已经成功了。所以这就是我所做的(任何人都可以随意指出任何错误..)

double minRGB(double r, double g, double b){
    if (r < g){
        if (r < b){
            return r;
        }else {
            return b;
        }
    }else { 
        if (g < b){
            return g;
        }else{
            return b;
        }
    }
}



double maxRGB(double r, double g, double b){
    if (r > g){
        if (r > b){
            return r;
        }else {
            return b;
        }
    }else { 
        if (g > b){
            return g;
        }else {
            return b;
        }
    }
}

void rgbToHsv(double redIn,double greenIn,double blueIn,double *hue,double *saturation,double* value){
    double min,max,delta;

    min                         =   minRGB(redIn,greenIn,blueIn);
    max                         =   maxRGB(redIn,greenIn,blueIn);
    *value                      =   max;
    delta                       =   max - min;
    if (max != 0) {
        *saturation             =   delta/max;
    }else {
        *saturation             =   0;
        *hue                        =   -1.0;
        return ;
    }
    if (redIn == max) {
        *hue                    =   (greenIn - blueIn)/delta;
    }else if (greenIn == max) {
        *hue                    =   2 + (blueIn - redIn)/delta;
    }else {
        *hue                    =   4 + (redIn - greenIn)/delta;
    }
    *hue                        *=  60.0;
    if (*hue < 0) {
        *hue                    +=  360.0;
    }
}

void hsvToRgb(double h,double s, double v, double *r,double *g, double *b){
    int i;
    float f, p, q, t;
    if( s == 0 ) {
        // achromatic (grey)
        *r = *g = *b = v;
        return;
    }
    h                           /=  60;         // sector 0 to 5
    i                           =   floor( h );
    f                           =   h - i;          // factorial part of h
    p                           =   v * ( 1 - s );
    q                           =   v * ( 1 - s * f );
    t                           =   v * ( 1 - s * ( 1 - f ) );
    switch( i ) {
        case 0:
            *r = v;
            *g = t;
            *b = p;
            break;
        case 1:
            *r = q;
            *g = v;
            *b = p;
            break;
        case 2:
            *r = p;
            *g = v;
            *b = t;
            break;
        case 3:
            *r = p;
            *g = q;
            *b = v;
            break;
        case 4:
            *r = t;
            *g = p;
            *b = v;
            break;
        default:        // case 5:
            *r = v;
            *g = p;
            *b = q;
            break;
    }
}


-(void)makeImagePolaroid:(UIImage*)myImage{
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,alphaIn;
double hue,saturation,value;

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;
    alphaIn                     =   (double)data[i+3]/255.0;
    
    rgbToHsv(redIn,greenIn,blueIn,&hue,&saturation,&value);
    
    hue                         =   hue * 0.7;
    if (hue > 360) {
        hue                     =   360;
    }
    
    saturation                  =   saturation *1.3;
    if (saturation > 1.0) {
        saturation              =   1.0;
    }

    value                       =   value * 0.8;
    if (value > 1.0) {
        value                   =   1.0;
    }
    
    hsvToRgb(hue,saturation,value,&redIn,&greenIn,&blueIn);
    data[i]                     =   redIn * 255.0;
    data[i+1]                   =   greenIn * 255.0;
    data[i+2]                   =   blueIn * 255.0;
}
CGImageRef outImage             =   CGBitmapContextCreateImage(bitmapContext);
myImage                         =   [UIImage imageWithCGImage:outImage];
CGImageRelease(outImage);
return myImage;
}

现在我对图像处理的想法非常幼稚(甚至不是业余的)。我阅读了this 并尝试调整饱和度和色调,看看我是否可以获得宝丽来效果..我想我错过了一些东西,因为除了宝丽来之外,我得到了地球上的所有效果(说我什么都没有) ..

  1. 网络上是否有任何文件(或 介绍图像的书籍) 过滤程序员的观点 看法? (而不是设计师的观点 视图且没有 Photoshop 截图)
  2. 什么是色相、饱和度、值 我必须在一个像素上做出改变 这样我就可以拍拍立得了?
  3. 第三,我走对了吗?

提前谢谢..

【问题讨论】:

  • 好像我一个人在这个:(

标签: iphone image filter rgb hsv


【解决方案1】:
     int step=10;// variable value that changes the level of saturation

int red = pixelRedVal;
int green = pixelGreenVal;
int blue = pixelBlueVal;

int avg = (red + green + blue) / 3;

pixelBuf[r] = SAFECOLOR((avg + step * (red - avg)));
pixelBuf[g] = SAFECOLOR((avg + step * (green - avg)));
pixelBuf[b] = SAFECOLOR((avg + step * (blue - avg)));
where SAFECOLOR is a macro

define SAFECOLOR(color) MIN(255,MAX(0,color))
and for Brightness int step=10;// variable value that changes the level of saturation

int red = pixelRedVal;
int green = pixelGreenVal;
int blue = pixelBlueVal;


pixelBuf[r] = SAFECOLOR(red * step);
pixelBuf[g] = SAFECOLOR(green * step);
pixelBuf[b] = SAFECOLOR(blue * step);




You can simply use this: with different parameters

// Note: the hue input ranges from 0.0 to 1.0, both red.  Values outside this range will be clamped to 0.0 or 1.0.
    //Polaroid with HSB parameter

   - (UIImage*) polaroidishEffectWithHue:(CGFloat)hue saturation:(CGFloat)sat brightness:(CGFloat)bright  alpha:(CGFloat)alpha
  {

   // Find the image dimensions.
    CGSize imageSize = [self size];
    CGRect imageExtent = CGRectMake(0,0,imageSize.width,imageSize.height);

    // Create a context containing the image.
    UIGraphicsBeginImageContext(imageSize);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self drawAtPoint:CGPointMake(0,0)];

    // Draw the hue on top of the image.
    CGContextSetBlendMode(context, kCGBlendModeHue);
    [[UIColor colorWithHue:hue saturation:sat brightness:bright alpha:alpha] set];
    UIBezierPath *imagePath = [UIBezierPath bezierPathWithRect:imageExtent];
    [imagePath fill];

    // Retrieve the new image.
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return result;
}

【讨论】:

  • Satish Azad,好吧,感谢您的回答,即使我不再从事此工作.. 但不要在一个问题中添加多个答案.. 编辑 4 个中的任何一个答案并在那里说出一切.. 删除其他 3 个答案。如果没有,他们很有可能会因垃圾邮件而对您采取行动
  • 你能把它转换成swift吗?
  • Rubaiyat Jahan Mumu:是的,我可以转换。但我建议你可以在 Swift 源代码中使用目标 C 代码,方法是使用将目标 C 类与 Swift 类链接的桥接头。
【解决方案2】:

这可能会有所帮助,从 Camera+ 从 photoshop 中获取过滤器并将它们复制到 iOS。

http://taptaptap.com/blog/creating-a-camera-plus-fx/

【讨论】:

  • 感谢richy的回答..但是我已经看过这个教程..并且也尝试过..但不幸的是它没有用..
猜你喜欢
  • 2016-12-20
  • 2012-09-19
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 2013-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多