想用另一种颜色更改图像中的特定颜色 - iPhone [英] Want to change a particular color inside an image with another color - iPhone

查看:132
本文介绍了想用另一种颜色更改图像中的特定颜色 - iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个房间图像。



现在,用户将点击任何特定颜色,假设图像中有蓝色。



并且用户将选择替换的颜色,如黑色颜色。



因此,图像中的所有蓝色 颜色。



任何人都有如何在iPhone中实现这一点的想法?



解决方案


  1. 您必须遍历图片中的每个像素,并取其rgb

  2. 检查其rgb值是否与您的fromColor rgb值匹配,如果是
    将像素值更改为toColor rgb值。如果不是,请留下
    的像素和请转到下一个..

从memory..errors写了一个函数可能...更正自己



- (UIImage *)changeColor:(UIImage *)myImage fromColor:(UIColor *)fromColor toColor:(UIColor *)toColor {
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;
CGFloat fromRed,fromGreen,fromBlue,fromAlpha;
CGFloat toRed,toGreen,toBlue,toAlpha;

//获取fromColor的值
int fromCountComponents = CGColorGetNumberOfComponents([fromColor CGColor]);
if(fromCountComponents == 4){
const CGFloat * _components = CGColorGetComponents([fromColor CGColor]);
fromRed = _components [0];
fromGreen = _components [1];
fromBlue = _components [2];
fromAlpha = _components [3];
}


//获取toColor的RGB值
int toCountComponents = CGColorGetNumberOfComponents([toColor CGColor]);
if(toCountComponents == 4){
const CGFloat * _components = CGColorGetComponents([toColor CGColor]);
toRed = _components [0];
toGreen = _components [1];
toBlue = _components [2];
toAlpha = _components [3];
}


//现在遍历图像中的每个像素。
for(int i = 0; i //当前像素的rgba值...
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;

//现在你得到当前的像素rgb值...检查它是否与你的fromColor
if(redIn == fromRed&& greenIn == fromGreen&& blueIn = = fromBlue){
//图像颜色匹配fromColor,然后将当前像素颜色更改为tocolor
data [i] = toRed;
data [i + 1] = toGreen;
data [i + 2] = toBlue;
data [i + 3] = toAlpha;
}
}
CGImageRef outImage = CGBitmapContextCreateImage(bitmapContext);
myImage = [UIImage imageWithCGImage:outImage];
CGImageRelease(outImage);
return myImage;
}



我希望你不是每次都调用这个函数...它是一个位处理器重。如果我是你的处理器,我不会与你高兴...):


I have one "room" image.

Now, user will click on any particular color suppose blue color in image.

And also user will select replaced color like "black" color.

So, all "blue" color inside the image will be replaced by "black" color.

Any one have idea for how to achieve this in iPhone?

Help is appreciated.

解决方案

  1. You have to iterate through each pixel in the image and take its rgb values
  2. Check its rgb values matches your fromColor rgb values, if yes change the pixel value to your toColor rgb values.If not, just leave that pixel and go to next one..

Wrote a function from memory..errors possible..correct yourself

-(UIImage*)changeColor:(UIImage*)myImage fromColor:(UIColor*)fromColor toColor:(UIColor*)toColor{
    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;
    CGFloat fromRed,fromGreen,fromBlue,fromAlpha;
    CGFloat toRed,toGreen,toBlue,toAlpha; 

    //Get RGB values of fromColor
    int fromCountComponents = CGColorGetNumberOfComponents([fromColor CGColor]);
    if (fromCountComponents == 4) {
     const CGFloat *_components = CGColorGetComponents([fromColor CGColor]);
     fromRed = _components[0];
     fromGreen = _components[1];
     fromBlue = _components[2];
     fromAlpha = _components[3];
    }


    //Get RGB values for toColor
    int toCountComponents = CGColorGetNumberOfComponents([toColor CGColor]);
    if (toCountComponents == 4) {
      const CGFloat *_components = CGColorGetComponents([toColor CGColor]);
      toRed   = _components[0];
      toGreen = _components[1];
      toBlue  = _components[2];
      toAlpha = _components[3];
    }


    //Now iterate through each pixel in the image..
    for (int i = 0; i < bytesInContext; i += numComponents) {
        //rgba value of current pixel..
        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;

        //now you got current pixel rgb values...check it curresponds with your fromColor
        if( redIn == fromRed && greenIn == fromGreen && blueIn == fromBlue ){
            //image color matches fromColor, then change current pixel color to toColor
            data[i]    =   toRed;
            data[i+1]  =   toGreen;
            data[i+2]  =   toBlue;
            data[i+3]  = toAlpha;       
        }
    }
    CGImageRef outImage =   CGBitmapContextCreateImage(bitmapContext);
    myImage             =   [UIImage imageWithCGImage:outImage];
    CGImageRelease(outImage);
    return myImage;
}

I hope you are not calling this function every time...It is a bit processor heavy..And if I was your processor, I wouldn't be happy with you..:)

这篇关于想用另一种颜色更改图像中的特定颜色 - iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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