CIImageProcessorKernel示例代码返回全白结果

时间:2017-12-11 06:49:19

标签: swift xcode ios11 ciimage

我尝试使用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])

结果是全白图像?

3 个答案:

答案 0 :(得分:0)

你需要:

  1. 在真实设备上进行测试。
  2. 调整阈值。
  3. 确保设备的两个摄像头都指向某个对象(例如,表)以确定正在使用哪个对象。

答案 1 :(得分:0)

请务必覆盖public canLeave: boolean = false; //this'll controll if the user can leave the page or not public userType: string = 'user'; // just setting a default value to your select // this is a navguard ionViewCanLeave() { if(!this.canLeave){ if(this.userType == 'user'){ this.canLeave = true; // you'll need to set canLeave to true so when setting the rootpage it doesn't enters the if statemente again this.navCtrl.setRoot('UserPage'); } else { this.canLeave = true; this.navCtrl.setRoot('AdminPage'); } } return true; } outputFormat。例如:

formatForInput

答案 2 :(得分:0)

我最近遇到了同样的错误,问题是这一行:

let thresholdValue = arguments?["thresholdValue"] as? Float

在较新的 Swift 版本中失败,因为 arguments?["thresholdValue"] 包含一个 Double,此时尝试将其转换为 Float 将失败。将行更改为

let thresholdValue = arguments?["thresholdValue"] as? Double

并在 MPSImageThresholdBinary 的初始值设定项中再次浮动。

thresholdValue: Float(thresholdValue)