gpt4 book ai didi

ios - vImageMatrixMultiply_ARGB8888ToPlanar8 在构建 Release模式 iOS 应用程序时不起作用

转载 作者:行者123 更新时间:2023-11-28 07:36:53 26 4
gpt4 key购买 nike

我正在开发图像处理应用程序并尝试使用 vImage。它工作得很好,并在 Debug模式下得到正确的结果。但是,它总是返回黑色图像或在 Release模式下引发错误。

官方已经试过了example当我在 Release模式下构建它时,它也会得到一个黑色图像。

func uiImageToBuffer(image: UIImage) -> vImage_Buffer? {
guard let cgImage = image.cgImage,
var format = cgImageFormat(of: image)
else { return nil }

var buffer = vImage_Buffer()
let error = vImageBuffer_InitWithCGImage(&buffer, &format, nil, cgImage, vImage_Flags(kvImageNoFlags))
if error == kvImageNoError {
return buffer
} else {
return nil
}
}


func grayBuffer(_ buffer: vImage_Buffer) -> vImage_Buffer {
var _buffer = buffer
var destinationBuffer = vImage_Buffer()
vImageBuffer_Init(&destinationBuffer, buffer.height, buffer.width, 8, vImage_Flags(kvImageNoFlags))

let redCoefficient: Float = 0.2126
let greenCoefficient: Float = 0.7152
let blueCoefficient: Float = 0.0722

let divisor = Float(0x1000)

var coefficientsMatrix = [
Int16(redCoefficient * divisor),
Int16(greenCoefficient * divisor),
Int16(blueCoefficient * divisor)
]

var preBias: Int16 = 0
let postBias: Int32 = 0

vImageMatrixMultiply_ARGB8888ToPlanar8(&_buffer, &destinationBuffer,
&coefficientsMatrix, 0x1000, &preBias, postBias, vImage_Flags(kvImageNoFlags))

return destinationBuffer
}

func main() {
guard let imageBuffer = uiImageToBuffer(image: image),
var grayBuffer = grayBuffer(buffer)
else { return }

var monoFormat = vImage_CGImageFormat(bitsPerComponent: 8, bitsPerPixel: 8,
colorSpace: Unmanaged.passRetained(CGColorSpaceCreateDeviceGray()),
bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue),
version: 0, decode: nil, renderingIntent: .defaultIntent)

let cgImage = vImageCreateCGImageFromBuffer(&grayBuffer, &monoFormat, nil, nil, vImage_Flags(kvImageNoFlags), nil)
if let cgImage = cgImage {
imageView.image = UIImage(cgImage: cgImage.takeRetainedValue())
}
free(imageBuffer.data)
free(grayBuffer.data)
}

最佳答案

预偏置应该是一个包含四个元素的向量。试试这个:

let preBias: [Int16] = [0, 0, 0, 0]
let postBias: Int32 = 0

vImageMatrixMultiply_ARGB8888ToPlanar8(&_buffer,
destinationBuffer,
&coefficientsMatrix,
0x1000,
preBias,
postBias,
vImage_Flags(kvImageNoFlags))

关于ios - vImageMatrixMultiply_ARGB8888ToPlanar8 在构建 Release模式 iOS 应用程序时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53090380/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com