iPhone中图像的直方图

3
我正在寻找一种在 iPhone 上获取图像直方图的方法。OpenCV 库太大了,无法包含在我的应用程序中(编译后 OpenCV 大约有 70MB),但我可以使用 OpenGL。然而,我不知道如何做到这两个操作。
我已经找到了如何获取图像的像素,但无法形成直方图。这似乎很简单,但我不知道如何将 uint8_t 存储到数组中。
以下是相关问题/答案,用于查找像素: Getting RGB pixel data from CGImage
2个回答

0

uint8_t*只是指向包含给定颜色字节的c数组的指针,即{r,g,b,a}或者您的图像缓冲区的颜色字节布局。

因此,参考您提供的链接和直方图的定义:

//Say we're in the inner loop and we have a given pixel in rgba format
const uint8_t* pixel = &bytes[row * bpr + col * bytes_per_pixel];
//Now save to histogram_counts uint32_t[4] planes r,g,b,a
//or you could just do one for brightness
//If you want to do data besides rgba, use bytes_per_pixel instead of 4
for (int i=0; i<4; i++) {
    //Increment count of pixels with this value
    histogram_counts[i][pixel[i]]++;
}

我该如何定义histogram_counts数组?然后,我该如何将整个数组输出到NSString中? - Flipper

0

您可以使用CGRef获取图像的RGB颜色。请看下面我用于此目的的方法。

- (UIImage *)processUsingPixels:(UIImage*)inputImage {

// 1. Get the raw pixels of the image
UInt32 * inputPixels;

CGImageRef inputCGImage = [inputImage CGImage];
NSUInteger inputWidth = CGImageGetWidth(inputCGImage);
NSUInteger inputHeight = CGImageGetHeight(inputCGImage);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

NSUInteger bytesPerPixel = 4;
NSUInteger bitsPerComponent = 8;

NSUInteger inputBytesPerRow = bytesPerPixel * inputWidth;

inputPixels = (UInt32 *)calloc(inputHeight * inputWidth, sizeof(UInt32));

CGContextRef context = CGBitmapContextCreate(inputPixels, inputWidth, inputHeight,
                                             bitsPerComponent, inputBytesPerRow, colorSpace,
                                             kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

// 3. Convert the image to Black & White
for (NSUInteger j = 0; j < inputHeight; j++) {
    for (NSUInteger i = 0; i < inputWidth; i++) {
        UInt32 * currentPixel = inputPixels + (j * inputWidth) + i;
        UInt32 color = *currentPixel;

        // Average of RGB = greyscale
        UInt32 averageColor = (R(color) + G(color) + B(color)) / 3.0;

        *currentPixel = RGBAMake(averageColor, averageColor, averageColor, A(color));
    }
}

// 4. Create a new UIImage
CGImageRef newCGImage = CGBitmapContextCreateImage(context);
UIImage * processedImage = [UIImage imageWithCGImage:newCGImage];

// 5. Cleanup!
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);

   return processedImage;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接