【问题标题】:Apply visual effect to images pixel by pixel in Swift在 Swift 中逐个像素地对图像应用视觉效果
【发布时间】:2014-10-24 14:49:07
【问题描述】:

我有一所大学的任务是创建视觉效果并将其应用于通过设备摄像头捕获的视频帧。我目前可以获取图像并显示,但无法更改像素颜色值。

我将样本缓冲区转换为 imageRef 变量,如果我将其转换为 UIImage,一切都很好。

但是现在我想用那个 imageRef 逐个像素地改变它的颜色值,在这个例子中改变为负颜色(我必须做更复杂的事情,所以我不能使用 CIFilters)但是当我执行注释部分时由于访问不正确,它崩溃了。

import UIKit
import AVFoundation

class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {

  let captureSession = AVCaptureSession()
  var previewLayer : AVCaptureVideoPreviewLayer?

  var captureDevice : AVCaptureDevice?

  @IBOutlet weak var cameraView: UIImageView!

  override func viewDidLoad() {
    super.viewDidLoad()

    captureSession.sessionPreset = AVCaptureSessionPresetMedium

    let devices = AVCaptureDevice.devices()

    for device in devices {
      if device.hasMediaType(AVMediaTypeVideo) && device.position == AVCaptureDevicePosition.Back {
        if let device = device as? AVCaptureDevice {
          captureDevice = device
          beginSession()
          break
        }
      }
    }
  }

  func focusTo(value : Float) {
    if let device = captureDevice {
      if(device.lockForConfiguration(nil)) {
        device.setFocusModeLockedWithLensPosition(value) {
          (time) in
        }
        device.unlockForConfiguration()
      }
    }
  }

  override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
    var touchPercent = Float(touches.anyObject().locationInView(view).x / 320)
    focusTo(touchPercent)
  }

  override func touchesMoved(touches: NSSet!, withEvent event: UIEvent!) {
    var touchPercent = Float(touches.anyObject().locationInView(view).x / 320)
    focusTo(touchPercent)
  }

  func beginSession() {
    configureDevice()

    var error : NSError?
    captureSession.addInput(AVCaptureDeviceInput(device: captureDevice, error: &error))

    if error != nil {
      println("error: \(error?.localizedDescription)")
    }

    previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)

    previewLayer?.frame = view.layer.frame
    //view.layer.addSublayer(previewLayer)

    let output = AVCaptureVideoDataOutput()
    let cameraQueue = dispatch_queue_create("cameraQueue", DISPATCH_QUEUE_SERIAL)
    output.setSampleBufferDelegate(self, queue: cameraQueue)
    output.videoSettings = [kCVPixelBufferPixelFormatTypeKey: kCVPixelFormatType_32BGRA]

    captureSession.addOutput(output)
    captureSession.startRunning()
  }

  func configureDevice() {
    if let device = captureDevice {
      device.lockForConfiguration(nil)
      device.focusMode = .Locked
      device.unlockForConfiguration()
    }
  }

  // MARK : - AVCaptureVideoDataOutputSampleBufferDelegate

  func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
    let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
    CVPixelBufferLockBaseAddress(imageBuffer, 0)

    let baseAddress = CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0)
    let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer)
    let width = CVPixelBufferGetWidth(imageBuffer)
    let height = CVPixelBufferGetHeight(imageBuffer)
    let colorSpace = CGColorSpaceCreateDeviceRGB()

    var bitmapInfo = CGBitmapInfo.fromRaw(CGImageAlphaInfo.PremultipliedFirst.toRaw())! | CGBitmapInfo.ByteOrder32Little

    let context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, bitmapInfo)
    let imageRef = CGBitmapContextCreateImage(context)

    CVPixelBufferUnlockBaseAddress(imageBuffer, 0)

    let data = CGDataProviderCopyData(CGImageGetDataProvider(imageRef)) as NSData
    let pixels = data.bytes

    var newPixels = UnsafeMutablePointer<UInt8>()

    //for index in stride(from: 0, to: data.length, by: 4) {

      /*newPixels[index] = 255 - pixels[index]
      newPixels[index + 1] = 255 - pixels[index + 1]
      newPixels[index + 2] = 255 - pixels[index + 2]
      newPixels[index + 3] = 255 - pixels[index + 3]*/
    //}

    bitmapInfo = CGImageGetBitmapInfo(imageRef)
    let provider = CGDataProviderCreateWithData(nil, newPixels, UInt(data.length), nil)

    let newImageRef = CGImageCreate(width, height, CGImageGetBitsPerComponent(imageRef), CGImageGetBitsPerPixel(imageRef), bytesPerRow, colorSpace, bitmapInfo, provider, nil, false, kCGRenderingIntentDefault)

    let image = UIImage(CGImage: newImageRef, scale: 1, orientation: .Right)
    dispatch_async(dispatch_get_main_queue()) {
      self.cameraView.image = image
    }
  }
}

