如何从CVPixelBufferRef转换为openCV cv :: Mat

问题描述 投票:9回答:2

我想对CVPixelBufferRef进行一些操作,然后拿出cv::Mat

  • 收获到感兴趣的地区
  • 缩放到固定维度
  • 均衡直方图
  • 转换为灰度 - 每像素8位(qazxsw poi)

我不确定最有效的顺序是什么,但是,我知道所有操作都可以在open:CV矩阵上使用,所以我想知道如何转换它。

CV_8UC1
c++ ios objective-c opencv avcapturesession
2个回答
12
投票

我在一些- (void) captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection { CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); cv::Mat frame = f(pixelBuffer); // how do I implement f()? 找到了答案。为简单起见,我在这里改编它也为我做了灰度转换。

excellent GitHub source code

我在想最好的订单是:

  1. 转换为灰度(因为它几乎是自动完成的)
  2. 裁剪(这应该是一个快速操作,将减少要使用的像素数)
  3. 缩小
  4. 均衡直方图

3
投票

我正在使用它。我的CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); OSType format = CVPixelBufferGetPixelFormatType(pixelBuffer); // Set the following dict on AVCaptureVideoDataOutput's videoSettings to get YUV output // @{ kCVPixelBufferPixelFormatTypeKey : kCVPixelFormatType_420YpCbCr8BiPlanarFullRange } NSAssert(format == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange, @"Only YUV is supported"); // The first plane / channel (at index 0) is the grayscale plane // See more infomation about the YUV format // http://en.wikipedia.org/wiki/YUV CVPixelBufferLockBaseAddress(pixelBuffer, 0); void *baseaddress = CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0); CGFloat width = CVPixelBufferGetWidth(pixelBuffer); CGFloat height = CVPixelBufferGetHeight(pixelBuffer); cv::Mat mat(height, width, CV_8UC1, baseaddress, 0); // Use the mat here CVPixelBufferUnlockBaseAddress(pixelBuffer, 0); 配置了BGR(8UC3)colorFormat。

CVImageBufferRef - > cv :: Mat

cv:Mat

cv :: Mat - > CVImageBufferRef(CVPixelBufferRef)

- (cv::Mat) matFromImageBuffer: (CVImageBufferRef) buffer {

    cv::Mat mat ;

    CVPixelBufferLockBaseAddress(buffer, 0);

    void *address = CVPixelBufferGetBaseAddress(buffer);
    int width = (int) CVPixelBufferGetWidth(buffer);
    int height = (int) CVPixelBufferGetHeight(buffer);

    mat   = cv::Mat(height, width, CV_8UC4, address, 0);
    //cv::cvtColor(mat, _mat, CV_BGRA2BGR);

    CVPixelBufferUnlockBaseAddress(buffer, 0);

    return mat;
}
© www.soinside.com 2019 - 2024. All rights reserved.