【问题标题】:Histogram of Image in iPhoneiPhone中的图像直方图
【发布时间】:2011-10-22 21:13:23
【问题描述】:

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

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

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

Getting RGB pixel data from CGImage

【问题讨论】:

    标签: iphone objective-c image-processing histogram


    【解决方案1】:

    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 中呢?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2011-08-17
      • 2012-08-22
      • 2018-06-10
      • 1970-01-01
      • 2017-10-11
      • 2015-05-01
      • 1970-01-01
      • 2013-05-02
      • 1970-01-01
      相关资源
      最近更新 更多