iPhone中的图像直方图 [英] Histogram of Image in iPhone

查看:1384
本文介绍了iPhone中的图像直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在iPhone上获取图像直方图的方法。 OpenCV库太大了,无法包含在我的应用程序中(OpenCV大约70MB编译),但我可以使用OpenGL。但是,我不知道如何做其中任何一个。

I am looking for a way to get a histogram of an image on the iPhone. The OpenCV library is way too big to be included in my app (OpenCV is about 70MB compiled), but I can use OpenGL. However, I have no idea on how to do either of these.

我已经找到了如何获取图像的像素,但不能形成直方图。这似乎应该很简单,但我不知道如何将 uint8_t 存储到数组中。

I have found how to get the pixels of the image, but cannot form a histogram. This seems like it should be simple, but I don't know how to store uint8_t into an array.

这是相关的问题/查找像素的答案:

Here is the relevant question/answer for finding pixels:

从CGImage获取RGB像素数据

推荐答案

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

You can take RGB color of your image with CGRef. Look at the below method which I used for this.

- (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;
}

这篇关于iPhone中的图像直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