【问题标题】:Detect colors ratio in image for iPhone SDK为 iPhone SDK 检测图像中的颜色比例
【发布时间】:2013-03-15 07:42:43
【问题描述】:

我想从图像中检测颜色,我在 AVFoundation 的实时相机类的帮助下逐帧获取图像。现在我想从图像中检测红色并在得到红色后进行操作。我读了这个Post,但找不到任何健康的解决方案。这可能不使用外部库吗?任何教程或帮助材料都会对我有所帮助。提前致谢。

【问题讨论】:

    标签: iphone ios xcode image-processing


    【解决方案1】:

    试试这个代码。 当屏幕发生触摸移动和触摸开始时调用此方法:

    -(void) getPixelColorAtLocation:(CGPoint)point {
    
    unsigned char pixel[4] = {0};
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast);
    CGContextTranslateCTM(context, -point.x, -point.y);
    [self.layer renderInContext:context];
    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
            if((pixel[0]=255) && (pixel[1] = 0) && (pixel[2] = 0)){
         //if pixel are red then this condition become true
    
    }
    

    使用此方法获取图片:

    - (UIImage *)drawLineFromPoint:(CGPoint)fromPoint toPoint:(CGPoint)toPoint image:(UIImage *)image
    {
    
    }
    

    你可以这样称呼它:

    self.image = [self drawLineFromPoint:previousPoint  toPoint:point image:self.image];
    

    【讨论】:

    • 感谢 Keyur,在这段代码中我正在传递 CGPoint,但是我可以在哪里以及如何将 UIImage 设置为这种检测颜色比率的方法?再次感谢。
    • 我已经编辑了我的代码,如果你会遇到任何问题,请尝试让我知道
    • 谢谢,伙计,我已经有了 UIImage,但我想问一下,我怎样才能将此图像传递给“-(void) getPixelColorAtLocation:(CGPoint)point”方法,因为您只是在传递 CGPoint 但在哪里图像?你是如何从图像中获取像素的?
    • 我觉得这里不用传uiimage你只要传你touch point就行了
    • 我不认为我需要 UITouch,我已经有 UIImage,如何从已有的 Image 中读取像素?
    猜你喜欢
    • 1970-01-01
    • 2018-07-16
    • 2011-03-16
    • 2010-12-26
    • 1970-01-01
    • 2016-08-29
    • 1970-01-01
    • 2013-08-19
    • 1970-01-01
    相关资源
    最近更新 更多