gpt4 book ai didi

image-processing - 无法让 AVFoundation 使用 AVCaptureSessionPresetPhoto 分辨率

转载 作者:行者123 更新时间:2023-12-02 16:57:15 25 4
gpt4 key购买 nike

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

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

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

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

最佳答案

SaveImage 内,CGImageCreate 的第 5 个参数是 bytesPerRow你不应该通过image->width因为在内存对齐的情况下,每行的字节数可能会有所不同。 AVCaptureSessionPresetPhoto 就是这种情况。在哪里 width = 852 (带 iPhone 4 摄像头),而第一个平面 (Y) 的每行字节数为 864因为它是 16 字节对齐的。

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

关于image-processing - 无法让 AVFoundation 使用 AVCaptureSessionPresetPhoto 分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12713844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com