【发布时间】:2015-09-29 15:33:34
【问题描述】:
我使用 iPhone 相机在我的 iOS APP 中拍摄图像。 AVCaptureVideoDataOutputSampleBufferDelegate 用于从 iPhone 摄像头获取图像。部分程序如下所示。
AVCaptureDevice *videoDevice = [AVCamViewController deviceWithMediaType:AVMediaTypeVideo preferringPosition:AVCaptureDevicePositionBack];
AVCaptureDeviceInput *videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:&error];
if (error) { NSLog(@"%@", error); }
if ([session canAddInput:videoDeviceInput])
{
[session addInput:videoDeviceInput]; [self setVideoDeviceInput:videoDeviceInput];
dispatch_async(dispatch_get_main_queue(), ^{
[[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] setVideoOrientation:(AVCaptureVideoOrientation)[self interfaceOrientation]]; });
}
AVCaptureVideoDataOutputSampleBufferDelegate 拍摄的图像质量和 iPhone 的相机应用程序拍摄的图像质量有很大不同。在 AVCaptureVideoDataOutputSampleBufferDelegate 调整哪些参数以具有与相机应用程序相同或更接近的质量?附上两张图。第二张图片是由 iPhone 相机的应用程序拍摄的。 谢谢
编辑: 我可以查明问题出在哪里。 我在 UIImage 到 cvMat 转换之前和之后打印两个图像。 在 UIImage 格式中,图像质量相当不错。转换后,质量变差,颜色改变等。 我附上了前后两张图片。第一个是之前。 我使用以下代码进行转换。
- (cv::Mat)cvMatFromUIImage:(UIImage *)image
{
CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
CGFloat cols = image.size.width;
CGFloat rows = image.size.height;
cv::Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels (color channels + alpha)
CGContextRef contextRef = CGBitmapContextCreate(cvMat.data, // Pointer to data
cols, // Width of bitmap
rows, // Height of bitmap
8, // Bits per component
cvMat.step[0], // Bytes per row
colorSpace, // Colorspace
kCGImageAlphaNoneSkipLast |
kCGBitmapByteOrderDefault); // Bitmap info flags
CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
CGContextRelease(contextRef);
return cvMat;
}
将图像写入 jpg 文件应该不是问题。但这是保存图像的代码
- (void) saveImage:(std::string)name img:(Mat)gray{
//Print
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%3s.jpg", name.c_str()]];
const char* cPath = [filePath cStringUsingEncoding:NSMacOSRomanStringEncoding];
const cv::String newPaths = (const cv::String)cPath;
//Save as Bitmap to Documents-Directory
cv::imwrite(newPaths, gray);
}
从 UIImage 到 cvMat 的转换代码非常标准,为什么会出现这个问题?
【问题讨论】:
标签: ios iphone image camera avfoundation