【问题标题】:How to read RGB pixel data on iPhone如何在 iPhone 上读取 RGB 像素数据
【发布时间】:2012-02-14 02:32:09
【问题描述】:

我想知道如何在 iPhone 上扫描图像并分析每个像素的 RGB 值,从而最终确定整个图像的平均 RGB。如果有人能把我推向正确的方向,将不胜感激。我是图像分析的新手,不知道从哪里开始,或者 iOS 5 API 中是否包含类似的东西。

【问题讨论】:

    标签: iphone ios image rgb


    【解决方案1】:

    粘贴即可,我正在检测触摸颜色。

    - (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
    
    if (self.view.hidden==YES) {
        //color wheel is hidden, so don't handle  this as a color wheel event.
        [[self nextResponder] touchesEnded:touches withEvent:event];
        return;
    }
    
    UITouch* touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view]; //where image was tapped
    UIColor * lastColor = [self getPixelColorAtLocation:point];
    NSLog(@"color %@",lastColor);
    UIImageView *lbl=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    lbl.layer.cornerRadius=50;
    [imageView addSubview:lbl];
    lbl.backgroundColor=lastColor;
    lbl.center=CGPointMake(stillImageFilter.center.x*320, (stillImageFilter.center.y*320)-125);
    NSLog(@"stillImageCenter = %f,%f",stillImageFilter.center.x,stillImageFilter.center.y);}
    
    - (UIColor*) getPixelColorAtLocation:(CGPoint)point {
    UIColor* color = nil;
    CGImageRef inImage = imageView.image.CGImage;
    
    CGContextRef cgctx = [self 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);
        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,      // bits per component
                                     bitmapBytesPerRow,
                                     colorSpace,
                                     kCGImageAlphaPremultipliedFirst);
    if (context == NULL)
    {
        free (bitmapData);
        fprintf (stderr, "Context not created!");
    }
    
    
    CGColorSpaceRelease( colorSpace );
    
    return context;
    

    }

    【讨论】:

    【解决方案2】:

    【讨论】:

    • 对于您发布的第二个链接,如果我希望将其编码为点击按钮,所有这些都会进入 IBAction 函数吗?我正在理解代码,只是对如何实现它感到好奇。谢谢。
    【解决方案3】:

    从 UIImage 获取 CGImage 可以为您提供这些数据

    CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage));
    const UInt8* data = CFDataGetBytePtr(pixelData);
    
    int pixelInfo = ((image.size.width  * y) + x ) * 4; // The image is png
    
    UInt8 red = data[pixelInfo];         
    UInt8 green = data[(pixelInfo + 1)]; 
    UInt8 blue = data[pixelInfo + 2];    
    UInt8 alpha = data[pixelInfo + 3];     
    CFRelease(pixelData);
    

    更多: Getting pixel data from UIImageView -- works on simulator, not device

    这里:Get Pixel color of UIImage

    【讨论】:

    • 是的,这会让我成为你的克星吗? :)
    • 这是否会扫描整个图像,因为它正在定义 image.size.width?
    猜你喜欢
    • 1970-01-01
    • 2010-09-13
    • 1970-01-01
    • 1970-01-01
    • 2010-09-13
    • 1970-01-01
    • 2011-08-29
    • 2011-05-20
    • 1970-01-01
    相关资源
    最近更新 更多