Book Image

iOS Application Development with OpenCV 3

By : Joseph Howse
4 (1)
Book Image

iOS Application Development with OpenCV 3

4 (1)
By: Joseph Howse

Overview of this book

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.
Table of Contents (13 chapters)
iOS Application Development with OpenCV 3
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Preface
Index

Understanding the camera and setting up photographic accessories


You have probably taken photos with an iOS device before. Perhaps you are even familiar with a variety of apps for image capture and image processing. Photography with an iPhone is certainly a popular pass time, and some people even define it as a distinct photographic movement called iPhoneography.

Tip

If you are entirely new to iPhone photography, take some time now to try Apple's Camera and Photo apps, as well as some third-party photography apps.

iPhone users are not alone in espousing a brand-centric view of photography. For example, another movement called Lomography derives its inspiration from a film camera called the LOMO LC-A, released by the Leningrad Optical Mechanical Associtaion (LOMO) in 1984. LOMO makes precise optical instruments including microscopes, telescopes, night-vision devices, and medical imaging systems, but ironically the company entered the consumer market with a cheap and quirky camera. By conventional standards, the LC-A and its successors suffer from major optical and mechanical flaws, which result in blurry images with uneven brightness and coloration. Lomographers like the unconventional appearance of these images.

Likewise, iPhoneographers are not necessarily concerned with the predictability and fidelity (true-to-life quality) of the camera's images. Considering that a new iPhone costs between $450 and $750, many photographers would find its image quality disappointing and its controls very limited. It bears no resemblance to conventional cameras in the same price range. On the other hand, iPhoneographers may assign greater value to the iPhone's ability to capture photos discretely and edit and share them immediately.

Some users may crave the best of both worlds—the brains of an iPhone in the body of a slightly more conventional camera. There are many third-party photo accessories for iOS devices and these accessories mimic some of the components of a modular, professional photo system. Particularly, we will discuss three kinds of accessories: lighting, tripods, and lens attachments. To help us appreciate the purpose of these accessories, let's establish a baseline of comparison. The following table shows the specifications of the built-in lenses and image sensors in iOS devices' rear cameras:

Device

Resolution (pixels)

Sensor diagonal (mm)

Focal length (mm)

Diagonal FOV (degrees)

Maximum aperture

iPhone 4

2592x1936

5.68

3.85

72.8

f/2.8

iPhone 4S

3264x2448

5.68

4.28

67.1

f/2.4

iPhone 5, 5C

3264x2448

5.68

4.10

69.4

f/2.4

iPhone 5S

3264x2448

6.11

4.12

73.1

f/2.2

iPhone 6, 6 Plus

3264x2448

6.11

4.15

72.7

f/2.2

iPhone 6S, 6S Plus

4032x3024

6.11

4.15

72.7

f/2.2

iPad 3, 4

2592x1936

5.68

4.3

66.9

f/2.4

iPad Air 1

iPad Mini 1, 2, 3

iPod Touch 5

2592x1936

4.33

3.3

66.5

f/2.4

iPad Air 2

iPad Mini 4

3264x2448

4.61

3.3

69.9

f/2.4

The field of view (FOV) is the angle formed by the lens's focal point and two points at diagonally opposite edges of the visible space. Some authors may specify horizontal or vertical FOV instead of diagonal FOV. By convention, FOV implies diagonal FOV if not otherwise specified. The focal length is the distance between the image sensor and the lens's optical center when the lens is focused on an infinitely distant subject. See the following diagram:

The diagonal FOV, the sensor's diagonal size, and the focal length are geometrically related according to the following formula:

diagonalFOVDegrees = 2 * atan(0.5 * sensorDiagonal / focalLength) * 180/pi

Depending on the model of the iOS device, the diagonal FOV ranges from 73.1 to 66.5 degrees. These values are equivalent to the FOV of a 29 mm to 33 mm lens in a traditional 35 mm camera system. Most photographers would characterize this FOV as moderately wide. Is moderately wide a good compromise? It depends on the use case. A wider angle helps to ensure that the subject literally has nowhere to hide. For example, this can be important in security applications. A narrower angle helps to ensure that details are captured even at a distance. For example, this can be important in product inspection applications, if the constraints of the workspace do not allow the camera to be placed close to the subject. If we want to choose the FOV, we must modify or accessorize the iOS device's optical system!

All the iOS cameras have small sensors. Their diagonal size ranges from 4.33 mm to 6.11 mm. For comparison, the diagonal size of the film or digital sensor in a 35 mm camera system is 43.3 mm. A smaller sensor has less capacity to gather light. To compensate, camera systems with small sensors tend to amplify the sensor's signal (the measurement of the light), but at the same time they amplify the random noise. Furthermore, to compensate for the noise, the system may blur the image. Thus, if we compare two images of the same scene at the same resolution, the image from the smaller sensor will tend to be noisier or blurrier. This difference becomes especially obvious when the light is dim. To summarize, we must expect that an iOS camera will take poor pictures in poor light. Thus, we must find or create good light!

Note

Engineers may refer to the amplification of the sensor's signal as gain and photographers may refer to it as ISO speed. The latter metric is formally defined by the International Standards Organization (ISO).

The ability to gather light is also directly related to the area of the lens's aperture. Typically, the aperture is expressed as an f-number or f-stop, which is defined as the ratio of the focal length to the aperture's diameter. Typically, an aperture is approximately circular and thus its area is proportional to the square of its radius. It follows that the intensity of the light passing through the aperture is inversely proportional to the square of the f-number. For example, an f/2 lens admits twice as much light as an f/2.8 lens. The iOS lenses have maximum apertures of f/2.2 to f/2.8, depending on the model. Values in this range are quite typical of wide-angle lenses in general, so the iOS lenses have neither an advantage nor disadvantage in this respect.

