Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
iOS Application Development with OpenCV 3
iOS Application Development with OpenCV 3

iOS Application Development with OpenCV 3: Create four mobile apps and explore the world through photography and computer vision

eBook
€15.99 €23.99
Paperback
€29.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

iOS Application Development with OpenCV 3

Chapter 2. Capturing, Storing, and Sharing Photos

For many people, photography is a collaborative and social activity. This is not just a new perspective. Even the pioneers of photography spent long hours sharing their creative processes with family, students, models, and curious members of the public, who hoped to be immortalized in this new art form.

Today's technology enables people to collaborate and socialize in a short time and at a great distance. As we discussed in the previous chapter, the iPhone offers a set of unobtrusive and intuitive tools to any user who wants to capture, edit, and share photos from this small screen. For better or worse, a few taps of the user's fingers have replaced all the manual work of the studio, darkroom, print shop, and delivery service.

This chapter addresses the technical challenges of developing a mobile workflow for the social photographer. We will accomplish the following tasks:

  • Configure the camera, including focus and exposure...

Planning a photo sharing application

When it opens, LightWork will present a vintage photograph and toolbar containing a few items. The following screenshot shows it all:

Planning a photo sharing application

This bucolic image is an early color photograph, shot in 1902 by Adolf Miethe, a German photographer, professor, and inventor. The photo almost looks like a painting due to its coarse grain and pastel colors. However, it is a true example of a color photographic process. Miethe captured a scene on three photographic plates behind three different filters: red, green, and blue. Then, to recreate a multicolored image, he superimposed the three images using a projection process with different colors of light or a printing process with different dyes. Similar techniques are still in use today in our digital sensors, monitors, and printers.

