Swift 2.2-计数UIImage中的黑色像素

SNo*_*Nos 3 objective-c pixels uiimage ios swift2

我需要计算中的所有黑色像素UIImage。我发现了一个可以工作的代码,但是它是用Objective-C编写的。我曾尝试将其快速转换,但出现很多错误,而且找不到修复它们的方法。

使用Swift执行此操作的最佳方法是什么?

简单的形象在此处输入图片说明

目标C:

/**
 * Structure to keep one pixel in RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA format
 */

struct pixel {
    unsigned char r, g, b, a;
};

/**
 * Process the image and return the number of pure red pixels in it.
 */

- (NSUInteger) processImage: (UIImage*) image
{
    NSUInteger numberOfRedPixels = 0;

    // Allocate a buffer big enough to hold all the pixels

    struct pixel* pixels = (struct pixel*) calloc(1, image.size.width * image.size.height * sizeof(struct pixel));
    if (pixels != nil)
    {
        // Create a new bitmap

        CGContextRef context = CGBitmapContextCreate(
            (void*) pixels,
            image.size.width,
            image.size.height,
            8,
            image.size.width * 4,
            CGImageGetColorSpace(image.CGImage),
            kCGImageAlphaPremultipliedLast
        );

        if (context != NULL)
        {
            // Draw the image in the bitmap

            CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, image.size.width, image.size.height), image.CGImage);

            // Now that we have the image drawn in our own buffer, we can loop over the pixels to
            // process it. This simple case simply counts all pixels that have a pure red component.

            // There are probably more efficient and interesting ways to do this. But the important
            // part is that the pixels buffer can be read directly.

            NSUInteger numberOfPixels = image.size.width * image.size.height;

            while (numberOfPixels > 0) {
                if (pixels->r == 255) {
                    numberOfRedPixels++;
                }
                pixels++;
                numberOfPixels--;
            }

            CGContextRelease(context);
        }

        free(pixels);
    }

    return numberOfRedPixels;
}
Run Code Online (Sandbox Code Playgroud)

akr*_*roy 6

使用Accelerate vImageHistogramCalculation可以更快地获得图像中不同通道的直方图:

let img: CGImage = CIImage(image: image!)!.cgImage!

let imgProvider: CGDataProvider = img.dataProvider!
let imgBitmapData: CFData = imgProvider.data!
var imgBuffer = vImage_Buffer(data: UnsafeMutableRawPointer(mutating: CFDataGetBytePtr(imgBitmapData)), height: vImagePixelCount(img.height), width: vImagePixelCount(img.width), rowBytes: img.bytesPerRow)

let alpha = [UInt](repeating: 0, count: 256)
let red = [UInt](repeating: 0, count: 256)
let green = [UInt](repeating: 0, count: 256)
let blue = [UInt](repeating: 0, count: 256)

let alphaPtr = UnsafeMutablePointer<vImagePixelCount>(mutating: alpha) as UnsafeMutablePointer<vImagePixelCount>?
let redPtr = UnsafeMutablePointer<vImagePixelCount>(mutating: red) as UnsafeMutablePointer<vImagePixelCount>?
let greenPtr = UnsafeMutablePointer<vImagePixelCount>(mutating: green) as UnsafeMutablePointer<vImagePixelCount>?
let bluePtr = UnsafeMutablePointer<vImagePixelCount>(mutating: blue) as UnsafeMutablePointer<vImagePixelCount>?

let rgba = [redPtr, greenPtr, bluePtr, alphaPtr]

let histogram = UnsafeMutablePointer<UnsafeMutablePointer<vImagePixelCount>?>(mutating: rgba)
let error = vImageHistogramCalculation_ARGB8888(&imgBuffer, histogram, UInt32(kvImageNoFlags))
Run Code Online (Sandbox Code Playgroud)

该程序运行后,alpharedgreen,和blue现在直方图在图像的颜色。如果redgreenblue各自仅在第0个位置计数,而alpha仅在最后一个位置计数,则您的图像为黑色。

如果您甚至不想检查多个阵列,可以使用vImageMatrixMultiply合并不同的通道:

let readableMatrix: [[Int16]] = [
    [3,     0,     0,    0]
    [0,     1,     1,    1],
    [0,     0,     0,    0],
    [0,     0,     0,    0]
]

var matrix: [Int16] = [Int16](repeating: 0, count: 16)

for i in 0...3 {
    for j in 0...3 {
        matrix[(3 - j) * 4 + (3 - i)] = readableMatrix[i][j]
    }
}
vImageMatrixMultiply_ARGB8888(&imgBuffer, &imgBuffer, matrix, 3, nil, nil, UInt32(kvImageNoFlags))
Run Code Online (Sandbox Code Playgroud)

如果您在直方图绘制之前imgBuffer将其粘贴,则将在适当位置进行修改以平均每个像素中的RGB,并将平均值写入B通道。这样,您只需检查blue直方图即可,而不是全部三个。

(顺便说一句,vImageMatrixMultiply我找到的最好的描述是在源代码中,例如https://github.com/phracker/MacOSX-SDKs/blob/2d31dd8bdd670293b59869335d9f1f80ca2075e0/MacOSX10.7.sdk/System/Library/Frameworks/Accelerate .framework / Versions / A / Frameworks / vImage.framework / Versions / A / Headers / Transform.h#L21