Ios iPhone摄像头拍摄的图像质量
我使用iPhone摄像头在iOS应用程序中拍摄图像。AVCaptureVideoDataOutputSampleBufferDelegate用于从iPhone摄像头获取图像。程序的一部分如下所示Ios iPhone摄像头拍摄的图像质量,ios,iphone,image,camera,avfoundation,Ios,Iphone,Image,Camera,Avfoundation,我使用iPhone摄像头在iOS应用程序中拍摄图像。AVCaptureVideoDataOutputSampleBufferDelegate用于从iPhone摄像头获取图像。程序的一部分如下所示 AVCaptureDevice *videoDevice = [AVCamViewController deviceWithMediaType:AVMediaTypeVideo preferringPosition:AVCaptureDevicePositionBack]; AVCaptureDe
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的哪些参数需要调整才能与camera app的质量相同或更接近?这两张图片附在后面。第二张照片是由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的转换代码非常标准,为什么会出现此问题?您可以使用
sessionPreset
控制捕获输入的质量
阅读更多信息我在UIImage到cvMat转换步骤中发现问题。我还在努力解决这个问题。有什么想法吗?UIImage到cvMat的转换步骤看起来不错,我认为问题可能在于将文件保存为jpeg。我找到了将图像保存到jpeg
NSArray*path=NSSearchPathForDirectoriesInDomains的代码(NSDocumentDirectory,NSUserDomainMask,YES);NSString*docs=[path objectAtIndex:0];NSString*path=[docs stringByAppendingFormat:@”/image1.jpg];NSData*imageData=[NSData dataWithData:uiimagejpegresentation(imageView.image,80)];n错误*writeError=nil;[imageData WriteFile:路径选项:NSDataWritingAtomic错误:&writeError]代码>