用户在黑色布的背景下拍摄照片(只有人脸部分),需要把人脸部分抠出来放在透明背景中用在别的地方。
我现在是用CubeMap实现,传的要消除的颜色的hsv范围是
CubeMap myCube = createCubeMap(0,41);
但是会把眉毛和嘴巴也抠去,大家还有别的实现思路吗???
代码如下
//原图
UIImageView *myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 100, 300, 300)];
myImageView.backgroundColor = [UIColor clearColor];
myImageView.image = [UIImage imageNamed:@"WechatIMG1.jpeg"];
[self.view addSubview:myImageView];
//创建透明背景图片
UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 300), NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//背景图片
UIImage *backgroundImage = blank;
//要消除的颜色的hsv值范围
CubeMap myCube = createCubeMap(0,41);
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:myImageView.image.CGImage] forKey:kCIInputImageKey];
//消除后的图片
CIImage *outputImage = colorCubeFilter.outputImage;
CIFilter *sourceOverCompositingFilter = [CIFilter filterWithName:@"CISourceOverCompositing"];
[sourceOverCompositingFilter setValue:outputImage forKey:kCIInputImageKey];
[sourceOverCompositingFilter setValue:[CIImage imageWithCGImage:backgroundImage.CGImage] forKey:kCIInputBackgroundImageKey];
outputImage = sourceOverCompositingFilter.outputImage;
CGImage *cgImage = [[CIContext contextWithOptions: nil]createCGImage:outputImage fromRect:outputImage.extent];
//合成图
myImageView.image = [UIImage imageWithCGImage:cgImage];