来自UIImage的偏振滤波器 [英] polaroid filter from UIImage

查看:186
本文介绍了来自UIImage的偏振滤波器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在iPhone中实现像宝丽来一些图像过滤器。我搜索了如何过滤现有的UIImage,将其转换为宝丽来风格,并碰到这个计算器链接。以这里的答案为出发点,我通过图像的每个像素,采取RGB值,并将其转换为HSV,直到这一点我已经成功。所以这就是我所做的(任何人都可以自由指出任何错误..)

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

} else {
if(g return g;
} else {
return b;





$ 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;



$ 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(gray)
* r = * g = * b = v;
return;
}
h / = 60; //扇区0到5
i = floor(h);
f = h - i; // 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;
案例1:
* r = q;
* g = v;
* b = p;
break;
案例2:
* r = p;
* g = v;
* b = t;
break;
案例3:
* r = p;
* g = q;
* b = v;
break;
案例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(); (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;
双色调,饱和度,价值; (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;
}

饱和度=饱和度* 1.3;
if(饱和度> 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);
返回myImage;



$ b现在我对图像处理的想法是非常幼稚的(甚至不是业余的)。我阅读了这个并试图调整饱和度和色调,看看我是否可以得到一个宝丽来效果..我想我失去了一些东西,因为我得到了除了宝丽来以外的地球上的每一个效果(说我没有得到任何东西)..



  1. 视图的程序员点上是否有任何有关net(或
    books)的文档,它告诉我们有关image
    的过滤功能? (而不是一个设计师点
    的视图和没有一个Photoshop
    截图)
  2. 什么是色相,饱和度,价值
    差异我必须使像素
    ,使我可以使宝丽来?

  3. 第三,我在正确的轨道上吗?

在此先感谢..

解决方案

photoshop和再现他们的iOS。



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


I am trying to implement some image filters, like polaroid, in iphone. I searched on how to filter an existing UIImage to convert it into a polaroid style and come across this stackoverflow link. Taking the answer there as a starting point, I looped through each pixel of the image, taking RGB values, and converted them to HSV, up to this point I have been successful. So this is what I have done (anyone is free to point any mistakes..)

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;
}

Now my idea about image processing is very childish (not even amateurish). I read this and tried to adjust the saturation and hue to see if I can get a polaroid effect..I think I am missing something, for I got every effect on earth other than a polaroid (saying that I havent got anything)..

  1. Is there any document on net (or books) which tells about image filtering on a programmers point of view? (and not on a designers point of view and without a photoshop screenshot)
  2. What is the hue, saturation, value difference I have to make on a pixel so that I can make it polaroid?
  3. And third, Am I on the right track?

Thanks in advance..

解决方案

This might be helpful, from Camera+ taking filters from photoshop and reproducing them for iOS.

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

这篇关于来自UIImage的偏振滤波器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