我试图在AVCapture预览图层中显示每个帧,我想基本上使用openCV来检测特定形状(更具体地说是圆圈)并在屏幕上将它们绘制给用户。我似乎无法获得更新屏幕预览图层的正确方法,相反,我只能从队列中获取输出帧,这对预览没有任何作用。
目前使用以下方法从输出缓冲区中提取帧:
// Create a UIImage from sample buffer data
- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer
{
// Get a CMSampleBuffer's Core Video image buffer for the media data
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
// Lock the base address of the pixel buffer
CVPixelBufferLockBaseAddress(imageBuffer, 0);
// Get the number of bytes per row for the pixel buffer
void *baseAddress = CVPixelBufferGetBaseAddress(imageBuffer);
// Get the number of bytes per row for the pixel buffer
size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
// Get the pixel buffer width and height
size_t width = CVPixelBufferGetWidth(imageBuffer);
size_t height = CVPixelBufferGetHeight(imageBuffer);
// Create a device-dependent RGB color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// Create a bitmap graphics context with the sample buffer data
CGContextRef context = CGBitmapContextCreate(baseAddress, width, height, 8,
bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
// Create a Quartz image from the pixel data in the bitmap graphics context
CGImageRef quartzImage = CGBitmapContextCreateImage(context);
// Unlock the pixel buffer
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
// Free up the context and color space
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
// Create an image object from the Quartz image
UIImage *image = [UIImage imageWithCGImage:quartzImage];
// Release the Quartz image
CGImageRelease(quartzImage);
return image;
}
有没有办法让每个发送的帧都显示在预览图层上?或使用AVCapture会话而不是opencv相机实现霍夫检测的任何其他方法?任何帮助都会非常感激!
答案 0 :(得分:0)
要做到这一点,最好的方法是从方法中取出每一帧:
- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer;
在此方法中提取UIImage并使用包含OpenCV的包装器在每个帧中查找houghcircles。霍夫圆圈将生成圆圈所在位置的坐标(如果有)。然后,我可以使用圆圈的位置在视图上绘制CAShapeLayer以显示圆圈。我使用以下内容在视图中绘制圆圈:
circleLayer = [CAShapeLayer layer];
[circleLayer setPath:[[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, radius, radius)] CGPath]];
[self.previewLayer addSublayer:circleLayer];
[circleLayer setStrokeColor:[[UIColor redColor] CGColor]];
[circleLayer setFillColor:[[UIColor clearColor] CGColor]];
我们可以根据openCV算法检测到houghcircle的位置简单地移动圆圈,如下所示:
[circleLayer setPosition:CGPointMake(x,y)];
可能需要根据捕获的图像的方向和缩放因子来调整CGPoint的参数。这个解决方案有效!