Reputation: 378
I tried binarizing the image captured by the phone camera with the sample codes at https://developer.apple.com/documentation/coreimage/ciimageprocessorkernel, that is:
First subclassing CIImageProcessorKernel
as:
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)
}
}
Then use it as (there is a typo in the Developer page missing an r at ThresholdImageProcessorKernel
):
let result = try? ThresholdImageProcessorKernel.apply(
withExtent: inputImage.extent,
inputs: [inputImage],
arguments: ["thresholdValue": 0.25])
The result is an all-white image??
Upvotes: 1
Views: 687
Reputation: 785
I encountered the same error recently and the problem is this line:
let thresholdValue = arguments?["thresholdValue"] as? Float
which fails in newer Swift versions because arguments?["thresholdValue"]
contains a Double
and trying to cast it to Float
will fail at this point. Change the line to
let thresholdValue = arguments?["thresholdValue"] as? Double
and in the initializer of MPSImageThresholdBinary
make a float out of it again.
thresholdValue: Float(thresholdValue)
Upvotes: 0
Reputation: 3633
Be sure to override outputFormat
and formatForInput
. For example:
override class var outputFormat: CIFormat {
return kCIFormatBGRA8
}
override class func formatForInput(at input: Int32) -> CIFormat {
return kCIFormatBGRA8
}
Upvotes: 1
Reputation: 5618
You need to:
Upvotes: 0