UIImage和查找中心像素的平均UIColor

时间:2013-06-27 22:50:06

标签: ios objective-c uiimage uicolor

我试图获得UIImage中心16像素的平均UIColor,但是得到的颜色永远不会接近正确。我在这做错了什么?点参数是正确的,我已经验证了这一点。我还有另一种方法,只需使用相同的点就可以返回中心像素的UIColor。

+ (UIColor*)getAverageColorForImage:(UIImage *)image atLocation:(CGPoint)point
{
    int radialSize = 2;

    int xStartPoint = point.x - radialSize;
    int yStartPoint = point.y - radialSize;
    int xEndPoint = point.x + radialSize;
    int yEndPoint = point.y + radialSize;

    CGImageRef rawImageRef = [image CGImage];

    CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider(rawImageRef));
    const UInt8 *rawPixelData = CFDataGetBytePtr(data);

    NSUInteger bytesPerRow = CGImageGetBytesPerRow(rawImageRef);
    NSUInteger stride = CGImageGetBitsPerPixel(rawImageRef) / 8;

    unsigned int red   = 0;
    unsigned int green = 0;
    unsigned int blue  = 0;

    for(int row = yStartPoint; row < yEndPoint; row++)
    {
        const UInt8 *rowPtr = rawPixelData + bytesPerRow * row;

        for(int column = xStartPoint; column < xEndPoint; column++)
        {
            red    += rowPtr[0];
            green  += rowPtr[1];
            blue   += rowPtr[2];

            rowPtr += stride;
        }
     }

    CFRelease(data);

    CGFloat f = 1.0f / (255.0f * (yEndPoint - yStartPoint) * (xEndPoint - xStartPoint));
    return [UIColor colorWithRed:f * red  green:f * green blue:f * blue alpha:1.0f];
}

2 个答案:

答案 0 :(得分:1)

您只是将像素从startPoint迭代到endPoint以下的一个像素。也许正在使用

for(int row = yStartPoint; row <= yEndPoint; row++)
// ...
for(int column = xStartPoint; column <= xEndPoint; column++)

会解决吗?

此外,您必须将rowPtr调整为从xStartPoint开始:

const UInt8 *rowPtr = rawPixelData + bytesPerRow * row + stride * xStartPoint;

否则,您将从每行x = 0开始。

你的平均值应如下计算:

int numberOfPixels = (yEndPoint - yStartPoint) * (xEndPoint - xStartPoint);

red /= numberOfPixels;
green /= numberOfPixels;
blue /= numberOfPixels;

return [UIColor colorWithRed:red / 255.0  green:green / 255.0 blue:blue / 255.0 alpha:1.0f];

答案 1 :(得分:1)

这篇文章来自很久以前,但对于那些想要做类似事情的人来说,我相信我找到了一个解决方案。从本质上讲,这就是Photoshop色彩采样器选择的3x3,5x5,11x11等选项。您的里程可能会有所不同,但我发现这对我有用。

它没有真正清理干净,可能看起来有点笨拙,但它确实有效。

+ (UIColor*)getAverageColorForImage:(UIImage *)image atLocation:(CGPoint)point withRadius:(int)radialSize
{
    int sampleSize = (radialSize * 2) + 1;
    int pixelsToCount = sampleSize * sampleSize;

    int xStartPoint = point.x - radialSize;
    int yStartPoint = point.y - radialSize;

    // Make sure we are not running off the edge of the image
    if(xStartPoint < 0){
        xStartPoint = 0;
    }
    else if (xStartPoint + sampleSize > image.size.width){
        xStartPoint = image.size.width - sampleSize - 1;
    }

    if(yStartPoint < 0){
        yStartPoint = 0;
    }
    else if (yStartPoint + sampleSize > image.size.height){
        yStartPoint = image.size.height - sampleSize - 1;
    }

    // This is the representation of the pixels we would like to average
    CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, CGRectMake(xStartPoint, yStartPoint, sampleSize, sampleSize));

    // Get the image dimensions
    NSUInteger width = CGImageGetWidth(imageRef);
    NSUInteger height = CGImageGetHeight(imageRef);

    // Set the color space to RGB
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    // Understand the bit length of the data
    unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = (NSUInteger)8;

    // Set up a Common Graphics Context
    CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);

    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
    CGContextRelease(context);

    // Now your rawData contains the image data in the RGBA8888 pixel format.
    int byteIndex = (int)((bytesPerRow * 0) + 0 * bytesPerPixel);

    CGFloat cumulativeRed = 0.0;
    CGFloat cumulativeGreen = 0.0;
    CGFloat cumulativeBlue = 0.0;

    for (int ii = 0 ; ii < pixelsToCount ; ++ii){
        CGFloat red   = (rawData[byteIndex]     * 1.0) / 255.0;
        CGFloat green = (rawData[byteIndex + 1] * 1.0) / 255.0;
        CGFloat blue  = (rawData[byteIndex + 2] * 1.0) / 255.0;
        __unused CGFloat alpha = (rawData[byteIndex + 3] * 1.0) / 255.0;

        cumulativeRed += red;
        cumulativeGreen += green;
        cumulativeBlue += blue;

        byteIndex += 4;
    }

    CGFloat avgRed = (cumulativeRed / pixelsToCount);
    CGFloat avgGreen = (cumulativeGreen / pixelsToCount);
    CGFloat avgBlue = (cumulativeBlue / pixelsToCount);

    free(rawData);

    return [UIColor colorWithRed:avgRed green:avgGreen blue:avgBlue alpha:1.0];
}