【问题标题】:How to convert CMSampleBuffer to std::vector<char>?如何将 CMSampleBuffer 转换为 std::vector<char>?
【发布时间】:2023-04-04 06:58:01
【问题描述】:

我有来自我的AVCaptureVideoDataOutputSampleBufferDelegate 方法的图像流:

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

然后我想把sampleBuffer 提供给C++ openalrp 库来识别哪个需要图像字节或原始像素数据。我应该使用什么函数以及如何将sampleBuffer 转换为合适的输入类型std::vector&lt;char&gt;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);

【问题讨论】:

标签: c++ ios avfoundation std openalpr


【解决方案1】:

终于找到了答案:

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 2014-02-19
    • 1970-01-01
    相关资源
    最近更新 更多