使用swift的vimagehistogram计算计算图像的直方图

时间:2022-12-27 21:22:38

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

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

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

这是完全相同的问题,但我使用的是swift:使用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?

建议吗?

1 个解决方案

#1


2  

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

我实现了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)
 }

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

(来自https://github.com/FlexMonkey/ShinpuruImage)

#1


2  

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

我实现了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)
 }

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

(来自https://github.com/FlexMonkey/ShinpuruImage)