谈谈iOS下图像颜色检测、颜色匹配的解决方法
来源:互联网 发布:zepto.js animate 编辑:程序博客网 时间:2024/05/02 11:49
最近写一个应用,需要对图像中某一个像素点的颜色进行取样,并检测是否与特定颜色一致或相近,解决的办法如下:
二、颜色匹配计算
一是精确的匹配,通过生成两个CGColor,利用CGColorEqualToColor函数进行对比即可,如下:
二是模糊的匹配,在RGB色彩空间下, 每个像素点的颜色有四个通道(R, G, B, Alpha),比较
两个颜色是否一致的方法,可以通过比较其向量的方式来进行,如下:
至于阀值的设定,就需要大家根据实际情况来调整了,一般可设置为85,如果严格一点,则小一点;宽松一点,则可以设大一点。
参考文章:http://www.markj.net/iphone-uiimage-pixel-color/
一、像素点颜色取样(代码参考国外一篇文章,在最后列出)
+ (UIColor*) getPixelColorAtLocation:(CGPoint)point inImage:(UIImage *)image { UIColor* color = nil; CGImageRef inImage = image.CGImage; CGContextRef cgctx = [DetectColour createARGBBitmapContextFromImage: inImage]; if (cgctx == NULL) { return nil; /* error */ } size_t w = CGImageGetWidth(inImage); size_t h = CGImageGetHeight(inImage); CGRect rect = {{0,0},{w,h}}; CGContextDrawImage(cgctx, rect, inImage); unsigned char* data = CGBitmapContextGetData (cgctx); if (data != NULL) { int offset = 4*((w*round(point.y))+round(point.x)); int alpha = data[offset]; int red = data[offset+1]; int green = data[offset+2]; int blue = data[offset+3]; NSLog(@"offset: %i colors: RGB A %i %i %i %i",offset,red,green, blue,alpha); NSLog(@"x:%f y:%f", point.x, point.y); color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue: (blue/255.0f) alpha:(alpha/255.0f)]; } CGContextRelease(cgctx); if (data) { free(data); } return color;}+ (CGContextRef) createARGBBitmapContextFromImage:(CGImageRef) inImage {CGContextRef context = NULL;CGColorSpaceRef colorSpace;void * bitmapData;int bitmapByteCount;int bitmapBytesPerRow;size_t pixelsWide = CGImageGetWidth(inImage);size_t pixelsHigh = CGImageGetHeight(inImage);bitmapBytesPerRow = (pixelsWide * 4);bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);colorSpace = CGColorSpaceCreateDeviceRGB();if (colorSpace == NULL){fprintf(stderr, "Error allocating color space\n");return NULL;}bitmapData = malloc( bitmapByteCount );if (bitmapData == NULL){fprintf (stderr, "Memory not allocated!");CGColorSpaceRelease( colorSpace );return NULL;} context = CGBitmapContextCreate (bitmapData,pixelsWide,pixelsHigh,8, bitmapBytesPerRow,colorSpace,kCGImageAlphaPremultipliedFirst);if (context == NULL){free (bitmapData);fprintf (stderr, "Context not created!");}CGColorSpaceRelease( colorSpace );return context;}
二、颜色匹配计算
一是精确的匹配,通过生成两个CGColor,利用CGColorEqualToColor函数进行对比即可,如下:
//生成采样对照颜色(黑色) UIColor* sampleColor = [UIColor colorWithRed:(0/255.0f) green:(0/255.0f) blue:(0/255.0f) alpha:(255/255.0f)];//比较 if (CGColorEqualToColor(detectedColor.CGColor, sampleColor.CGColor)) { //处理}
二是模糊的匹配,在RGB色彩空间下, 每个像素点的颜色有四个通道(R, G, B, Alpha),比较
两个颜色是否一致的方法,可以通过比较其向量的方式来进行,如下:
//获取颜色分量const CGFloat* components = CGColorGetComponents(detectedColor.CGColor);CGFloat Red, Green, Blue, Alpha;Alpha = components[0];Red = components[0+1];....//向量比较float difference = pow( pow((red1 - red2), 2) + pow((green1 - green2), 2) +pow((blue1 - blue2), 2), 0.5 );
至于阀值的设定,就需要大家根据实际情况来调整了,一般可设置为85,如果严格一点,则小一点;宽松一点,则可以设大一点。
参考文章:http://www.markj.net/iphone-uiimage-pixel-color/
- 谈谈iOS下图像颜色检测、颜色匹配的解决方法
- 谈谈iOS下图像颜色检测、颜色匹配的解决方法
- 谈谈iOS下图像颜色检测、颜色匹配的解决方法
- iOS下图像颜色检测、颜色匹配的解决方法
- iOS - UITextView 匹配@和#号的特殊颜色
- 匹配颜色
- 颜色匹配
- 检测图片的主要颜色
- ios navigationBar颜色比实际设置值浅的解决方法
- ios navigationBar颜色比实际设置值浅的解决方法
- 谈谈巨头公司的LOGO颜色设计
- 谈谈改变 UIAlertView 和 UIActionSheet 的颜色
- android 沉浸式状态栏(像ios那样的状态栏与应用统一颜色样式)
- android 沉浸式状态栏(像ios那样的状态栏与应用统一颜色样式)
- android 沉浸式状态栏(像ios那样的状态栏与应用统一颜色样式)
- android 沉浸式状态栏(像ios那样的状态栏与应用统一颜色样式)
- android 沉浸式状态栏(像ios那样的状态栏与应用统一颜色样式)
- android 沉浸式状态栏(像ios那样的状态栏与应用统一颜色样式)
- 进制转换的一种思路两种实现方法
- scp 无密码传输
- ASP.Net 去除字符串中的html标记
- 二级菜单
- 读研的那些事儿(五)
- 谈谈iOS下图像颜色检测、颜色匹配的解决方法
- PHP面向对象
- sql语法
- 。。。
- Mysql 数据类型
- 使用泛型迭代Map集合
- loveJ总结
- linux 批量删除 find exec rm
- Linux学习笔记----vim文本编辑软件