Book Image

Instant OpenCV for iOS

4 (1)
Book Image

Instant OpenCV for iOS

4 (1)

Overview of this book

Computer vision on mobile devices is becoming more and more popular. Personal gadgets are now powerful enough to process high-resolution images, stitch panoramas, and detect and track objects. OpenCV, with its decent performance and wide range of functionality, can be an extremely useful tool in the hands of iOS developers. Instant OpenCV for iOS is a practical guide that walks you through every important step for building a computer vision application for the iOS platform. It will help you to port your OpenCV code, profile and optimize it, and wrap it into a GUI application. Each recipe is accompanied by a sample project or an example that helps you focus on a particular aspect of the technology. Instant OpenCV for iOS starts by creating a simple iOS application and linking OpenCV before moving on to processing images and videos in real-time. It covers the major ways to retrieve images, process them, and view or export results. Special attention is also given to performance issues, as they greatly affect the user experience.Several computer vision projects will be considered throughout the book. These include a couple of photo filters that help you to print a postcard or add a retro effect to your images. Another one is a demonstration of the facial feature detection algorithm. In several time-critical cases, the processing speed is measured and optimized using ARM NEON and the Accelerate framework. OpenCV for iOS gives you all the information you need to build a high-performance computer vision application for iOS devices.
Table of Contents (7 chapters)

Control advanced camera settings (Advanced)


In computer vision, we often should calibrate the camera of a device and find its intrinsic and extrinsic parameters (pinhole camera model). In order to do this, we should have the possibility to lock some camera settings (for example, focus) to calculate the camera parameters as accurately as possible. In this recipe, we'll consider some advanced settings provided by the CvVideoCamera class that can help you during the calibration process.

Getting ready

We will use the Recipe10_CapturingVideo project as a starting point, trying to add more control over the iOS camera. The source code can be found in the Recipe11_AdvancedCameraControl folder in the code bundle that accompanies this book. For this recipe you can't use Simulator, as it doesn't support camera.

How to do it...

The following are the required steps:

  1. Add four buttons to our GUI to control the focus, exposure, white balance, and camera rotation.

  2. Then implement actions for all buttons.

Let's implement the described steps:

  1. Similarly to previous recipe, we should implement basic functions to work with the video camera and add four more buttons to our UI:

    #import <opencv2/highgui/ios.h>
    
    @interface ViewController : UIViewController<CvVideoCameraDelegate>
    {
        CvVideoCamera* videoCamera;
        BOOL isCapturing;
        BOOL isFocusLocked, isExposureLocked, isBalanceLocked;
    }
    
    @property (nonatomic, strong) CvVideoCamera* videoCamera;
    @property (nonatomic, strong) IBOutlet UIImageView* imageView;
    @property (nonatomic, strong) IBOutlet UIToolbar* toolbar;
    @property (nonatomic, weak) IBOutlet
        UIBarButtonItem* startCaptureButton;
    @property (nonatomic, weak) IBOutlet
        UIBarButtonItem* stopCaptureButton;
    
    @property (nonatomic, weak) IBOutlet
        UIBarButtonItem* lockFocusButton;
    @property (nonatomic, weak) IBOutlet
        UIBarButtonItem* lockExposureButton;
    @property (nonatomic, weak) IBOutlet
        UIBarButtonItem* lockBalanceButton;
    @property (nonatomic, weak) IBOutlet
        UIBarButtonItem* rotationButton;
    
    -(IBAction)startCaptureButtonPressed:(id)sender;
    -(IBAction)stopCaptureButtonPressed:(id)sender;
    
    - (IBAction)actionLockFocus:(id)sender;
    - (IBAction)actionLockExposure:(id)sender;
    - (IBAction)actionLockBalance:(id)sender;
    
    - (IBAction)rotationButtonPressed:(id)sender;
    
    @end
  2. Then, the first three buttons will be used to control the focus, exposure, and white balance settings. These buttons will have two modes: locked and unlocked. In order to do it, we should implement the corresponding actions:

    - (IBAction)actionLockFocus:(id)sender
    {
        if (isFocusLocked)
        {
            [self.videoCamera unlockFocus];
            [lockFocusButton setTitle:@"Lock focus"];
            isFocusLocked = NO;
        }
        else
        {
            [self.videoCamera lockFocus];
            [lockFocusButton setTitle:@"Unlock focus"];
            isFocusLocked = YES;
        }
    }
    
    - (IBAction)actionLockExposure:(id)sender
    {
        if (isExposureLocked)
        {
            [self.videoCamera unlockExposure];
            [lockExposureButton setTitle:@"Lock exposure"];
            isExposureLocked = NO;
        }
        else
        {
            [self.videoCamera lockExposure];
            [lockExposureButton setTitle:@"Unlock exposure"];
            isExposureLocked = YES;
        }
    }
    
    - (IBAction)actionLockBalance:(id)sender
    {
        if (isBalanceLocked)
        {
            [self.videoCamera unlockBalance];
            [lockBalanceButton setTitle:@"Lock balance"];
            isBalanceLocked = NO;
        }
        else
        {
            [self.videoCamera lockBalance];
            [lockBalanceButton setTitle:@"Unlock balance"];
            isBalanceLocked = YES;
        }
    }
  3. The remaining fourth button will change the camera image orientation relative to the device orientation. It has two possible modes, and we just change the current mode in the action:

    - (IBAction)rotationButtonPressed:(id)sender
    {
        videoCamera.rotateVideo = !videoCamera.rotateVideo;
    } 

How it works...

First, let's investigate the focus changing on iOS devices. The iOS camera API supports three modes for camera focus:

  • AVCaptureFocusModeLocked: When enabled, the focus becomes fixed.

  • AVCaptureFocusModeAutoFocus: When enabled, the camera performs an autofocus operation and then returns to the locked mode.

  • AVCaptureFocusModeContinuousAutoFocus: When enabled, the camera continuously monitors focus and autofocuses as needed.

CvVideoCamera uses the AVCaptureFocusModeContinuousAutoFocus mode by default, so the focus may change with the scene. This can make the process of camera calibration much more difficult. The best way, in this case, is to set the focus to some special value (for example, infinity), but unfortunately, the iOS API doesn't contain all functions needed by computer vision specialists. There is no way to programmatically set the camera focus of an iOS device to infinity or any other predefined value. So we can only lock the current focus value. For that purpose, the CvVideoCamera class provides the lockFocus method. It changes the focus mode to the AVCaptureFocusModeLocked value. In order to unlock it again, you should use the unlockFocus function.

You can also control the exposure and white balance in the same way using lockExposure and lockBalance functions.

To control image rotation in camera, you can change the rotateVideo property. The default value of this variable is NO.

Note

In previous OpenCV versions the default value was YES.

In this mode, the camera image will not be rotated with the device rotation. If you change this value, the image will be rotated every time a device changes its orientation by 90 degrees.

Each newly added button in this project allows you to switch between the two modes. To indicate mode changing, we'll change the button's text. For that purpose, you can use the setTitle method.

There's more...

AVFoundation contains a lot of useful functions for advanced control of the iOS camera. For example, you can get exposure time for each frame. If you need such fine-grain control, you should work with AVFoundation directly.