简体   繁体   English

iPhone相机拍摄的图像质量

[英]Image quality taken by iPhone camera

I use iPhone camera to capture images in my iOS APP. 我使用iPhone相机在iOS APP中捕获图像。 AVCaptureVideoDataOutputSampleBufferDelegate is used to get images from iPhone camera. AVCaptureVideoDataOutputSampleBufferDelegate用于从iPhone相机获取图像。 A part of the program is shown below. 该程序的一部分如下所示。

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

The quality of the image taken by AVCaptureVideoDataOutputSampleBufferDelegate and that of the image taken iPhone's camera app are much different. AVCaptureVideoDataOutputSampleBufferDelegate拍摄的图像质量与iPhone的相机应用拍摄的图像质量有很大不同。 What are the parameters to adjust at AVCaptureVideoDataOutputSampleBufferDelegate to have the same or closer quality as camera app? 在AVCaptureVideoDataOutputSampleBufferDelegate上要调整哪些参数以使其具有与相机应用相同或接近的质量? The two images are attached. 附有两个图像。 The second image is taken by iPhone camera's app. 第二张图像是由iPhone相机的应用程序拍摄的。 Thanks 谢谢 在此处输入图片说明

EDIT: I can pinpoint where is the problem. 编辑:我可以查明问题出在哪里。 I print two images before and after UIImage to cvMat conversion. 我在UIImage到cvMat转换之前和之后打印两个图像。 In UIImage format, the image quality is quite good. 在UIImage格式下,图像质量很好。 After conversion, the quality becomes bad, the color is changed etc.. I attached two images before and after. 转换后,质量变差,颜色改变等。我在前后附加了两个图像。 The first one is before. 第一个是之前。 在此处输入图片说明 在此处输入图片说明 I used the following code to convert. 我使用以下代码进行转换。

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

Writing the image to jpg file shouldn't be a problem. 将图像写入jpg文件应该没有问题。 But this is the code to save image 但这是保存图像的代码

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

Conversion code from UIImage to cvMat is quite standard and why it has this problem? 从UIImage到cvMat的转换代码非常标准,为什么会有这个问题?

您可以使用sessionPreset控制捕获输入信号的质量阅读更多在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM