iPhone 中图像的直方图

发布于 2024-11-28 17:18:25 字数 339 浏览 1 评论 0原文

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

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

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

从 CGImage 获取 RGB 像素数据

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.

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:

Getting RGB pixel data from CGImage

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡淡離愁欲言轉身 2024-12-05 17:18:25

uint8_t* 只是一个指向 ac 数组的指针,其中包含给定颜色的字节,即 {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]]++;
}

The uint8_t* is just a pointer to a c array containing the bytes of the given color, i.e. {r, g, b, a} or whatever the color byte layout is for your image buffer.

So, referencing the link you provided, and the definition of histogram:

//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]]++;
}
情绪 2024-12-05 17:18:25

您可以使用 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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文