如何将CMSampleBuffer转换为std :: vector< char&gt ;? [英] How to convert CMSampleBuffer to std::vector<char>?

查看:166
本文介绍了如何将CMSampleBuffer转换为std :: vector< char&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的AVCaptureVideoDataOutputSampleBufferDelegate方法具有图像流:

- (void)captureOutput:(AVCaptureOutput )captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection )connection;

然后我要使用sampleBuffer并将其提供给C ++ openalrp库,以识别哪个需要图像字节或原始像素数据.我应该使用什么功能以及如何将sampleBuffer转换为合适的输入类型std::vector<char>unsigned char* pixelData?


Then I want to take sampleBuffer and provide it for C++ openalrp library to recognize which takes image bytes or raw pixel data. What function should I use and how to convert sampleBuffer to suitable input type, std::vector<char> or unsigned char* pixelData?

来自alpr.h文件:

  // Recognize from an image on disk
  AlprResults recognize(std::string filepath);

  // Recognize from byte data representing an encoded image (e.g., BMP, PNG, JPG, GIF etc).
  AlprResults recognize(std::vector<char> imageBytes);

  // Recognize from byte data representing an encoded image (e.g., BMP, PNG, JPG, GIF etc).
  AlprResults recognize(std::vector<char> imageBytes, std::vector<AlprRegionOfInterest> regionsOfInterest);

  // Recognize from raw pixel data.  
  AlprResults recognize(unsigned char* pixelData, int bytesPerPixel, int imgWidth, int imgHeight, std::vector<AlprRegionOfInterest> regionsOfInterest);

推荐答案

最后,我找到了答案:

CVPixelBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(imageBuffer, 0);
unsigned char *tempAddress = (unsigned char *)CVPixelBufferGetBaseAddress(imageBuffer);
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
CVPixelBufferUnlockBaseAddress(imageBuffer, 0);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef newContext = CGBitmapContextCreate(tempAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGImageRef newImage = CGBitmapContextCreateImage(newContext);
CGContextRelease(newContext);
CGColorSpaceRelease(colorSpace);
UIImage *image = [UIImage imageWithCGImage:newImage];

NSData *data = UIImagePNGRepresentation(image);

if (data) {
    unsigned long int len = data.length;
    std::vector<char> v((char *)data.bytes, (char *)data.bytes + len);
    alpr::AlprResults results = delegate->recognize(v);
}

CGImageRelease(newImage);

这篇关于如何将CMSampleBuffer转换为std :: vector&lt; char&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