0

在我的项目中,我需要显示给定图像的前三种颜色(请看下面的示例图像)。功能要求是我必须分别获得图像每个像素中的前三种颜色,然后必须为所有像素计算这些颜色。最后,必须将给定图像中呈现的前三种颜色列为输出。(查看了 GPUImage,但我找不到任何符合我要求的代码)。谢谢 ..

在此处输入图像描述

4

1 回答 1

1

使用双 for 循环尝试以下函数。我想,我挑选了一些有人在此处发布的代码,然后进行了一些更改。我不再开发iOS了。所以我无法回答详细的问题。但是你应该能够从这个函数中得到一些想法。

- (UIColor *)getRGBAsFromImage:(UIImage *)image atX:(CGFloat)xx atY:(CGFloat)yy {
    CGImageRef imageRef = [image CGImage];
    NSUInteger width = CGImageGetWidth(imageRef);
    NSUInteger height = CGImageGetHeight(imageRef);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;
    CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                             bitsPerComponent, bytesPerRow, colorSpace,
                                                         kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);
    CGContextDrawImage(context, CGRectMake(0,0,width,height),imageRef);
    CGContextRelease(context);

    int index = 4*((width*yy)+xx);
    int R = rawData[index];
    int G = rawData[index+1];
    int B = rawData[index+2];
    UIColor *aColor;
    aColor = [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1.0];
    rValue = R; gValue = G; bValue = B;
    free(rawData);
    return aColor;
}

// 更新 //

例子

UIColor *c = [self getRGBAsFromImage:colorImage1.image atX:0 atY:0]; // colorImage1 is UIImageView

首先获取图像尺寸。然后使用双 for 循环迭代 x 和 y 值。然后为您的目标将颜色值存储在一个数组中。

于 2013-08-27T06:46:50.387 回答