Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compute the histogram of an image using vImageHistogramCalculation in swift

I'm trying to compute the histogram of an image using Accelerate vImageHistogramCalculation_ARGBFFFF function, but I'm getting a vImage_Error of type kvImageNullPointerArgument (error code is -21772).

This is the exact same question, but I'm working in Swift: Compute the histogram of an image using vImageHistogramCalculation

    // Get CGImage from UIImage
    var image:UIImage = UIImage(named: "happiness1")!
    var img:CGImageRef = image.CGImage

    // Create vImage_Buffer with data from CGImageRef
    var inProvider:CGDataProviderRef = CGImageGetDataProvider(img)
    var inBitmapData:CFDataRef = CGDataProviderCopyData(inProvider)

    // The next three lines set up the inBuffer object
    var height:vImagePixelCount = CGImageGetHeight(img)
    var width:vImagePixelCount = CGImageGetWidth(img)
    var rowBytes:UInt = CGImageGetBytesPerRow(img)
    var data:UnsafePointer<Void> = UnsafePointer<Void>(CFDataGetBytePtr(inBitmapData))

    // Setup inBuffer
    var inBuffer = vImage_Buffer(data: &data, height: height, width: width, rowBytes: rowBytes)

    var histogram_entries:UInt32 = 4
    var minVal:Pixel_F = 0
    var maxVal:Pixel_F = 255
    //let flags:vImage_Flags = kvImageNoFlags = 0

    var histogram = UnsafeMutablePointer<UnsafeMutablePointer<vImagePixelCount>>()

    var error:vImage_Error = vImageHistogramCalculation_ARGBFFFF(&inBuffer, histogram, histogram_entries, minVal, maxVal, 0)

    println(error)

The problem is in the histogram variable, I need to recreate something like this:

// create an array of four histograms with eight entries each.
vImagePixelCount histogram[4][8] = {{0}};  
// vImageHistogramCalculation requires an array of pointers to the histograms.
vImagePixelCount *histogramPointers[4] = { &histogram[0][0], &histogram[1][0], &histogram[2][0], &histogram[3][0] };
vImage_Error error = vImageHistogramCalculation_ARGBFFFF(&inBuffer, histogramPointers, 8, 0, 255, kvImageNoFlags);
// You can now access bin j of the histogram for channel i as histogram[i][j].
// The storage for the histogram will be cleaned up when execution leaves the
// current lexical block.

Suggestion?

like image 841
Marco Avatar asked Jan 08 '23 18:01

Marco


2 Answers

I've implemented vImageHistogramCalculation_ARGB8888 as an extension to UIImage in Swift with the following:

 func SIHistogramCalculation() -> (alpha: [UInt], red: [UInt], green: [UInt], blue: [UInt]) {
   let imageRef = self.CGImage
   let inProvider = CGImageGetDataProvider(imageRef)
   let inBitmapData = CGDataProviderCopyData(inProvider)

   var inBuffer = vImage_Buffer(data: UnsafeMutablePointer(CFDataGetBytePtr(inBitmapData)), height: UInt(CGImageGetHeight(imageRef)), width: UInt(CGImageGetWidth(imageRef)), rowBytes: CGImageGetBytesPerRow(imageRef))

   var alpha = [UInt](count: 256, repeatedValue: 0)
   var red = [UInt](count: 256, repeatedValue: 0)
   var green = [UInt](count: 256, repeatedValue: 0)
   var blue = [UInt](count: 256, repeatedValue: 0)

   var alphaPtr = UnsafeMutablePointer<vImagePixelCount>(alpha)
   var redPtr = UnsafeMutablePointer<vImagePixelCount>(red)
   var greenPtr = UnsafeMutablePointer<vImagePixelCount>(green)
   var bluePtr = UnsafeMutablePointer<vImagePixelCount> (blue)

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

   var histogram = UnsafeMutablePointer<UnsafeMutablePointer<vImagePixelCount>>(rgba)
   var error = vImageHistogramCalculation_ARGB8888(&inBuffer, histogram, UInt32(kvImageNoFlags))
   return (alpha, red, green, blue)
 }

(Taken from https://github.com/FlexMonkey/ShinpuruImage)

like image 111
Simon Gladman Avatar answered Jan 15 '23 20:01

Simon Gladman


For Swift 5, you need to explicitly let the compiler know that your pointers are optional. Change your UnsafeMutablePointer declarations to the following:

Swift 5 version:

let redPtr = red.withUnsafeMutableBufferPointer { $0.baseAddress }
let greenPtr = green.withUnsafeMutableBufferPointer { $0.baseAddress }
let bluePtr = blue.withUnsafeMutableBufferPointer { $0.baseAddress }
let alphaPtr = alphaChannel.withUnsafeMutableBufferPointer { $0.baseAddress }
let histogram = UnsafeMutablePointer<UnsafeMutablePointer<vImagePixelCount>?>.allocate(capacity: 4)

histogram[0] = redPtr
histogram[1] = greenPtr
histogram[2] = bluePtr
histogram[3] = alphaPtr
let error:vImage_Error = vImageHistogramCalculation_ARGB8888(&inBuffer, histogram, UInt32(kvImageNoFlags))

Swift 4 version:

let redPtr: UnsafeMutablePointer<vImagePixelCount>? = UnsafeMutablePointer(mutating: red)
let greenPtr: UnsafeMutablePointer<vImagePixelCount>? = UnsafeMutablePointer(mutating:green)
let bluePtr: UnsafeMutablePointer<vImagePixelCount>? = UnsafeMutablePointer(mutating:blue)
let alphaPtr: UnsafeMutablePointer<vImagePixelCount>? = UnsafeMutablePointer(mutating:alpha)
like image 40
fargofargofargo Avatar answered Jan 15 '23 21:01

fargofargofargo