我在Objective-C中有一个自定义的iOS iPhone相机应用程序,该应用程序在预览层中显示摄像机流,并在视场中有特定对象时捕获帧。图像识别是通过OpenCV实现的。
该应用程序运行良好,但我想让用户能够控制视频流的亮度/伽玛,并从亮度受控的流中捕获修改后的图像。
我知道如何创建滑块控件,但是我找不到如何控制来自摄像机的视频图像的亮度/伽玛并将其显示在预览层中。摄像机通过以下AVCapture会话进行设置。
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted)
{
if(granted){ // Access has been granted ..do something
NSLog(@"Camera Status Access Granted By User");
//Setup Video Camera and START
self.videoCamera = [[VideoCamera alloc] initWithParentView:self->_img];
self.videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionBack;
self.videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPreset640x480;
self.videoCamera.defaultAVCaptureVideoOrientation =
AVCaptureVideoOrientationPortrait;
self.videoCamera.defaultFPS = 60;
self.videoCamera.rotateVideo = NO;
self.videoCamera.grayscaleMode = NO;
self.videoCamera.delegate = self;
[self.videoCamera start];
}
如何控制实时视频流的亮度/伽玛并将其显示在预览层中,以便随后捕获修改后的图像。
这里是预览层的设置。
- (void)layoutPreviewLayer {
if (self.parentView != nil) {
// Center the video preview.
self.customPreviewLayer.position = CGPointMake(0.5 * self.parentView.frame.size.width,
0.5 * self.parentView.frame.size.height);
// Find the video's aspect ratio.
CGFloat videoAspectRatio = self.imageWidth / (CGFloat)self.imageHeight;
// Scale the video preview while maintaining its aspect ratio.
CGFloat boundsW;
CGFloat boundsH;
if (self.imageHeight > self.imageWidth) {
if (self.letterboxPreview) {
boundsH = self.parentView.frame.size.height;
boundsW = boundsH * videoAspectRatio;
} else {
boundsW = self.parentView.frame.size.width;
boundsH = boundsW / videoAspectRatio;
}
} else {
if (self.letterboxPreview) {
boundsW = self.parentView.frame.size.width;
boundsH = boundsW / videoAspectRatio;
} else {
boundsH = self.parentView.frame.size.height;
boundsW = boundsH * videoAspectRatio;
}
}
self.customPreviewLayer.bounds = CGRectMake(0.0, 0.0, boundsW, boundsH);
}
}
任何代码,帮助和指导都将非常感谢您进行此设置。
我找到了几种方法来实现这一目标,并认为我会将它们发布在这里以帮助需要它的其他人。
选项1。
使用滑块使用此代码修改OpenCV Mat的像素,使其更亮或更暗。
float gammaValue = 1.5;
//EXPOSURE CONTROL USING OPENCV
- (void) gammaCorrection: (cv::Mat &)mat;
{
float fGamma = gammaValue;
mat.convertTo(mat, -1, fGamma, 1 ); //Control the brightness by fGamma
NSLog(@"Gamma Value is: %f", fGamma);
}
- (IBAction)changeExposureTargetBias:(id)sender {
UISlider *control = sender;
NSLog(@"Target Bias %f", control.value );
gammaValue = control.value;
}
选项2。
用滑块控制AVCaptureDevice的曝光。我的设备的曝光偏差范围是-8.0到+8.0
float exposureBIAS = 0.0;
- (void)setExposure:(float)exposureBIAS
{
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if([device isExposureModeSupported:AVCaptureExposureModeCustom])
{
NSError *error = nil;
if ( [device lockForConfiguration:&error] ) {
[device setExposureTargetBias:exposureBIAS completionHandler:nil];
[device unlockForConfiguration];
}
else {
NSLog( @"Could not lock device for configuration: %@", error );
}
}
}
- (IBAction)changeExposureTargetBias:(id)sender {
UISlider *control = sender;
[self setExposure: control.value];
}