iOS开发-简单图片背景替换(实现抠图效果)
之前好奇, 想实现这样的功能 -----> iOS图像处理-(jpg去除白色背景)
把一张图片(.jpg)的白色背景抠掉,转成.png 格式的有alpha通道的透明图。
原图黑白分明, 像这样转换成这样
然后在论坛,得到了想要的答案。这里先谢过那位大牛, 也提供了参考资料: iOS8 Core Image In Swift:更复杂的滤镜
然后今天, 自己也总结一下。
写了个小小的demo, 实现背景图片的切换. 效果如下:
可以看到, 原先的黄色渐变背景被替换掉了。 接下去就是要实现这样一个功能。
当然. 如果感兴趣, 你可以从这里下载到源码: http://download.csdn.net/detail/hitwhylz/8186081
消除橙黄色
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 you want to remove float alpha = (hsv[0] > minHueAngle && hsv[0] < 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 into memory for the next color value } } } map.data = cubeData; return map; }
我将这个方法稍微改造了一下,选择一个结构体,因为外面要用到length和dimension。苹果没有提供rgbToHSV方法的实现,可以用我找到的这个:
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 *= 60; if( *h < 0 ) *h += 360; }
#include <stdio.h> #include <stdlib.h> #include <math.h>
使用这个文件的时候, 需要把调用方, 比如我这里的ViewController.m 改为 ViewController.mm
这样就能支持c文件。 然后导入头文件 #include "cubeMap.c"
之后就是简单的调用了, 具体参加如下代码:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //size 600?×?450 //显示原图片 UILabel *oldLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 65, 160, 15)]; oldLabel.text = @"原图"; 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]; //显示背景图片 UILabel *backLabel = [[UILabel alloc]initWithFrame:CGRectMake(160, 65, 160, 15)]; backLabel.text = @"背景图"; 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]; //更换背景图片 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 = @"合成图"; 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]; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。