LightWork will also be capable of displaying a live preview from a camera. However, for our purposes, a static (still) image is also a useful preview. A static image (unlike a camera preview...

Configuring the project

Create an Xcode project named LightWork using the Single View Application template. Configure the project according to the instructions in the Configuring the project section in Chapter 1, Setting Up Software and Hardware. Next, we will take a few additional configuration steps because LightWork depends on more frameworks and requires a camera.

Adding frameworks

Besides its dependency on opencv2.framework, LightWork also depends on the following standard frameworks from the iOS SDK:

  • Accelerate.framework: This is optional but recommended because it enables OpenCV to use advanced optimizations
  • AssetsLibrary.framework
  • AVFoundation.framework
  • CoreGraphics.framework
  • CoreMedia.framework
  • CoreVideo.framework
  • Photos.framework
  • QuartzCore.framework
  • Social.framework
  • UIKit.framework

Add these frameworks to the Build Phases | Link Binary With Libraries section of the project settings.

Specifying the camera requirement

Currently, there is a camera in all devices that support iOS 9. However, perhaps...

Defining and laying out the view controller

As discussed in the previous chapter, we may declare GUI elements in a view controller's source code and lay them out in a storyboard. Thus, to begin, let's open ViewController.m and define the private interface of our ViewController class. The interface depends on headers from the iOS SDK's Photos and Social frameworks as well as the OpenCV framework. Moreover, it depends on headers that define the public interfaces of our own classes, ViewController and VideoCamera. The latter class will handle many aspects of camera input and video display and we will write it later in the Controlling the camera section. Let's import these dependencies by adding the following code at the start of ViewController.m:

#import <Photos/Photos.h>
#import <Social/Social.h>

#import <opencv2/core.hpp>
#import <opencv2/imgcodecs.hpp>
#import <opencv2/imgcodecs/ios.h>
#import <opencv2/imgproc.hpp>

#import "ViewController...

Controlling the camera

The iOS SDK and OpenCV provide several programming interfaces for camera control. Within the iOS SDK, AVFoundation is the general-purpose framework for all recording and playback of audiovisual (AV) content. AVFoundation provides complete access to the iOS camera's parameters, including the image format, focus, exposure, flash, frame rate, and digital zoom (crop factor). However, AVFoundation does not solve any GUI problems. The application developer may create a custom camera GUI, use a higher-level framework that provides a GUI, or automate the camera so that it operates without GUI input. AVFoundation is sufficiently flexible to support any of these designs, but this flexibility comes at a price as AVFoundation is complex.

Note

The official AVFoundation Programming Guide is located at https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/AVFoundationPG.

The iOS SDK implements a standard camera GUI in the UIImagePickerController class,...

Working with various color formats

As we have seen, OpenCV and the iOS SDK work with various formats for color and grayscale images and sometimes we need to convert between formats. Let's step back from the code for a few moments to discuss the differences between formats and the problems that can arise if we do not perform the correct conversions.

RGB, BGR, RGBA, and BGRA

You probably learned the 24-bit RGB color format long ago, the first time you picked a custom color in a paint program or word processor. A pixel's color is represented by a sequence of three values, each with a range of 0 to 255 (that is, 8 bits or 1 byte). The first value is the color's red component or channel, followed by green, and lastly blue. For example, the color of an amber traffic light is (255, 126, 0), a mixture of lots of red and some green, but no blue. A series of pixel data makes an image.

The 24-bit BGR format simply reverses the channel order. For example, the color of the amber traffic...

Planning a photo sharing application


When it opens, LightWork will present a vintage photograph and toolbar containing a few items. The following screenshot shows it all:

This bucolic image is an early color photograph, shot in 1902 by Adolf Miethe, a German photographer, professor, and inventor. The photo almost looks like a painting due to its coarse grain and pastel colors. However, it is a true example of a color photographic process. Miethe captured a scene on three photographic plates behind three different filters: red, green, and blue. Then, to recreate a multicolored image, he superimposed the three images using a projection process with different colors of light or a printing process with different dyes. Similar techniques are still in use today in our digital sensors, monitors, and printers.

LightWork will also be capable of displaying a live preview from a camera. However, for our purposes, a static (still) image is also a useful preview. A static image (unlike a camera preview...

Configuring the project


Create an Xcode project named LightWork using the Single View Application template. Configure the project according to the instructions in the Configuring the project section in Chapter 1, Setting Up Software and Hardware. Next, we will take a few additional configuration steps because LightWork depends on more frameworks and requires a camera.

Adding frameworks

Besides its dependency on opencv2.framework, LightWork also depends on the following standard frameworks from the iOS SDK:

  • Accelerate.framework: This is optional but recommended because it enables OpenCV to use advanced optimizations

  • AssetsLibrary.framework

  • AVFoundation.framework

  • CoreGraphics.framework

  • CoreMedia.framework

  • CoreVideo.framework

  • Photos.framework

  • QuartzCore.framework

  • Social.framework

  • UIKit.framework

Add these frameworks to the Build Phases | Link Binary With Libraries section of the project settings.

Specifying the camera requirement

Currently, there is a camera in all devices that support...

Defining and laying out the view controller


As discussed in the previous chapter, we may declare GUI elements in a view controller's source code and lay them out in a storyboard. Thus, to begin, let's open ViewController.m and define the private interface of our ViewController class. The interface depends on headers from the iOS SDK's Photos and Social frameworks as well as the OpenCV framework. Moreover, it depends on headers that define the public interfaces of our own classes, ViewController and VideoCamera. The latter class will handle many aspects of camera input and video display and we will write it later in the Controlling the camera section. Let's import these dependencies by adding the following code at the start of ViewController.m:

#import <Photos/Photos.h>
#import <Social/Social.h>

#import <opencv2/core.hpp>
#import <opencv2/imgcodecs.hpp>
#import <opencv2/imgcodecs/ios.h>
#import <opencv2/imgproc.hpp>

#import "ViewController.h"
#import ...

Controlling the camera


The iOS SDK and OpenCV provide several programming interfaces for camera control. Within the iOS SDK, AVFoundation is the general-purpose framework for all recording and playback of audiovisual (AV) content. AVFoundation provides complete access to the iOS camera's parameters, including the image format, focus, exposure, flash, frame rate, and digital zoom (crop factor). However, AVFoundation does not solve any GUI problems. The application developer may create a custom camera GUI, use a higher-level framework that provides a GUI, or automate the camera so that it operates without GUI input. AVFoundation is sufficiently flexible to support any of these designs, but this flexibility comes at a price as AVFoundation is complex.

Note

The official AVFoundation Programming Guide is located at https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/AVFoundationPG.

The iOS SDK implements a standard camera GUI in the UIImagePickerController class, which...

Working with various color formats


As we have seen, OpenCV and the iOS SDK work with various formats for color and grayscale images and sometimes we need to convert between formats. Let's step back from the code for a few moments to discuss the differences between formats and the problems that can arise if we do not perform the correct conversions.

RGB, BGR, RGBA, and BGRA

You probably learned the 24-bit RGB color format long ago, the first time you picked a custom color in a paint program or word processor. A pixel's color is represented by a sequence of three values, each with a range of 0 to 255 (that is, 8 bits or 1 byte). The first value is the color's red component or channel, followed by green, and lastly blue. For example, the color of an amber traffic light is (255, 126, 0), a mixture of lots of red and some green, but no blue. A series of pixel data makes an image.

The 24-bit BGR format simply reverses the channel order. For example, the color of the amber traffic light is (0, 126...

Starting and stopping the busy mode


Remember that we want to show an activity indicator and disable all the toolbar items while LightWork is busy saving or sharing a photo. Conversely, when LightWork is no longer busy with the photo, we want to hide the activity indicator and re-enable the toolbar items. As these actions affect the GUI, we must ensure that they run on the app's main thread.

Note

If our code is running on a background thread, nothing will happen when we try to show or hide the activity indicator.

To run code on a specific thread, we can make a post to the thread's event queue. The iOS SDK provides a C function, dispatch_async, which takes a target queue and code block as arguments. Another C function, dispatch_get_main_queue(), enables us to get the main thread's event queue. Let's use these functions in the following helper method, which starts the busy mode:

- (void)startBusyMode {
  dispatch_async(dispatch_get_main_queue(), ^{
    [self.activityIndicatorView startAnimating...
Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Efficiently harness iOS and OpenCV to capture and process high-quality images at high speed
  • Develop photographic apps and augmented reality apps quickly and easily
  • Detect, recognize, and morph faces and objects

Description

iOS Application Development with OpenCV 3 enables you to turn your smartphone camera into an advanced tool for photography and computer vision. Using the highly optimized OpenCV library, you will process high-resolution images in real time. You will locate and classify objects, and create models of their geometry. As you develop photo and augmented reality apps, you will gain a general understanding of iOS frameworks and developer tools, plus a deeper understanding of the camera and image APIs. After completing the book's four projects, you will be a well-rounded iOS developer with valuable experience in OpenCV.

Who is this book for?

If you want to do computational photography and computer vision on Apple’s mobile devices, then this book is for you. No previous experience with app development or OpenCV is required. However, basic knowledge of C++ or Objective-C is recommended.

What you will learn

  • Use Xcode and Interface Builder to develop iOS apps
  • Obtain OpenCV s standard modules and build extra modules from source
  • Control all the parameters of the iOS device s camera
  • Capture, save, and share photos and videos
  • Analyze colors, shapes, and textures in ordinary and specialized photographs
  • Blend and compare images to create special photographic effects and augmented reality tools
  • Detect faces and morph facial features
  • Classify coins and other objects

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jun 30, 2016
Length: 228 pages
Edition : 1st
Language : English
ISBN-13 : 9781785289491
Vendor :
Apple
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Jun 30, 2016
Length: 228 pages
Edition : 1st
Language : English
ISBN-13 : 9781785289491
Vendor :
Apple
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 113.97
OpenCV 3 Computer Vision Application Programming Cookbook
€41.99
OpenCV By Example
€41.99
iOS Application Development with OpenCV 3
€29.99
Total 113.97 Stars icon

Table of Contents

6 Chapters
1. Setting Up Software and Hardware Chevron down icon Chevron up icon
2. Capturing, Storing, and Sharing Photos Chevron down icon Chevron up icon
3. Blending Images Chevron down icon Chevron up icon
4. Detecting and Merging Faces of Mammals Chevron down icon Chevron up icon
5. Classifying Coins and Commodities Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
(5 Ratings)
5 star 40%
4 star 40%
3 star 0%
2 star 20%
1 star 0%
Sai Prasanth Dec 01, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
great book. I had some questions about the code and Joseph was kind enough to answer them personally. Kudos! Good for beginners of ObjC
Amazon Verified review Amazon
Amazon Customer Jul 18, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is a very handy book for not only those that are getting started with iOS but also those who want to specifically focus on OpenCV with iOS. The code is well written and documented and explains concepts in a nice and crisp manner.
Amazon Verified review Amazon
Wayne Duquaine Jan 20, 2024
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Feefo Verified review Feefo
Andre Frank Jan 04, 2017
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
I would highly recommend this book for all beginners who wants to use OpenCV3 for IOS development. The writer explains the OpenCV classes/methods very good. Step by step you will learn what exactly OpenCV is and how you can use it for IOS development. There are not so much tutorials but this doesn't matter. The level of details and complexity gains with every tutorial.At the end you will have a solid knowledge to write own code with OpenCV.You also learn how to install OpenCV in a correct way . It can be frustrating to do so without introduction...The only negative aspect for me was the format of the eBook. I struggled with line breaks of the printed source code. It looks a bit fragmented.
Amazon Verified review Amazon
Dillon Jun 30, 2017
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
I tried the first project, but could not get it to work. I download the necessary components and imported the OpenCV library, but it still would not work, thus rendering the rest of the book useless.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.