iPhone相机拍摄的图像质量

时间:2015-09-29 15:33:35

标签: ios iphone image camera avfoundation

我使用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相机的应用程序拍摄的。 谢谢 enter image description here

编辑: 我可以找出问题所在。 我在UIImage之前和之后打印两个图像到cvMat转换。 在UIImage格式中,图像质量非常好。转换后,质量变差,颜色变化等。 我之前和之后附上了两张图片。第一个是之前的。 enter image description here enter image description here 我使用以下代码进行转换。

- (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的转换代码是非常标准的,为什么会出现这个问题?

1 个答案:

答案 0 :(得分:0)

您可以使用sessionPreset来控制捕获输入的质量 阅读更多here