【问题讨论】:

    标签: ios iphone camera swift core-image


    【解决方案1】:

    您在像素操作循环中无法访问,因为 newPixels UnsafeMutablePointer 使用内置 RawPointer 初始化并指向内存中的 0x0000,在我看来,它指向一个未分配的内存空间,您无权存储数据。

    为了获得更长的解释和“解决方案”,我做了一些更改...

    首先,自从 OP 发布以来,Swift 发生了一些变化,这行必须根据 rawValue 的功能进行修改:

        //var bitmapInfo = CGBitmapInfo.fromRaw(CGImageAlphaInfo.PremultipliedFirst.toRaw())! | CGBitmapInfo.ByteOrder32Little
        var bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue) | CGBitmapInfo.ByteOrder32Little
    

    还需要对指针进行一些更改,所以我发布了所有更改(我在其中保留了带有注释标记的原始行)。

        let data = CGDataProviderCopyData(CGImageGetDataProvider(imageRef)) as NSData
    
        //let pixels = data.bytes
        let pixels = UnsafePointer<UInt8>(data.bytes)
    
        let imageSize : Int = Int(width) * Int(height) * 4
    
        //var newPixels = UnsafeMutablePointer<UInt8>()
    
        var newPixelArray = [UInt8](count: imageSize, repeatedValue: 0)
    
        for index in stride(from: 0, to: data.length, by: 4) {
            newPixelArray[index] = 255 - pixels[index]
            newPixelArray[index + 1] = 255 - pixels[index + 1]
            newPixelArray[index + 2] = 255 - pixels[index + 2]
            newPixelArray[index + 3] = pixels[index + 3]
        }
    
        bitmapInfo = CGImageGetBitmapInfo(imageRef)
        //let provider = CGDataProviderCreateWithData(nil, newPixels, UInt(data.length), nil)
        let provider = CGDataProviderCreateWithData(nil, &newPixelArray, UInt(data.length), nil)
    

    一些解释:所有旧像素字节都必须转换为 UInt8,因此不是这样做,而是将像素更改为 UnsafePointer。然后我为新像素创建了一个数组并消除了 newPixels 指针并直接使用该数组。最后将指向新数组的指针添加到提供程序以创建图像。并去掉了对 alpha 字节的修改。

    在此之后,我能够以非常低的性能将一些负面图像放入我的视图中,每十秒大约 1 张图像(iPhone 5,通过 XCode)。并且在 imageview 中呈现第一帧需要很多时间。

    当我将 captureSession.stopRunning() 添加到 didOutputSampleBuffer 函数的开头,然后在处理完成后再次使用 captureSession.startRunning() 开始时,响应速度更快。有了这个,我有将近 1fps 的速度。

    感谢有趣的挑战!

    【讨论】:

    • 道歉,如果这很明显,但是captureOutput函数是如何被触发的?
    • 您的视图必须是 AVCaptureVideoDataOutputSampleBufferDelegate,因为它可以在原始帖子中找到(第一行中的类定义)。然后视图会在服务启动并且缓冲区中有数据后触发该函数。
    • 啊!谢谢。我错过了。我想知道为什么我必须将videoDataOutput.setSampleBufferDelegate(self, queue: sessionQueue) 调整为非自我
    • 虽然,我在 swift 2 中遇到了一个错误,您摘录的第一行 CFData is not convertible to NSData 有什么想法吗?
    • 在我可以访问我的 mac 的同时,尝试使用新 XCode 中内置的 swift 转换器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-19
    • 1970-01-01
    • 2011-08-17
    • 1970-01-01
    相关资源
    最近更新 更多