0

我似乎无法让像素对齐与 AVCaptureSessionPresetPhoto 分辨率中的 AVFoundation 一起使用。像素对齐适用于较低的分辨率,如 AVCaptureSessionPreset1280x720 (AVCaptureSessionPreset1280x720_Picture)。AVCaptureSessionPreset1280x720_pictureAVCaptureSessionPresetPhoto_picture

具体来说,当我取消注释这些行时: if ([captureSession canSetSessionPreset:AVCaptureSessionPresetPhoto]) { [captureSession setSessionPreset:AVCaptureSessionPresetPhoto];
} else { NSLog(@"无法将分辨率设置为 AVCaptureSessionPresetPhoto"); 我得到一个错过的对齐图像,如下面的第二张图片所示。非常感谢任何意见/建议。

这是我设置 1) 捕获会话、2) 委托回调和 3) 保存一个蒸汽图像以验证像素对齐的代码。
1.捕获会话设置

    - (void)InitCaptureSession {
captureSession = [[AVCaptureSession alloc] init];
if ([captureSession canSetSessionPreset:AVCaptureSessionPreset1280x720]) {
    [captureSession setSessionPreset:AVCaptureSessionPreset1280x720];        
} else {
    NSLog(@"Unable to set resolution to AVCaptureSessionPreset1280x720");
}

//    if ([captureSession canSetSessionPreset:AVCaptureSessionPresetPhoto]) {
//        [captureSession setSessionPreset:AVCaptureSessionPresetPhoto];        
//    } else {
//        NSLog(@"Unable to set resolution to AVCaptureSessionPresetPhoto");
//    }

captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
videoInput = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:nil];

AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init];
captureOutput.alwaysDiscardsLateVideoFrames = YES; 

dispatch_queue_t queue;
queue = dispatch_queue_create("cameraQueue", NULL);
[captureOutput setSampleBufferDelegate:self queue:queue];
dispatch_release(queue);
NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey; 
    NSNumber* value = [NSNumber     numberWithUnsignedInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange];
NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];     
[captureOutput setVideoSettings:videoSettings]; 

[captureSession addInput:videoInput];
[captureSession addOutput:captureOutput];
    [captureOutput release];

    previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

    CALayer *rootLayer = [previewView layer];// self.view.layer; //

[rootLayer setMasksToBounds:YES];
[previewLayer setFrame:[rootLayer bounds]];
[rootLayer addSublayer:previewLayer];
[captureSession startRunning];  
}


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

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
static int processedImage = 0;
processedImage++;
if (processedImage==100) {
    [self SaveImage:sampleBuffer]; 
}

[pool drain];
} 

// Create a UIImage CMSampleBufferRef and save for verifying pixel alignment
- (void) SaveImage:(CMSampleBufferRef) sampleBuffer 
{
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 
CVPixelBufferLockBaseAddress(imageBuffer, 0); 
CvSize imageSize;
imageSize.width = CVPixelBufferGetWidth(imageBuffer); ;
imageSize.height = CVPixelBufferGetHeight(imageBuffer); 
IplImage *image = cvCreateImage(imageSize, IPL_DEPTH_8U, 1);     
void *y_channel = CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0); 
char *tempPointer = image->imageData;
memcpy(tempPointer, y_channel, image->imageSize);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
NSData *data = [NSData dataWithBytes:image->imageData length:image->imageSize];
CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);
CGImageRef imageRef = CGImageCreate(image->width, image->height,
                                    8, 8, image->width,
                                    colorSpace, kCGImageAlphaNone|kCGBitmapByteOrderDefault,
                                    provider, NULL, false, kCGRenderingIntentDefault);
UIImage *Saveimage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpace);
UIImageWriteToSavedPhotosAlbum(Saveimage, nil, nil, nil);
CVPixelBufferUnlockBaseAddress(imageBuffer,0);
}
4

1 回答 1

3

在 内, isSaveImage的第 5 个参数并且您不应该传递,因为在内存对齐的情况下每行的字节数可能会有所不同。where (带有 iPhone 4 摄像头)就是这种情况,而第一个平面 (Y) 的每行字节数是 16 字节对齐的。CGImageCreatebytesPerRowimage->widthAVCaptureSessionPresetPhotowidth = 852864

1/ 你应该得到每行的字节数如下:

size_t bpr = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer, 0);

2 /然后在将像素复制到您的时注意考虑每行的字节数IplImage

char *y_channel = (char *) CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0);

// row by row copy
for (int i = 0; i < image->height; i++)
  memcpy(tempPointer + i*image->widthStep, y_channel + i*bpr, image->width);

您可以保持[NSData dataWithBytes:image->imageData length:image->imageSize];原样,因为image->imageSize考虑到对齐 ( imageSize = height*widthStep)。

3/ 最后将IplImage宽度步长作为CGImageCreate第 5 个参数传递:

CGImageCreate(image->width, image->height, 8, 8, image->widthStep, ...);
于 2012-10-04T10:08:16.883 回答