triiiiista
triiiiista

Reputation: 378

CIImageProcessorKernel sample codes return all-white result

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

Answers (3)

Nicolas
Nicolas

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

Flex Monkey
Flex Monkey

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

Coder-256
Coder-256

Reputation: 5618

You need to:

  1. Test on a real device.
  2. Adjust the threshold value.
  3. Make sure that both of the device's cameras are pointing at some object (for example, not the table) to figure out which one is being used.

Upvotes: 0

Related Questions