Swift implementation of the development of iOS, and the realization of image synthesis function

Source: Internet
Author: User

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

Related Article

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.