You must have used PS to pull the map (buckle), and in Swift, the use of code can also be used to achieve the cutout function.
1, there are usually two ways to pull a character or object out of the background:
(1) Use Coreimage gamut: Suitable for pure color background (or background hue to single, color difference is not too big), pull the figure accurate
(2) Using OPENCV edge detection: Complex background is also applicable, the default cutout is not accurate
2, the following use the first scheme to pull out the kitten below, placed on the snow background.
3, how to use the core image cutout
For a solid color background, you can directly remove the background color, so the rest is the main body. To eliminate the background color, you can use the Cicolorcube filter.
And the Cicolorcube filter requires a cube mapping table, this table is actually a color table (3D color lookup table), the color you want to eliminate the alpha value set to 0, the other color unchanged, Core image will be the image data on the color map to the color of the table, In order to eliminate the purpose of a certain color.
4, the HSV (hue value) graph representing the color value area
This makes it easy to view the HSV value for the RGB color. For example, the kitten background is blue (except in different shades), we only need to place the HSV in 210 to 240 of the color.
5, here is the sample effect chart
For ease of comparison, I have done a "dig-only", and "pull the map and replace the background" two features. (really no PS traces)
6, the code is as follows:
(1) First create the cube map table
Create a new "C file" file cubemap.c, Xcode will automatically generate the corresponding header file CubeMap.h, as well as the connection header file (bridging header file). The code in each file is as follows:
6, the code is as follows:
(1) First create the cube map table
Create a new "C file" file cubemap.c, Xcode will automatically generate the corresponding header file CubeMap.h, as well as the connection header file (bridging header file). The code in each file is as follows:
Import Uikit
Class viewcontroller:uiviewcontroller{
@IBOutlet weak var imageview:uiimageview!
Picture image
Lazy var originalimage:uiimage = {
Return UIImage (named: "Cat.jpg")
}()!
Lazy var context:cicontext = {
Return Cicontext (Options:nil)
}()
Override Func Viewdidload () {
Super.viewdidload ()
Imageview.image = Originalimage
}
Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
Pull a picture
@IBAction func Cutout (sender:anyobject) {
Let CubeMap = Createcubemap (210,240)
Let data = NSData (BytesNoCopy:cubeMap.data, Length:int (cubemap.length),
Freewhendone:true)
Eliminate a color
Let Colorcubefilter = Cifilter (name: "Cicolorcube")!
Colorcubefilter.setvalue (Cubemap.dimension, Forkey: "Inputcubedimension")
Colorcubefilter.setvalue (data, Forkey: "Inputcubedata")
Colorcubefilter.setvalue (Ciimage (image:originalimage), Forkey:kciinputimagekey)
Let Outputimage = Colorcubefilter.outputimage
Let Cgimage = Context.createcgimage (outputimage!, fromrect:outputimage!. Extent
Imageview.image = UIImage (cgimage:cgimage)
}
Pull and synthesize
@IBAction func cutoutandcompose (sender:anyobject) {
Let CubeMap = Createcubemap (210,240)
Let data = NSData (BytesNoCopy:cubeMap.data, Length:int (cubemap.length),
Freewhendone:true)
Eliminate a color
Let Colorcubefilter = Cifilter (name: "Cicolorcube")!
Colorcubefilter.setvalue (Cubemap.dimension, Forkey: "Inputcubedimension")
Colorcubefilter.setvalue (data, Forkey: "Inputcubedata")
Colorcubefilter.setvalue (Ciimage (image:originalimage), Forkey:kciinputimagekey)
var outputimage = Colorcubefilter.outputimage
and background image synthesis
Let Sourceovercompositingfilter = Cifilter (name: "Cisourceovercompositing")!
Sourceovercompositingfilter.setvalue (Outputimage, Forkey:kciinputimagekey)
Sourceovercompositingfilter.setvalue (Ciimage image:uiimage (named: "Bg.jpg")!),
Forkey:kciinputbackgroundimagekey)
Outputimage = Sourceovercompositingfilter.outputimage
Let Cgimage = Context.createcgimage (outputimage!, fromrect:outputimage!. Extent
Imageview.image = UIImage (cgimage:cgimage)
}
Restore picture
@IBAction func resetimage (sender:anyobject) {
Self.imageView.image = Originalimage
}
}