SlideShare a Scribd company logo
Computer Vision
Programming using Android
and Open CV
Vishwas Raval,
Computer Vision in brief
Computer Vision… what?
 Computer Vision is the process of using a
computer based algorithm to identify
patterns in the data of images
 Basic Steps:
 Process the image
 Use an algorithm to identify a pattern
 Present that pattern in a meaningful way
What is Computer Vision?
Image Processing
An “Image” and A “Frame”
 Both images and frames are
made up of individual pixels
organized in a 2 dimensional
array.
 For a color image, each pixel
can be anything from 8 to 32
bits wide.
 Most monochrome images use
8 bits per pixel.
 A frame is a single image in a
video sequence.
X
Y
pixel
“Feature” - A Fundamental
Concept in Computer Vision
Feature (fchr) n.
A prominent or distinctive aspect, quality, or characteristic: a feature of one’s
personality; a feature of the landscape. http://www.thefreedictionary.com/feature
 The concept of, “a feature of an object” is very
important for most computer vision algorithms.
 In an image or frame, a feature is a group of
pixels with some unique attribute.
corner points edge contrast motion
Some Basic “Building Blocks”
Algorithms in Computer Vision
 Detection
 Motion Detection—Finds groups of pixels (features) that
are in motion (change in position from one frame to the
next).
 Line Detection—Finds groups of pixels (features) that are
organized in straight lines, along edges.
 Face Detection—Finds groups of pixels organized in a
group that fits the template of a face.
 Tracking
 Optical Flow based tracking—A combination of algorithms
used to track moving objects in a video using features.
What is Image in Computer?
Digital Image- Representing
Image into digits
W
H
A
TISIT
A BEGINNERS GUIDE TOCOMPUTER VISION
W
H
A
TISIT
A BEGINNERS GUIDE TOCOMPUTER VISION
W
H
A
TISIT
For more precision – Using
floating values
Image Classification
Classification
Why is it challenging?
COMPUTER
VISION HAS
SURPRISING
APPLICATIONS
OpenCV+Android.pptx
OpenCV+Android.pptx
OpenCV in brief
OpenCV… what?
What is OpenCV?
 Open source Computer Vision library
BSD License
http://opencv.org
 Originally developed by Intel
 Has more than 2500 optimized
algorithms
 Supports a lot of different languages
C, C++, Python, Java but is written
natively in C++
 Cross platform
also available for Android and iOS
Open CV – At Glance
Capabilities
What it is used for?
 Human-Computer Interaction
(HCI)
 Object Recognition
 Face Recognition
 Gesture Recognition
 Motion Tracking
 Image Processing
 Mobile Robotics
 … and so on
Always ask
Motivations
Why OpenCV?
OpenCV: pros & cons
Specificity
OpenCV was made for image processing, Matlab
is quite generic
Processing Speed
30+ frames/second in real time image
processing with OpenCV. Around 4-5
frames/second in Matlab
Efficient
Matlab needs more system resources than
OpenCV
 Price (!)
OpenCV: pros & cons
 Easy of use
Matlab won hands down!
 Integrated Development Environment (IDE)
you can use Eclipse, Netbeans, Visual
Studio, Qt, XCode, Android Studio … even a
simple text editor for OpenCV
 Matlab has its own IDE
 Automatic memory management in OpenCV
OpenCV meets Android
Modules
OpenCV has a modular structure:
the package includes several shared or static libraries
core
basic structures and algorithms
imgproc
image processing algorithms (such as image
filtering, geometrical image transformations,
histograms, etc.)
video
video analysis (such as motion estimation and
object tracking)
Modules
highgui
basic operation to read/write/encode images; in C,
C++ and Python it provides also basic UI
capabilities
calib3d
camera calibration and 3D reconstruction
features2d
2D features framework (feature detectors,
descriptors, and descriptor matchers)
objdetect
detection of objects and other items (e.g., faces,
eyes, mugs, people, …)
Modules
 ml
machine learning classes used for statistical
classification, regression and clustering of
data
 gpu
GPU-accelerated algorithms
 photo
computational photography
 ccl
OpenCL-accelerated algorithms
Data Structures
 We speak about Java API
 All the OpenCV classes and methods are placed
into the org.opencv.* packages
 Mat: The primary image structure in OpenCV 2.x
overcomes the “old” IplImage/CvMat problem of
(OpenCV x/C API) automatic memory management
(more or less in C++) two data parts:
 matrix header (contains information about the matrix)
 a pointer to the matrix containing the pixel values
Data Structures
Point
2D point defined by x, y coordinates
 Point first = new Point(2, 3);
