Replace a color value of an image

Source: Internet
Author: User

ConstUnsignedCharRgbmax =255;#defineEqualzero (a) ((a) = = 0)voidCVTRGBTOHSI (cgfloat ir,cgfloat IG, cgfloat IB, unsignedChar* RH, unsignedChar* RS, unsignedChar*RI) {    /*When a person observes a colored object, it uses hue, saturation, brightness to describe the object's color. Hsi(hue-saturation-intensity (lightness), the HSI or Hsl) color model describes the color characteristics with H, S, I three parameters, where h defines the wavelength of the color, called the hue, and S indicates the degree of depth of the color, called saturation; I represents strength or brightness. In the HSI color model of the double hexagonal pyramid, I is the intensity axis, the angle range of the hue h is [0,2π], where the pure red angle is 0, the pure green angle is 2Π/3, the pure blue angle is 4Π/3*/    Const   floatPI2 =2*3.1415926; floatR = (float) IR; floatg = (float) IG; floatB = (float) IB; floatMaxval =Max (Max (R, G), b); floatMinval =min (min (R, G), b); floati = (r + G + B)/3; floats =0.0f; floath =0.0f; if(! Equalzero (i) &&! Equalzero (Maxval-minval)) {        floatdiff =0.5f* (R-g + Rb); floatDIFF2 = (r-g) * (r-g) + (r-g) * (g-b);//diff2 Forever > 0        floatSita = ACOs (Diff/sqrt (diff2))/pi2;//angle, PI2 is 1h = (g>=b)? Sita: (1.0f-Sita); S=1.0f-minval/i; }    if(H <0.0f) H + =1.0f; Else if(H >1.0f) H-=1.0f; *RH = (unsignedChar) round (H*rgbmax);//range:0 to 255;*rs = (unsignedChar) round (S*rgbmax);//range:0 to 255;*ri = (unsignedChar) round (I*rgbmax);//range:0 to 255;}unsignedCharSubunsignedchar (unsignedChara,unsignedCharb) {    return(a>b)? (a):(B-a);}-(BOOL) Besimilarcolor: (uicolor*) Color1 Withcolor: (uicolor*) Color2 withdifference: (unsignedChar) difference{cgfloat r1,r2,g1,g2,b1,b2,a1,a2; unsignedCharH1,h2,s1,s2,i1,i2; [Color1 getred:&AMP;R1 green:&g1 blue:&b1 alpha:&A1]; [Color2 getred:&AMP;R2 green:&g2 blue:&b2 alpha:&A2]; CVTRGBTOHSI (R1, G1, B1,&AMP;H1, &AMP;S1, &i1); CVTRGBTOHSI (R2, G2, B2,&AMP;H2, &AMP;S2, &i2); unsignedCharMaxgap =difference; if(A1 = = A2 && (Subunsignedchar (H1, H2) <= maxgap) && (Subunsignedchar (S1, S2) <= maxgap) && ( Subunsignedchar (I1, I2) <=maxgap)) {        returnYES; }    returnNO;}#pragmaMark Picture Replacement Color-(uiimage*) Videopictureimageforreplacecolor: (uicolor*) color Tocolor: (uicolor*) Othercolor withimage: ( uiimage*) Image withdifference: (unsignedChar) difference{cvpixelbufferref pixelbuffer=NULL; Cgimageref img=image.    Cgimage; size_t width=cgimagegetwidth (IMG); size_t Height=cgimagegetheight (IMG); Cvreturn Status= Cvpixelbuffercreate (kcfallocatordefault, width, height, kcvpixelformattype_32bgra, nil, &Pixelbuffer); if(Status! =kcvreturnsuccess) {        returnNULL; } size_t rowbytes=Cvpixelbuffergetbytesperrow (Pixelbuffer); Cvpixelbufferlockbaseaddress (Pixelbuffer,0); void*data =cvpixelbuffergetbaseaddress (Pixelbuffer);    CGFloat R,g,b,a; [Othercolor getred:&r green:&g blue:&b alpha:&a]; Cgcolorspaceref Rgbcolorspace=Cgcolorspacecreatedevicergb (); Cgcontextref Context= Cgbitmapcontextcreate (data, width, height,8, Rowbytes, Rgbcolorspace, (cgbitmapinfo) kcgbitmapbyteorder32little |Kcgimagealphapremultipliedfirst); Cgcontextclearrect (Context, CGRectMake (0,0, width, height)); Cgcontextdrawimage (Context, CGRectMake (0,0, width, height), img); unsignedChar*colorptr = (unsignedChar*) data;  for(size_t h =0; h < height; h++) {         for(size_t w =0; W < width; w++) {                        if([Self Besimilarcolor:[uicolor colorwithred:colorptr[0]/255.0fgreen:colorptr[1]/255.0fblue:colorptr[2]/255.0falpha:colorptr[3]/255.0f] Withcolor:color withdifference:difference]) {//printf ("%d%d%d\n", colorptr[0],colorptr[1],colorptr[2],colorptr[3]);colorptr[0] = Round (r*255); colorptr[1] = Round (g*255); colorptr[2] = Round (b*255); colorptr[3] = Round (A *255); } colorptr+=4; }} cgimageref Imageref=cgbitmapcontextcreateimage (context);    Cgcolorspacerelease (Rgbcolorspace);    Cgcontextrelease (context); Cvpixelbufferunlockbaseaddress (Pixelbuffer,0); UIImage*returnimage =[UIImage Imagewithcgimage:imageref]; // Clean Upcgimagerelease (IMAGEREF);    Cvpixelbufferrelease (Pixelbuffer); returnreturnimage;}

Replace a color value of an image

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.