How to convert CMSampleBuffer to std::vector<char>?

半腔热情 提交于 2019-12-12 03:33:41

问题


I have image stream from my AVCaptureVideoDataOutputSampleBufferDelegate method:

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


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?

From alpr.h file:

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

回答1:


Finally, I've found the answer:

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


来源:https://stackoverflow.com/questions/41997900/how-to-convert-cmsamplebuffer-to-stdvectorchar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!