Size
2D size structure specify the size (width and
height) of an image or rectangle
Rect
2D rectangle object
Basic Image I/O
 Highgui.imread
loads an image from file and return the
corresponding Mat object
 Highui.imwrite save an image on disk
Basic Drawing Operations
 Core.circle
draws a simple or filled circle with a given center and radius
on a given image
 Core.line
draws a line between two point in the given image
 Core.ellipse
draws an ellipse outline, a filled ellipse, an elliptic arc, a
filled ellipse sector, …
 Core.rectangle
draws a rectangle outline or a filled rectangle note that
negative thickness will fill the rectangle
Color Spaces
 Imgproc.cvtColor
converts an input image from one color space to
another examples:
 Important! Images in OpenCV uses BGR instead
of RGB
 cvtColor(src, dest, Imgproc.COLOR_RGB2GRAY);
 cvtColor(src, dest, Imgproc.COLOR_HSV2BGR);
 cvtColor(src, dest, Imgproc.COLOR_RGB2BGR);
Getting Started…
Configuring Open CV in Android
Studio
1. Extract the downloaded zip file.
2. Open Android Studio and create a new project
with package of your choice.
3. Then select File ->New -> Import Module
4. You need to select the OpenCV SDK location.
Select OpenCV-android-sdk/sdk/java. Then
select Next and Finish. OpenCV sdk is imported
as a module.
OpenCV+Android.pptx
Configuring Open CV in Android
Studio
But it may throw error. Lets see how to fix this.
5. In the project explorer change the project view
from Android to Project. Open Project ->
openCVLibrary -> build.gradle
6. Change
the compileSdkVersion, targetSdkVersion and
buildToolsVersion value to the latest version
you use. Then sync the project. The errors will be
gone.
OpenCV+Android.pptx
Configuring Open CV in Android
Studio
7. Switch back to Android view in Project explorer.
Right click on the app module and select Open
Module Settings.
8. For the app module in the Dependencies tab, select
Add -> Module Dependency -> openCVLibrary
OpenCV+Android.pptx
Add Native Library
1. Now we need to add native JNI libraries in our
project. These libraries should be added in
jniLibs directory. Create a new jniLibs directory
in app-> src -> main.
2. Open the extracted OpenCV SDK directory.
Switch to OpenCV-android-sdk/sdk/native/libs
directory.
Add Native Library
3. You will find directories for many CPU
architectures. Copy the required architecture
directory to the jniLibs directory. (for eg. Copy
x86_64 and armeabi-v7a because if your Android
emulator has x86_64 architecture and Phone has
armeabi-v7a architecture. Delete all files except
libopencv_java3.so. (Deleting is optional)
Demo
• See the demo given

More Related Content

OpenCV+Android.pptx

  • 1. Computer Vision Programming using Android and Open CV Vishwas Raval,
  • 2. Computer Vision in brief Computer Vision… what?
  • 3.  Computer Vision is the process of using a computer based algorithm to identify patterns in the data of images  Basic Steps:  Process the image  Use an algorithm to identify a pattern  Present that pattern in a meaningful way What is Computer Vision?
  • 5. An “Image” and A “Frame”  Both images and frames are made up of individual pixels organized in a 2 dimensional array.  For a color image, each pixel can be anything from 8 to 32 bits wide.  Most monochrome images use 8 bits per pixel.  A frame is a single image in a video sequence. X Y pixel
  • 6. “Feature” - A Fundamental Concept in Computer Vision Feature (fchr) n. A prominent or distinctive aspect, quality, or characteristic: a feature of one’s personality; a feature of the landscape. http://www.thefreedictionary.com/feature  The concept of, “a feature of an object” is very important for most computer vision algorithms.  In an image or frame, a feature is a group of pixels with some unique attribute. corner points edge contrast motion
  • 7. Some Basic “Building Blocks” Algorithms in Computer Vision  Detection  Motion Detection—Finds groups of pixels (features) that are in motion (change in position from one frame to the next).  Line Detection—Finds groups of pixels (features) that are organized in straight lines, along edges.  Face Detection—Finds groups of pixels organized in a group that fits the template of a face.  Tracking  Optical Flow based tracking—A combination of algorithms used to track moving objects in a video using features.
  • 8. What is Image in Computer?
  • 11. A BEGINNERS GUIDE TOCOMPUTER VISION W H A TISIT
  • 12. A BEGINNERS GUIDE TOCOMPUTER VISION W H A TISIT
  • 13. For more precision – Using floating values
  • 15. Why is it challenging?
  • 20. What is OpenCV?  Open source Computer Vision library BSD License http://opencv.org  Originally developed by Intel  Has more than 2500 optimized algorithms  Supports a lot of different languages C, C++, Python, Java but is written natively in C++  Cross platform also available for Android and iOS
  • 21. Open CV – At Glance
  • 23. What it is used for?  Human-Computer Interaction (HCI)  Object Recognition  Face Recognition  Gesture Recognition  Motion Tracking  Image Processing  Mobile Robotics  … and so on
  • 26. OpenCV: pros & cons Specificity OpenCV was made for image processing, Matlab is quite generic Processing Speed 30+ frames/second in real time image processing with OpenCV. Around 4-5 frames/second in Matlab Efficient Matlab needs more system resources than OpenCV  Price (!)
  • 27. OpenCV: pros & cons  Easy of use Matlab won hands down!  Integrated Development Environment (IDE) you can use Eclipse, Netbeans, Visual Studio, Qt, XCode, Android Studio … even a simple text editor for OpenCV  Matlab has its own IDE  Automatic memory management in OpenCV
  • 29. Modules OpenCV has a modular structure: the package includes several shared or static libraries core basic structures and algorithms imgproc image processing algorithms (such as image filtering, geometrical image transformations, histograms, etc.) video video analysis (such as motion estimation and object tracking)
  • 30. Modules highgui basic operation to read/write/encode images; in C, C++ and Python it provides also basic UI capabilities calib3d camera calibration and 3D reconstruction features2d 2D features framework (feature detectors, descriptors, and descriptor matchers) objdetect detection of objects and other items (e.g., faces, eyes, mugs, people, …)
  • 31. Modules  ml machine learning classes used for statistical classification, regression and clustering of data  gpu GPU-accelerated algorithms  photo computational photography  ccl OpenCL-accelerated algorithms
  • 32. Data Structures  We speak about Java API  All the OpenCV classes and methods are placed into the org.opencv.* packages  Mat: The primary image structure in OpenCV 2.x overcomes the “old” IplImage/CvMat problem of (OpenCV x/C API) automatic memory management (more or less in C++) two data parts:  matrix header (contains information about the matrix)  a pointer to the matrix containing the pixel values
  • 33. Data Structures Point 2D point defined by x, y coordinates  Point first = new Point(2, 3); Size 2D size structure specify the size (width and height) of an image or rectangle Rect 2D rectangle object
  • 34. Basic Image I/O  Highgui.imread loads an image from file and return the corresponding Mat object  Highui.imwrite save an image on disk
  • 35. Basic Drawing Operations  Core.circle draws a simple or filled circle with a given center and radius on a given image  Core.line draws a line between two point in the given image  Core.ellipse draws an ellipse outline, a filled ellipse, an elliptic arc, a filled ellipse sector, …  Core.rectangle draws a rectangle outline or a filled rectangle note that negative thickness will fill the rectangle
  • 36. Color Spaces  Imgproc.cvtColor converts an input image from one color space to another examples:  Important! Images in OpenCV uses BGR instead of RGB  cvtColor(src, dest, Imgproc.COLOR_RGB2GRAY);  cvtColor(src, dest, Imgproc.COLOR_HSV2BGR);  cvtColor(src, dest, Imgproc.COLOR_RGB2BGR);
  • 38. Configuring Open CV in Android Studio 1. Extract the downloaded zip file. 2. Open Android Studio and create a new project with package of your choice. 3. Then select File ->New -> Import Module 4. You need to select the OpenCV SDK location. Select OpenCV-android-sdk/sdk/java. Then select Next and Finish. OpenCV sdk is imported as a module.
  • 40. Configuring Open CV in Android Studio But it may throw error. Lets see how to fix this. 5. In the project explorer change the project view from Android to Project. Open Project -> openCVLibrary -> build.gradle 6. Change the compileSdkVersion, targetSdkVersion and buildToolsVersion value to the latest version you use. Then sync the project. The errors will be gone.
  • 42. Configuring Open CV in Android Studio 7. Switch back to Android view in Project explorer. Right click on the app module and select Open Module Settings. 8. For the app module in the Dependencies tab, select Add -> Module Dependency -> openCVLibrary
  • 44. Add Native Library 1. Now we need to add native JNI libraries in our project. These libraries should be added in jniLibs directory. Create a new jniLibs directory in app-> src -> main. 2. Open the extracted OpenCV SDK directory. Switch to OpenCV-android-sdk/sdk/native/libs directory.
  • 45. Add Native Library 3. You will find directories for many CPU architectures. Copy the required architecture directory to the jniLibs directory. (for eg. Copy x86_64 and armeabi-v7a because if your Android emulator has x86_64 architecture and Phone has armeabi-v7a architecture. Delete all files except libopencv_java3.so. (Deleting is optional)
  • 46. Demo • See the demo given