如何在iOS中检测颜色并打印其名称? [英] How to detect a color and print its name in iOS?

查看:51
本文介绍了如何在iOS中检测颜色并打印其名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检测图像/摄像机上的主色并打印其名称?

How can I detect dominant color on an image/cam and print its name?

例如:在图像中可能存在非常浅的蓝色,浅蓝色,蓝色,深蓝色,这是因为当我分析该图像时,我想使自己知道主导色是蓝色,然后打印该图像或物体时,该物体或图像上的闪电屏幕上显示蓝色".如果我需要使用RGB,我想我需要颜色范围对吗?

eg: in image there can be, very light blue, light blue, blue, dark blue just because of lightning on that object or image when I analyze that image I want to make that I know dominant color is blue, and print "Blue" on screen. If I need to work with RGB I think I will need color ranges right?

ps:图像上也可能有红色,黄色等,但我想检测主要颜色.

ps: there can be also red, yellow etc on image but I want to detect dominant color.

推荐答案

- (UIColor *)averageColor {

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char rgba[4];
    CGContextRef context = CGBitmapContextCreate(rgba, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

    CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), self.CGImage);
    CGColorSpaceRelease(colorSpace);
    CGContextRelease(context);  

    if(rgba[3] > 0) {
        CGFloat alpha = ((CGFloat)rgba[3])/255.0;
        CGFloat multiplier = alpha/255.0;
        return [UIColor colorWithRed:((CGFloat)rgba[0])*multiplier
                               green:((CGFloat)rgba[1])*multiplier
                                blue:((CGFloat)rgba[2])*multiplier
                               alpha:alpha];
    }
    else {
        return [UIColor colorWithRed:((CGFloat)rgba[0])/255.0
                               green:((CGFloat)rgba[1])/255.0
                                blue:((CGFloat)rgba[2])/255.0
                               alpha:((CGFloat)rgba[3])/255.0];
    }
}

来自 Objective-c-在图像中获得最少使用和最常用的颜色

来自 http://www.bobbygeorgescu.com/2011/08/finding-average-color-of-uiimage/

这篇关于如何在iOS中检测颜色并打印其名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