2017-06-13 98 views
1

我能夠將UIImage轉換爲ARGB CVPixelBuffer,但現在我試圖將UIImage轉換爲灰度級緩衝區。 我想我就再沒代碼經歷,但coreML模型笙歌說:將UIImage轉換爲8灰度像素緩衝區

「錯誤域= com.apple.CoreML代碼= 1」圖片預計不會式 8灰色,反而是不支持的(40)」

這裏是灰度CGContext我到目前爲止:

public func pixelBufferGray(width: Int, height: Int) -> CVPixelBuffer? { 

     var pixelBuffer : CVPixelBuffer? 
     let attributes = [kCVPixelBufferCGImageCompatibilityKey: kCFBooleanTrue, kCVPixelBufferCGBitmapContextCompatibilityKey: kCFBooleanTrue] 

     let status = CVPixelBufferCreate(kCFAllocatorDefault, Int(width), Int(height), kCVPixelFormatType_8IndexedGray_WhiteIsZero, attributes as CFDictionary, &pixelBuffer) 

     guard status == kCVReturnSuccess, let imageBuffer = pixelBuffer else { 
      return nil 
     } 

     CVPixelBufferLockBaseAddress(imageBuffer, CVPixelBufferLockFlags(rawValue: 0)) 

     let imageData = CVPixelBufferGetBaseAddress(imageBuffer) 

     guard let context = CGContext(data: imageData, width: Int(width), height:Int(height), 
             bitsPerComponent: 8, bytesPerRow: CVPixelBufferGetBytesPerRow(imageBuffer), 
             space: CGColorSpaceCreateDeviceGray(), 
             bitmapInfo: CGImageAlphaInfo.none.rawValue) else { 
             return nil 
     } 

     context.translateBy(x: 0, y: CGFloat(height)) 
     context.scaleBy(x: 1, y: -1) 

     UIGraphicsPushContext(context) 
     self.draw(in: CGRect(x:0, y:0, width: width, height: height)) 
     UIGraphicsPopContext() 
     CVPixelBufferUnlockBaseAddress(imageBuffer, CVPixelBufferLockFlags(rawValue: 0)) 

     return imageBuffer 

    } 

任何幫助,將不勝感激

+1

當你創建這個代碼上下文,您是否嘗試用kCVPixelFormatType_8IndexedGray_WhiteIsZero或kCVPixelFormatType_8Indexed替換空格參數? –

+0

感謝您的幫助,正確的像素格式是kCVPixelFormatType_OneComponent8灰度UIImage – user1988824

+0

你可能已經投了我的評論至少大聲笑 –

回答

4

即使圖像稱爲灰度,正確的像素格式爲:kCVPixelFormatType_OneComponent8


希望這個完整的代碼片段將幫助別人一起:

public func pixelBufferGray(width: Int, height: Int) -> CVPixelBuffer? { 

     var pixelBuffer : CVPixelBuffer? 
     let attributes = [kCVPixelBufferCGImageCompatibilityKey: kCFBooleanTrue, kCVPixelBufferCGBitmapContextCompatibilityKey: kCFBooleanTrue] 

     let status = CVPixelBufferCreate(kCFAllocatorDefault, Int(width), Int(height), kCVPixelFormatType_OneComponent8, attributes as CFDictionary, &pixelBuffer) 

     guard status == kCVReturnSuccess, let imageBuffer = pixelBuffer else { 
      return nil 
     } 

     CVPixelBufferLockBaseAddress(imageBuffer, CVPixelBufferLockFlags(rawValue: 0)) 

     let imageData = CVPixelBufferGetBaseAddress(imageBuffer) 

     guard let context = CGContext(data: imageData, width: Int(width), height:Int(height), 
             bitsPerComponent: 8, bytesPerRow: CVPixelBufferGetBytesPerRow(imageBuffer), 
             space: CGColorSpaceCreateDeviceGray(), 
             bitmapInfo: CGImageAlphaInfo.none.rawValue) else { 
             return nil 
     } 

     context.translateBy(x: 0, y: CGFloat(height)) 
     context.scaleBy(x: 1, y: -1) 

     UIGraphicsPushContext(context) 
     self.draw(in: CGRect(x:0, y:0, width: width, height: height)) 
     UIGraphicsPopContext() 
     CVPixelBufferUnlockBaseAddress(imageBuffer, CVPixelBufferLockFlags(rawValue: 0)) 

     return imageBuffer 

    }