Legoless / LegoCV

Native OpenCV Swift Framework
https://github.com/legoless/legocv
Other
192 stars 27 forks source link

Error when creating OCVMat from CVPixelBuffer (or from any other supported format - UIImage,..) #8

Open anonym24 opened 6 years ago

anonym24 commented 6 years ago

more: https://stackoverflow.com/questions/48540004/converting-cvpixelbuffer-to-mat-opencv

opencv(1934,0x16f087000) malloc: *** error for object 0x16f086108: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

my 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 }
}

screenshot at jan 31 12-10-05

https://github.com/Legoless/LegoCV/blob/master/LegoCV/LegoCV/Wrapper/Core/Mat/OCVMatDataAllocator.mm

anonym24 commented 6 years ago

even simple UIImage to OCVMat won't work!

        let image = UIImage(named: "myImage")
        let mat = OCVMat(image: image!)

opencv(1984,0x1b355cb80) malloc: error for object 0x16ed112d8: pointer being freed was not allocated set a breakpoint in malloc_error_break to debug (lldb)

anonym24 commented 6 years ago

I commented

- (void)dealloc {
    if (source) {
        free(source);
    }

    source = NULL;
}

but another issue with https://github.com/Legoless/LegoCV/blob/10dfdd46ff016f0f3bdc3bf99a2c60ed6bcd66ba/LegoCV/LegoCV/Wrapper/Core/Mat/OCVMatDataAllocator.mm#L21

- (cv::Mat *)source {
    return source;
}

it returns mat with incorrect size, for example initially it was 400x400px than it becomes 1x1498465

yjjwxq commented 4 years ago

I have the same problem, but I don't know how to solve it