UPDATE seems issue with LegoCV, it can't even create OCVMat
from simple UIImage
let image = UIImage(named: "myImage")
let mat = OCVMat(image: image!)
I'm trying to convert CVPixelBuffer
(from a Camera - Video Output) to a Mat (OpenCV) - OCVMat
I use the next framework to add OpenCV in my iOS Swift project https://github.com/Legoless/LegoCV
it wraps OpenCV native C++ classes into lightweight Objective-C classes, which are then natively bridged to Swift
I implemented AVCaptureVideoDataOutputSampleBufferDelegate
for my camera class to get camera frame buffers
set needed video input and started a session, camera works ok, buffers are coming, but when I'm trying to create OCVMat
from CVPixelBuffer
("OCVMat(pixelBuffer: imageBuffer)
") app crashes with next error
opencv(1934,0x16f087000) malloc: *** error for object 0x16f086108: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Objective-C class: https://github.com/Legoless/LegoCV/blob/master/LegoCV/LegoCV/Wrapper/Core/Mat/OCVMatDataAllocator.mm
Some Swift Code
fileprivate func configureVideoOutput() {
let videoOutput = AVCaptureVideoDataOutput()
videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "sample buffer"))
if self.session.canAddOutput(videoOutput) {
print("canAddOutput yes")
self.session.addOutput(videoOutput)
print("canAddOutput yes added")
} else {
print("canAddOutput no")
}
}
private func matFromSampleBuffer(sampleBuffer: CMSampleBuffer) -> OCVMat? {
guard let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return nil }
return OCVMat(pixelBuffer: imageBuffer)
}
public func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
count = count + 1
print("Got a frame # \(count)")
guard let mat = matFromSampleBuffer(sampleBuffer: sampleBuffer) else { return }
}