我正在修改此处找到的代码。在代码中,我们使用 AVCaptureSession 从手机摄像头捕获视频,并使用 CIDetector 检测图像馈送中的矩形。提要有一个 640x842 的图像(iphone5 纵向)。然后我们在图像上进行叠加,以便用户可以看到检测到的矩形(实际上它大部分时间都是梯形)。
当用户按下 UI 上的按钮时,我们会从视频中捕获图像并在这个较大的图像 (3264x2448) 上重新运行矩形检测,您可以看到它是横向的。然后我们对检测到的矩形进行透视变换并裁剪图像。
这工作得很好,但我遇到的问题是 5 个中的 1 个捕获较大图像上检测到的矩形与从较小图像中检测到(并呈现给用户)的矩形不同。尽管我仅在检测到手机(相对)静止时才捕获,但最终图像并不代表用户期望的矩形。
为了解决这个问题,我的想法是使用最初捕获的矩形的坐标并将它们转换为捕获的静止图像上的矩形。这就是我挣扎的地方。
我用检测到的矩形尝试了这个:
CGFloat radians = -90 * (M_PI/180);
CGAffineTransform rotation = CGAffineTransformMakeRotation(radians);
CGRect rect = CGRectMake(detectedRect.bounds.origin.x, detectedRect.bounds.origin.y, detectedRect.bounds.size.width, detectedRect.bounds.size.height);
CGRect rotatedRect = CGRectApplyAffineTransform(rect, rotation);
所以给定一个检测到的矩形:
左上:88.213425、632.31329 右上:545.59302、632.15546 右下:575.57819、369.22321 左下:49.973862、369.40466
我现在有这个旋转的矩形:
原点 = (x = 369.223206, y = -575.578186) 尺寸 = (宽度 = 263.090088, 高度 = 525.604309)
如何将较小肖像图像中的旋转矩形坐标转换为 3264x2448 图像的坐标?
编辑 Duh .. 阅读我自己的方法后,我意识到用梯形创建一个矩形并不能解决我的问题!
支持代码检测矩形等...
// In this method we detect a rect from the video feed and overlay
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
CVPixelBufferRef pixelBuffer = (CVPixelBufferRef)CMSampleBufferGetImageBuffer(sampleBuffer);
CIImage *image = [CIImage imageWithCVPixelBuffer:pixelBuffer];
// image is 640x852 on iphone5
NSArray *rects = [[CIDetector detectorOfType:CIDetectorTypeRectangle context:nil options:@{CIDetectorAccuracy : CIDetectorAccuracyHigh}] featuresInImage:image];
CIRectangleFeature *detectedRect = rects[0];
// draw overlay on image code....
}
这是如何获得静止图像的总结版本:
// code block to handle output from AVCaptureStillImageOutput
[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
{
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
CIImage *enhancedImage = [[CIImage alloc] initWithData:imageData options:@{kCIImageColorSpace:[NSNull null]}];
imageData = nil;
CIRectangleFeature *rectangleFeature = [self getDetectedRect:[[self highAccuracyRectangleDetector] featuresInImage:enhancedImage]];
if (rectangleFeature) {
enhancedImage = [self correctPerspectiveForImage:enhancedImage withTopLeft:rectangleFeature.topLeft andTopRight:rectangleFeature.topRight andBottomRight:rectangleFeature.bottomRight andBottomLeft:rectangleFeature.bottomLeft];
}
}
谢谢你。