Get pixel value from CVPixelBufferRef in Swift

前端 未结 4 1769
鱼传尺愫
鱼传尺愫 2020-12-05 11:36

How can I get the RGB (or any other format) pixel value from a CVPixelBufferRef? Ive tried many approaches but no success yet.

func captureOutput(captureOutp         


        
4条回答
  •  误落风尘
    2020-12-05 12:05

    Update for Swift3:

    let pixelBuffer: CVPixelBufferRef = CMSampleBufferGetImageBuffer(sampleBuffer)!
    CVPixelBufferLockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0));
    let int32Buffer = unsafeBitCast(CVPixelBufferGetBaseAddress(pixelBuffer), to: UnsafeMutablePointer.self)
    let int32PerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)
    // Get BGRA value for pixel (43, 17)
    let luma = int32Buffer[17 * int32PerRow + 43]
    
    CVPixelBufferUnlockBaseAddress(pixelBuffer, 0)
    

提交回复
热议问题