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)

Getting started with iOS (Simple)


In this recipe, we'll provide all the necessary steps to set up your environment and run a "Hello World" application on a device. Development for the iOS platform may seem to be difficult in the beginning, because the list of prerequisites is somewhat large. We'll provide important links for those who are not familiar with Mac/iOS development, Objective-C, and Xcode. If you're already familiar with the iOS development, you can skip this recipe.

Getting ready

Apple has established a very rich and sound ecosystem for developers, where each component is tightly integrated with others. Once you're familiar with its basic rules and principles, you'll be able to switch between different types of projects easily. But it may take some time to familiarize yourself with the tools and frameworks. And the very first prerequisite for iOS development is a Mac OS X workstation or laptop, and you cannot use other operating systems. It is also highly recommended to use the latest available version of the OS and tools, as some new features are not backported to older versions. Currently, the latest version is the Mac OS X 10.8, also known as Mountain Lion.

Note

Unfortunately, you can't move forward without a Mac, but if you're new to this platform, it could become a very rewarding experience. Proficiency with multiple platforms is beneficial for your professional skills.

Secondly, to run some examples from this book, you will definitely need an iOS device, because iOS Simulator lacks camera support. You should also know that Simulator executes x86 native code, while real iOS devices are running on ARM. This difference will not allow you to understand the actual performance of your application, which is usually important. You can use a simple device such as iPod Touch, which is quite cheap and may be useful not only for development! But of course, we recommend you to find one of the latest iOS devices; currently these are iPhone 5 and iPad 4. The iOS version should be 6.0 or higher.

Note

Actually, you can use Simulator for experimenting with many examples in this book. It is also a good chance to test your application on a tablet if you only have a phone, or vise versa. But please note that samples that need camera will not work. Every time you need a real device, we will mention that in the beginning of the recipe.

When you have all the hardware, you'll need to install Xcode—the cornerstone of all Mac-centric development. You will need Version 4.6.3 or higher. We recommend installing it together with the command-line tools, so you'll have access to compiler and some other tools from the Terminal.

We're almost ready to start development, and you can actually proceed to the following How to do it... section if you're going to use Simulator. But if you're going to use a real device now or later, there is one more step. In order to run your application on a real device, you have to become a registered Apple developer (which is free), and you will also need to subscribe for the iOS Developer Program, which will cost you $99 per year. This may look too high for the opportunity to play with your little applications, but this is how Apple verifies that you're serious, and that you will do your best to create a decent app. Also, it gives you access to all beta software from Apple, related to iOS, which is very important from a developer's perspective. Registration procedure is described at https://developer.apple.com/register/index.action, and the page about the "iOS Developer Program" is at https://developer.apple.com/programs/ios/. Finally, you need to register your device according to this instruction found at http://bit.ly/3848_DeviceProvisioning.

Source code for this recipe is available in the Recipe01_HelloWorld folder in the code bundle that accompanies this book.

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

How to do it...

So, now we're ready to create our first "Hello World" application, and it's going to be in Objective-C. The following are the steps required to achieve our goal:

  1. Connect your device to the host computer.

  2. Open Xcode and create a new project.

  3. Modify the code, so that the "Hello World" text will appear.

  4. Run the application.

Let's implement the described steps:

  1. We'll start by connecting your device to the computer (you can skip this step in case you're using Simulator). This is done using the USB cable, and it not only allows you to charge your device, but also provides some control over it. There are some applications available that allow you to copy files from/to a connected device (for example, iFunBox), but we'll not need it, because we'll be using Xcode to communicate with the device.

  2. Next, we'll launch Xcode. When started, Xcode will show your menu with several options, and you should choose the Create a new Xcode project option. Then you need to choose Single View Application template by navigating to iOS | Application. In the dialog box that appears, you have to specify values for Product Name, Organization Name (you can use your name), and Company Identifier. The following screenshot shows the window with options for creating a new project:

  3. We also recommend you uncheck the Include Unit Tests checkbox, because we don't need them for now. Then click on Next, choose a folder for you project, click on Create, and you're done!

  4. Now it's time to add some handcrafted code to the auto-generated project. You can see that the Xcode window is divided into several "areas". The following screenshot is taken from the official Xcode User Guide, explaining the layout (http://bit.ly/3848_Xcode):

  5. Open the ViewController.m file, which can be found in the Project Navigator Area on the left-hand side. We're going to add a simple logging to the console, and an alert window. In order to do that, please edit the viewDidLoad method, so it looks like the following code:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        // Console output
        NSLog(@"Hello, World!");
    
        // Alert window
        UIAlertView *alert = [UIAlertView alloc];
        alert = [alert initWithTitle:@"Hey there!"
                             message:@"Welcome to OpenCV on iOS \
                                       development community"
                            delegate:nil
                   cancelButtonTitle:@"Continue"
                   otherButtonTitles:nil];
        
        [alert show];
    }
  6. Then open the Debug Area in the Xcode navigating to View | Debug Area | Show Debug Area. Now click on the Run button located in the top-left corner of the Xcode window, and check that you can see both the alert window on the device's screen and the log message in the Debug Area. That's it; you have your first application running!

How it works...

The NSLog function, which we added first, is intended for logging simple text messages, similar to the printf function in the C language. You can also see that our string was preceded with the @ character, which is used for implicit conversion to the NSString object. Just like printf, NSLog allows you to print values of multiple variables with proper formatting, so this function is quite helpful during debugging and profiling of your application.

Note

While the NSLog function is useful for logging, you should not keep these logs in the production code because like any other IO procedure, it has its cost, which may negatively affect the performance. So, you should use conditional compilation or something else to remove all debug logs from the release code.

The next code block shows how one can create a simple message window with some notification. We are not going to use UIAlertView in later recipes, but this is a good occasion to get familiar with creating objects and calling their methods in Objective-C. The first line shows how a new object may be created. Here, the alloc method is called that was inherited by UIAlertView from its parent NSObject class. Next, we're calling the initWithTitle method, passing necessary arguments to it. This method returns a newly initialized alert window. Finally, we call the show method to display the window with our message.

Please note that we've reformatted the code for readability, and we've even created one surplus temporary object. It was possible to call the initWithTitle method of a newly allocated object, so in most cases, you will likely meet the initialization code as shown in the following code snippet.

UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Hey there!" message:@"Welcome to OpenCV on iOS development community" delegate:nil cancelButtonTitle:@"Continue" otherButtonTitles:nil];

There's more...

That's all for this recipe, but if you feel that you need some more introductory information on iOS development in general, we'll provide you with some pointers. Later, we will focus mostly on OpenCV-related aspects of programming, so if you want to better understand Apple's tools and frameworks, you should spend some time studying other resources.

Xcode

We encourage you to "sharpen your saw" and read more about Xcode. Refer to the official documentation at https://developer.apple.com/xcode/. For example, you can create several Simulators to test your application on different types of devices. Navigate to Xcode | Preferences, and then go to the Downloads | Components to see the available options. Many useful tips on Xcode and sample code examples are available in the iOS Developer Library at http://developer.apple.com/library/ios/navigation/.

Objective-C

One of the important things you should know about Objective-C is that it is a strict superset of C, and you can mix it with C. Because OpenCV is written in C++, we have to use Objective-C++, which allows to use a combination of C++ and Objective-C syntax and also allows you to reuse your C++ code or libraries (as we do with OpenCV). Nonetheless, Objective-C is the main language for iOS, so you should get familiar with it in order to use the available libraries and frameworks effectively.