Previously curious, wanted to implement such features-----> iOS image processing-(jpg white background removal)
Key The white background of a picture (. jpg) into a. PNG-formatted transparent diagram with an alpha channel.
The original is black and white, like this
Converted into this
Then in the forum, get the answer you want. Thank the Daniel here first, also provided reference: IOS8 Core Image in Swift: more complex filters
And then today, I'll summarize it myself.
Wrote a small demo, to achieve the background image of the switch. The effect is as follows:
As you can see, the original yellow gradient background was replaced. The next is to implement such a function.
Of course. If interested, you can download from here to Source: http://download.csdn.net/detail/hitwhylz/8186081
Eliminate orange Yellow just like Photoshop's magic wand, the Core image has a similar filter, but it's not that simple and rough, it's cumbersome to use. In core image, in order to eliminate a certain color, we need to useCicolorcubeFilter, and the Cicolorcube filter needs a cube mapping table, this table is actually a color table (3D color lookup table), the color you want to eliminate the alpha value of 0, the other color is unchanged, the Core image will map the image data on the color of the table color, To achieve the purpose of eliminating a certain color. Cicolorcube This table by default will not do any processing for inputimage, but we have to kill all the orange color, so we need to build this table.
We want to eliminate the "orange yellow" is not just a visual color, but the range of colors, the most direct way isturn Rgba into HSV (hue,saturation,value), in the HSV format, the color is expressed around the angle of the axis of the cylinder, in this way, you can think of the color range as a fan, and then directly to the block area (Alpha set to 0), This means that we actually need to specify the range of color areas------the minimum angle around the axis of the cylinder and the maximum angle, the color alpha in this range is set to 0. Finally, the data in the cube map tablemust be multiplied by alpha, so the last step in creating a cube map is to multiply the RGB value by the alpha value you just calculated: if it's the color you want to eliminate, multiply it by 0, and the opposite is the same. This is an HSV (hue value) graph that represents the color value area:
Can see if it is a pure yellow, its value is 60 degrees, blue is 240 degrees, we have a value of about 35 to 55 (yellow One o'clock), on this site can see more detailed RGB color corresponding to the HSV value. So then we're going to create a cube map table, and the way to create the cube map table is found in the Apple official example, which is implemented in C, and for convenience, we create a C file to contain the code directly. Select in the projectcreate a new. c File, after I have named the cubemap.c,.c file, I add the code in the official Apple example (the following code):
struct CUBEMAP {int length; float dimension; float *data;}; struct CubeMap createcubemap (float minhueangle, float maxhueangle) {const unsigned int size = 64; struct CUBEMAP map; map.length = size * Size * size * sizeof (FLOAT) * 4; map.dimension = size; float *cubedata = (float *) malloc (map.length); Float rgb[3], hsv[3], *c = Cubedata; for (int z = 0, z < size; z++) {rgb[2] = ((double) z)/(size-1);//Blue value for (int y = 0; y < size; y++) {rgb[1] = ((double) y)/(size-1);//Green value for (int x = 0; x < size; x + +) { Rgb[0] = ((double) x)/(SIZE-1); Red value RGBTOHSV (RGB,HSV); Use the hue value to determine which to make transparent//the minimum and maximum hue angle depends on The color want to remove float Alpha = (hsv[0] > Minhueangle && hsv[0] &l T Maxhueangle)? 0.0f:1.0f; Calculate premultiplied alpha values for the cube c[0] = rgb[0] * ALPHA; C[1] = rgb[1] * ALPHA; C[2] = rgb[2] * ALPHA; C[3] = Alpha; c + = 4; Advance our pointer to memory for the next color value}}} Map.data = Cubedata; return map;}
I changed the method a little bit and chose a struct, because the length and dimension are used outside. Apple does not provide an implementation of the RGBTOHSV method, which I found:
void Rgbtohsv (float *rgb, float *hsv) { float min, Max, Delta; float r = rgb[0], G = rgb[1], B = rgb[2]; float *h = HSV, *s = HSV + 1, *v = HSV + 2; min = Fmin (Fmin (R, G), b); max = Fmax (Fmax (R, G), b); *v = max; Delta = max-min; if (max! = 0) *s = Delta/max; else { *s = 0; *h =-1; return; } if (r = = max) *h = (g-b)/delta; else if (g = = max) *h = 2 + (B-R)/delta; else *h = 4 + (R-G)/delta; *h *=; if (*h < 0) *h + = 360;}
The libraries I imported in. c Files:
#include <stdio.h> #include <stdlib.h> #include <math.h>
When using this file, you need to change the caller, like my viewcontroller.m here, to viewcontroller.mm
This will enable the C file to be supported. Then import the header file #include "cubemap.c"
After that is the simple call, the specific participation in the following code:
-(void) viewdidload{[Super Viewdidload]; Additional setup after loading the view, typically from a nib. Size 600?x?450//show original picture UILabel *oldlabel = [[UILabel alloc]initwithframe:cgrectmake (0, 65, 160, 15)]; Oldlabel.text = @ "original"; Oldlabel.textalignment = Nstextalignmentcenter; [Self.view Addsubview:oldlabel]; Uiimageview *oldimageview = [[Uiimageview alloc]initwithframe:cgrectmake (5, 80, 150, 112)]; Oldimageview.image = [UIImage imagenamed:@ "beautypic.jpg"]; [Self.view Addsubview:oldimageview]; Show background picture UILabel *backlabel = [[UILabel alloc]initwithframe:cgrectmake (160, 65, 160, 15)]; Backlabel.text = @ "background map"; Backlabel.textalignment = Nstextalignmentcenter; [Self.view Addsubview:backlabel]; Uiimageview *backimageview = [[Uiimageview alloc]initwithframe:cgrectmake (165, 80, 150, 112)]; Backimageview.image = [UIImage imagenamed:@ "background"]; [Self.view Addsubview:backimageview]; //Replace background image CubeMap Mycube = Createcubemap (35, 55); NSData *mydata = [[NSData alloc]initwithbytesnocopy:mycube.data length:myCube.length freewhendone:true]; Cifilter *colorcubefilter = [Cifilter filterwithname:@ "Cicolorcube"]; [Colorcubefilter setvalue:[nsnumber numberWithFloat:myCube.dimension] forkey:@ "inputcubedimension"]; [Colorcubefilter setvalue:mydata forkey:@ "Inputcubedata"]; [Colorcubefilter setvalue:[ciimage imageWithCGImage:oldImageView.image.CGImage] forkey:kciinputimagekey]; Ciimage *outputimage = colorcubefilter.outputimage; Cifilter *sourceovercompositingfilter = [Cifilter filterwithname:@ "cisourceovercompositing"]; [Sourceovercompositingfilter setvalue:outputimage Forkey:kciinputimagekey]; [Sourceovercompositingfilter setvalue:[ciimage ImageWithCGImage:backImageView.image.CGImage] Forkey: Kciinputbackgroundimagekey]; Outputimage = Sourceovercompositingfilter.outputimage; Cgimage *cgimage = [[Cicontext contextwithoptions:nil]createCgimage:outputimage FromRect:outputImage.extent]; UILabel *newlabel = [[UILabel alloc]initwithframe:cgrectmake (0, 200, 320, 15)]; Newlabel.text = @ "Crafting chart"; Newlabel.textalignment = Nstextalignmentcenter; [Self.view Addsubview:newlabel]; Uiimageview *newimageview = [[Uiimageview alloc]initwithframe:cgrectmake (10, 220, 300, 225)]; Newimageview.image = [UIImage imagewithcgimage:cgimage]; [Self.view Addsubview:newimageview]; }
iOS development-Simple picture background replacement (for keying effect)