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)

Saving video from camera (Simple)


In the earlier recipes, we saved images, after some filtering, to Gallery as user's photos. In this recipe, we will investigate how to create a video file from camera images and save it to Gallery.

Getting ready

We will use the Recipe12_CapturingVideo project as a starting point, trying to add the possibility to save processed camera images as video. The source code can be found in the Recipe13_SavingVideo 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...

One of the many features supported by the latest iOS devices is 1080p HD video recording. It is incredible, because it is the largest resolution for the majority of modern TVs. Also, the modern iOS devices support hardware encoding with the H.264 codec and QuickTime container (.mov).

The following are the required steps:

  1. Enable video recording by changing recordVideo property.

  2. Add code for copying resulting video to Gallery when capturing is stopped.

Let's implement the described steps:

  1. In order to enable video recording, we'll change the default value of the recordVideo property in the viewDidLoad method:

    videoCamera.recordVideo = YES;
  2. After that, we will add additional code to the Stop capture button's action:

    -(IBAction)stopCaptureButtonPressed:(id)sender
    {
        [videoCamera stop];
        
        NSString* relativePath = [videoCamera.videoFileURL relativePath];
        UISaveVideoAtPathToSavedPhotosAlbum(relativePath, nil, NULL, NULL);
        
        //Alert window
        UIAlertView *alert = [UIAlertView alloc];
        alert = [alert initWithTitle:@"Status"
                             message:@"Saved to the Gallery!"
                            delegate:nil
                   cancelButtonTitle:@"Continue"
                   otherButtonTitles:nil];
        [alert show];
        
        isCapturing = FALSE;
    } 

How it works...

At a high level, the main method of the CvVideoCamera class (that captures frames and processes them) looks like the following:

while (flag)
{
    if (self.delegate)
    {
        //Get current frame
        
        //Call process function
        
        if (self.recordVideo == YES) {
            //Add the image to video file
        }
    }
}

In other words, the processImage method will be called for each frame after calling the start method. And each processed frame is saved to the resulting video file if the value of the recordVideo variable is YES.

When a user clicks on the Stop capture button, the stopCaptureButtonPressed method is called. In this function, we have to call the stop method initially. After that, the resulting video file becomes available in tmp folder of the current application. You can copy it from device with some software, such as iFunBox. You can get the path to this file through the videoCamera.videoFileURL property. In order to copy it to Gallery, we should call the UISaveVideoAtPathToSavedPhotosAlbum function using the path to the generated file. After that, we can show an alert window with a notification message about video saving as done in the Capturing a video from camera (Simple) recipe.