gpt4 book ai didi

swift - CIImageProcessorKernel 示例代码返回全白结果

转载 作者:行者123 更新时间:2023-11-28 07:57:08 32 4
gpt4 key购买 nike

我尝试使用 https://developer.apple.com/documentation/coreimage/ciimageprocessorkernel 中的示例代码对手机摄像头捕获的图像进行二值化处理。 ,即:

首先将 CIImageProcessorKernel 子类化为:

class ThresholdImageProcessorKernel: CIImageProcessorKernel {
static let device = MTLCreateSystemDefaultDevice()
override class func process(with inputs: [CIImageProcessorInput]?, arguments: [String : Any]?, output: CIImageProcessorOutput) throws {
guard
let device = device,
let commandBuffer = output.metalCommandBuffer,
let input = inputs?.first,
let sourceTexture = input.metalTexture,
let destinationTexture = output.metalTexture,
let thresholdValue = arguments?["thresholdValue"] as? Float else {
return
}

let threshold = MPSImageThresholdBinary(
device: device,
thresholdValue: thresholdValue,
maximumValue: 1.0,
linearGrayColorTransform: nil)

threshold.encode(
commandBuffer: commandBuffer,
sourceTexture: sourceTexture,
destinationTexture: destinationTexture)
}
}

然后将其用作(在开发者页面中有一个拼写错误,在 ThresholdImageProcessorKernel 处缺少一个 r):

    let result = try? ThresholdImageProcessorKernel.apply( 
withExtent: inputImage.extent,
inputs: [inputImage],
arguments: ["thresholdValue": 0.25])

结果是全白图像??

最佳答案

一定要覆盖 outputFormatformatForInput。例如:

override class var outputFormat: CIFormat {
return kCIFormatBGRA8
}
override class func formatForInput(at input: Int32) -> CIFormat {
return kCIFormatBGRA8
}

关于swift - CIImageProcessorKernel 示例代码返回全白结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47747723/

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