Finally, let's consider an issue of ergonomics. All iOS devices are lightweight and smooth and most of them are too small to hold in both hands. Thus, the user's grip is not firm. When a typical user holds out an iPhone to take a photo, the user's arm is like a long branch and the little iPhone shakes like a leaf. A high proportion of the pictures may suffer from motion blur. Comparatively, the design of a more traditional camera and lens may permit the user to brace the equipment's weight against his or her body in several places. Consider the following photograph:

The man in the background is Bob. Bob is left-handed. He is holding an iPhone in his left hand as he taps its camera button with his right hand. The man in the foreground is Joe. Joe is right-handed. He is equipped with a photo-sniper kit, which is a long lens mounted on two handles and a shoulder stock. The equipment's weight is braced against Joe's right knee and right shoulder. For additional stability, Joe's legs are folded and he is leaning leftward against a steel post and concrete slab. From another angle, the same pose looks like this:

This type of human stabilization can work well for some equipment. However, a more reliable approach is to use rigid support such as a tripod and we should definitely consider this when we tackle computer vision problems with a smartphone or tablet.

Now, let's take stock of the types of accessories that can change the lighting, stabilization, and perspective.

Lights

Many iOS devices have a built-in flash, which consists of a white LED light on the back of the device. Camera apps may activate the flash during photo capture, especially if the scene is dimly lit. Other apps may activate the flash for a long duration so that it acts as a flashlight or torch to help the user see. With only a single LED, the built-in flash may provide insufficient or uneven illumination in some circumstances.

If you need stronger or more evenly distributed illumination, or if your iOS device lacks a built-in flash altogether, you may want to purchase an external flash. Depending on the design, the external flash may mount as part of a case or may plug into the iOS device's audio jack. Typically, the external flash will have multiple white LEDs arranged in a line, grid, or ring. The latter design is called a ring flash.

Alternatively, in a controlled environment, you may set up any kind of lighting anywhere you please and you do not need to rely on the iOS device as a power source. Even a pair of well-placed desk lamps can greatly enhance the clarity and beauty of a scene. Normally, it is best to illuminate the subject from multiple angles to prevent shadows. Do not shine a light directly into the camera and do not illuminate the background more brightly than the subject, as these types of lighting tend to give the subject a very murky appearance with low contrast in the foreground. Sometimes, murky light can be artistically interesting, but it is not good for computer vision.

Tripods and other stabilization

A conventional photo camera has a threaded mount where the user may screw in the head of a tripod. Of course, an iOS device has no threaded mount. If we want to use a tripod with a standard screw, we may purchase an adapter that consists of a threaded mount and clip to hold the iOS device. Alternatively, we may purchase a tripod that has a built-in clip instead of a standard screw. Regardless of the type of mount, we also need to consider the following characteristics of the tripod:

  • Height: How tall is the tripod? Most tripods have extensible legs so that their height can vary. To help you decide what tripod height you require for a given application, consider how a person would normally look at the subject. If the subject is a small object such as a coin, a person might inspect it up close and similarly a short tripod might be appropriate. If the subject is a large object such as a lineup of cars on a highway, a person might watch it at eye level or might even look down on it from higher ground and similarly a tall tripod might be appropriate.

  • Weight: A heavy tripod is cumbersome to carry, but it may be able to resist a destabilizing force such as a gust of wind.

  • Material: Plastic may flex and crack. Metal may vibrate. Carbon fiber is less prone to these weaknesses, but it is relatively expensive. Some small tripods have bendable wire legs so that the user may wrap the tripod around another support, such as a branch or post. For example, GorillaPod is a well-known brand of tripods with bendable legs.

Typically, a small, lightweight tripod might cost between $10 and $30. This kind is often marketed as a mini or travel tripod. A tripod is a useful but optional accessory for all chapters in this book.

If you do not have a tripod or there is nowhere to place it, you may want to experiment with makeshift forms of stabilization. For example, if you want to monitor a room or hallway, you can tape the iOS device to a wall or ceiling. Be careful to choose tape that you can remove cleanly, without damaging the device's screen.

Lens attachments

A lens attachment or add-on lens is an additional optical unit that sits in front of the iPhone or iPad's built-in lens. Typically, the attachment is designed for the rear camera and its mount may consist of a magnet, clip, or case. The types of add-on lenses include the following:

  • Telephoto attachment: This enables the lens to capture a narrower (zoomed in) field of view, comparable to a spyglass. Sometimes, a telephoto attachment is called a zoom attachment.

  • Wide-angle attachment: This enables the lens to capture a wider (zoomed out) field of view.

  • Fisheye attachment: This enables the lens to capture an extremely wide field of view, between 100 and 180 degrees diagonally. By design, the fisheye perspective is distorted such that straight lines appear curved. Sometimes, a fisheye attachment is called a panoramic attachment because software can convert a fisheye image into a panorama (a perspective-corrected image with a wide aspect ratio).

  • Macro or close-up attachment: This enables the lens to focus at a short distance in order to capture a sharp image at a high level of magnification, comparable to a magnifying glass.

  • Microscope attachment: This enables a more extreme level of magnification, comparable to a microscope. The focus distance is so short that the lens attachment may almost touch the subject. Typically, the attachment includes a ring of LED lights to illuminate the subject.

Typically, a lens attachment might cost between $20 and $50. The sharpness of the optics can vary greatly, so try to compare reviews before you choose a product. A fisheye attachment could be a fun accessory for our photographic work in Chapter 2, Capturing, Storing, and Sharing Photos. A macro, close-up, or microscope attachment could be useful for our work with small objects in Chapter 5, Classifying Coins and Commodities. Generally, you can experiment with any lens attachment in any chapter's project.