Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building Camera Features in a Snap

Building Camera Features in a Snap

Android 5.0 introduced the Camera2 APIs which include a full spectrum of camera features and configurations. However, building features with this powerful API can be overwhelming.

In this talk, we’ll:

Learn how camera hardware works and how that’s exposed in software
Digest the bits and bytes of color spaces and image formats
Visualize how image kernels work
Set up a testing environment that allows us to iterate quickly
Introduce the concepts of computer vision

This talk will get you up and running with the camera APIs. You’ll learn how to write efficient code that operates on millions of pixels.

Matthew Precious

September 25, 2017
Tweet

More Decks by Matthew Precious

Other Decks in Programming

Transcript

  1. Library Hours Months • A lot of code is written

    for you • Changes and fixes require understanding all the code
  2. RGB

  3. YUV

  4. YUV

  5. RGB

  6. RGB 888 Red Green Blue 24 bits per pixel 8

    bits 8 bits 8 bits 1920×1080 ≈ 6MB
  7. RGB 888 Red Green Blue 24 bits per pixel 8

    bits 8 bits 8 bits 1920×1080 ≈ 6MB 60fps: 360MB/s
  8. RGB 444 Red Green Blue 12 bits per pixel 4

    bits 4 bits 4 bits 1920×1080 ≈ 3MB
  9. RGB 444 Red Green Blue 12 bits per pixel 4

    bits 4 bits 4 bits 1920×1080 ≈ 3MB 60fps: 180MB/s
  10. YUV

  11. YUV 4:4:4 Y' Cr Cb 24 bits per pixel 8

    bits 8 bits 8 bits 1920×1080 ≈ 6MB
  12. YUV 4:2:2 Y' Cr Cb 8 bits 8 bits 8

    bits 2 pixels 2 pixels
  13. YUV 4:2:2 Y' Cr Cb 16 bits per pixel 8

    bits 4 bits 4 bits 1920×1080 ≈ 4MB
  14. YUV 4:2:0 Y' Cr Cb 8 bits 8 bits 8

    bits 4 pixels 4 pixels
  15. YUV 4:2:0 Y' Cr Cb 12 bits per pixel 8

    bits 2 bits 2 bits 1920×1068 ≈ 3MB
  16. Camera1 Camera2 Min SDK 1 Min SDK 21 2009 2014

    100% of devices 80%* of devices
  17. Camera1 Camera2 Min SDK 1 Min SDK 21 2009 2014

    100% of devices 80%* of devices
  18. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 0 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Convolution
  19. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 0 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Convolution
  20. Convolution 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 255 255 255
  21. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 255 255 255 Convolution
  22. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 255 255 255 Convolution
  23. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 255 255 255 0 1 0 1 –4 1 0 1 0 Convolution
  24. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 255 255 255 0 1 0 1 –4 1 0 1 0 Convolution
  25. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 255 255 255 0 × 1 × 0 × 1 × –4 × 1 × 0 × 1 × 0 × Convolution
  26. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 255 255 255 0 0 × 1 × 0 × 1 × –4 × 1 × 0 × 1 × 0 × Convolution
  27. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 × 1 × 0 × 1 × –4 × 1 × 0 × 1 × 0 × Convolution
  28. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 0 × 1 × 0 × 1 × –4 × 1 × 0 × 1 × 0 × Convolution
  29. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 0 0 × 1 × 0 × 1 × –4 × 1 × 0 × 1 × 0 × Convolution
  30. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 0 0 0 × 1 × 0 × 1 × –4 × 1 × 0 × 1 × 0 × Convolution
  31. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 0 0 0 0 × 1 × 0 × 1 × –4 × 1 × 0 × 1 × 0 × Convolution
  32. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 0 0 0 255 0 × 1 × 0 × 1 × –4 × 1 × 0 × 1 × 0 × Convolution
  33. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 0 0 0 255 255 0 × 1 × 0 × 1 × –4 × 1 × 0 × 1 × 0 × Convolution
  34. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 0 0 0 255 255 0 0 × 1 × 0 × 1 × –4 × 1 × 0 × 1 × 0 × Convolution
  35. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 0 0 0 255 255 0 255 0 × 1 × 0 × 1 × –4 × 1 × 0 × 1 × 0 × Convolution
  36. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 0 0 0 255 255 0 255 510 0 × 1 × 0 × 1 × –4 × 1 × 0 × 1 × 0 × Convolution
  37. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 0 0 0 255 255 0 255 510 255 0 × 1 × 0 × 1 × –4 × 1 × 0 × 1 × 0 × Convolution
  38. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 0 0 0 255 255 0 255 510 255 0 0 × 1 × 0 × 1 × –4 × 1 × 0 × 1 × 0 × Convolution
  39. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 0 0 0 255 255 0 255 510 255 0 255 0 × 1 × 0 × 1 × –4 × 1 × 0 × 1 × 0 × Convolution
  40. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 0 0 0 255 255 0 255 510 255 0 255 255 0 × 1 × 0 × 1 × –4 × 1 × 0 × 1 × 0 × Convolution
  41. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 0 0 0 255 255 0 255 510 255 0 255 255 0 0 × 1 × 0 × 1 × –4 × 1 × 0 × 1 × 0 × Convolution
  42. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 255 510 255 255 0 0 255 255 0 0 0 0 255 255 0 0 Convolution
  43. 0 0 0 0 0 0 0 0 0 0

    0 0 0 0 0 0 0 0 0 0 0 255 255 255 0 0 0 255 255 255 0 0 0 255 255 255 510 Convolution
  44. Sobel Filter 1 0 –1 2 0 –2 1 0

    –1 1 2 1 0 0 0 –1 –2 –1
  45. 1 0 –1 2 0 –2 1 0 –1 Sobel

    Filter 1 2 1 0 0 0 –1 –2 –1 Gy = Gx = =
  46. y

  47. y

  48. y r

  49. y

  50. θ ~r ~r ~r 0 12 9 3 15 13

    11 5 30 13 11 7 45 13 11 8 60 11 11 9 75 9 9 9 90 6 7 9 105 3 4 8 120 0 2 6 135 -3 0 4 150 -6 -3 2 165 -9 -6 0 y
  51. θ ~r ~r ~r 0 12 9 3 15 13

    11 5 30 13 11 7 45 13 11 8 60 11 11 9 75 9 9 9 90 6 7 9 105 3 4 8 120 0 2 6 135 -3 0 4 150 -6 -3 2 165 -9 -6 0 y
  52. θ ~r ~r ~r 0 12 9 3 15 13

    11 5 30 13 11 7 45 13 11 8 60 11 11 9 75 9 9 9 90 6 7 9 105 3 4 8 120 0 2 6 135 -3 0 4 150 -6 -3 2 165 -9 -6 0
  53. θ ~r ~r ~r 0 12 9 3 15 13

    11 5 30 13 11 7 45 13 11 8 60 11 11 9 75 9 9 9 90 6 7 9 105 3 4 8 120 0 2 6 135 -3 0 4 150 -6 -3 2 165 -9 -6 0
  54. class ByteGrid( private val data: ByteArray, private val width: Int,

    private val height: Int )x{ fun get(x: Int, y: Int): Byte }x
  55. class ByteGrid( private val data: ByteArray, private val width: Int,

    private val height: Int )x{ fun get(x: Int, y: Int): Byte fun rotate(rotation: Int) }x
  56. class ByteGrid( private val data: ByteArray, private val width: Int,

    private val height: Int )x{ fun get(x: Int, y: Int): Byte fun rotate(rotation: Int) fun crop(x: Int, y: Int, width: Int, height: Int) }x
  57. val KERNEL = arrayOf( arrayOf(0, 1, 0), arrayOf(1, -4, 1),

    arrayOf(0, 1, 0) ) var sum = 0 for (kernelY in 0 until KERNEL.size) { for (kernelX in 0 until KERNEL[kernelY].size) { sum += KERNEL[kernelY][kernelX] * image.get(x, y) } }
  58. !// Kernel: !// 0 1 0 !// 1 -4 1

    !// 0 1 0 var sum = (image.get(x, y - 1) + image.get(x - 1, y) - 4 * image.get(x, y) + image.get(x + 1, y) + image.get(x, y + 1))