iOS开发中,如何在黑色背景下比较准确地抠出人脸
PHP中文网
PHP中文网 2017-04-18 09:40:56
0
0
936

用户在黑色布的背景下拍摄照片(只有人脸部分),需要把人脸部分抠出来放在透明背景中用在别的地方。
我现在是用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];
PHP中文网
PHP中文网

认证高级PHP讲师

reply all(0)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!