菜鸟教程小白 发表于 2022-12-11 19:41:45

ios - 将 CVPixelBuffer 转换为 Mat (OpenCV)


                                            <p><p><strong>UPDATE</strong> 似乎与 <strong>LegoCV</strong> 有问题,它甚至无法从简单的 <code>UIImage</code> 创建 <code>OCVMat</code> </p >

<pre><code>    let image = UIImage(named: &#34;myImage&#34;)
    let mat = OCVMat(image: image!)
</code></pre>

<p> <a href="/image/OgIEZ.png" rel="noreferrer noopener nofollow"><img src="/image/OgIEZ.png" alt="enter image description here"/></a> </p>

<hr/>

<hr/>

<p>我正在尝试将 <code>CVPixelBuffer</code>(从相机 - 视频输出)转换为 Mat (OpenCV) - <code>OCVMat</code></p>

<p>我使用下一个框架在我的 iOS Swift 项目中添加 OpenCV <a href="https://github.com/Legoless/LegoCV" rel="noreferrer noopener nofollow">https://github.com/Legoless/LegoCV</a> </p>

<blockquote>
<p>it wraps OpenCV native C++ classes into lightweight Objective-C
classes, which are then natively bridged to Swift</p>
</blockquote>

<p>我为我的相机类实现了 <code>AVCaptureVideoDataOutputSampleBufferDelegate</code> 以获取相机帧缓冲区</p>

<p>设置所需的视频输入并开始 session ,相机工作正常,缓冲区即将到来,但是当我尝试从 <code>CVPixelBuffer</code> 创建 <code>OCVMat</code> ("<code> OCVMat(pixelBuffer: imageBuffer)</code>") 应用程序崩溃并出现下一个错误</p>

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

<p>Objective-C 类:<a href="https://github.com/Legoless/LegoCV/blob/master/LegoCV/LegoCV/Wrapper/Core/Mat/OCVMatDataAllocator.mm" rel="noreferrer noopener nofollow">https://github.com/Legoless/LegoCV/blob/master/LegoCV/LegoCV/Wrapper/Core/Mat/OCVMatDataAllocator.mm</a> </p>

<p> <a href="/image/RAIkh.png" rel="noreferrer noopener nofollow"><img src="/image/RAIkh.png" alt="enter image description here"/></a> </p>

<p><strong>一些 Swift 代码</strong></p>

<pre><code>fileprivate func configureVideoOutput() {
    let videoOutput = AVCaptureVideoDataOutput()
    videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: &#34;sample buffer&#34;))

    if self.session.canAddOutput(videoOutput) {
      print(&#34;canAddOutput yes&#34;)
      self.session.addOutput(videoOutput)
      print(&#34;canAddOutput yes added&#34;)
    } else {
      print(&#34;canAddOutput no&#34;)
    }
}

private func matFromSampleBuffer(sampleBuffer: CMSampleBuffer) -&gt; 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(&#34;Got a frame # \(count)&#34;)
    guard let mat = matFromSampleBuffer(sampleBuffer: sampleBuffer) else { return }
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>解决方案 - 不要使用包装器,最好在 .mm 文件中编写 OpenCV C++ 代码(与 Objective-C 混合)</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 将 CVPixelBuffer 转换为 Mat (OpenCV),我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/48540004/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/48540004/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 将 CVPixelBuffer 转换为 Mat (OpenCV)