0

我正在尝试获取 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];
}
4

2 回答 2

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];
}
于 2014-05-01T02:13:01.467 回答
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];
于 2013-06-27T23:25:49.223 回答