使用swift中的vImageHistogramCalculation计算图像的直方图

问题描述 投票:3回答:1

我正在尝试使用Accelerate vImageHistogramCalculation_ARGBFFFF函数来计算图像的直方图,但我得到了vImage_Error类型的kvImageNullPointerArgument(错误代码是-21772)。

这是完全相同的问题,但我在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)

问题出在直方图变量中,我需要重新创建这样的东西:

// 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.

建议?

ios swift histogram accelerate-framework vimage
1个回答
2
投票

我已经实现了vImageHistogramCalculation_ARGB8888作为Swift中UIImage的扩展,具体如下:

 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)
 }

(摘自https://github.com/FlexMonkey/ShinpuruImage

© www.soinside.com 2019 - 2024. All rights reserved.