Introduction to OpenCV 3.0 (with Java)

34
Computer Vision OpenCV Luigi De Russis Politecnico di Torino Dipartimento di Automatica e Informatica (DAUIN) Torino - Italy [email protected] This work is licensed under the Creative Commons (CC BY-SA) License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/

Transcript of Introduction to OpenCV 3.0 (with Java)

Page 1: Introduction to OpenCV 3.0 (with Java)

Computer Vision

OpenCV

Luigi De Russis

Politecnico di TorinoDipartimento di Automatica e Informatica (DAUIN)

Torino - Italy

[email protected]

This work is licensed under the Creative Commons (CC BY-SA)

License. To view a copy of this license, visit

http://creativecommons.org/licenses/by-sa/3.0/

Page 2: Introduction to OpenCV 3.0 (with Java)

What is OpenCV?

Computer Vision library Open Source (BSD License)

http://opencv.org

Originally developed by Intel

With more than 2500 optimized algorithms

Supports a lot of different languages C, C++, Python, and Java

but is written natively in C++

Cross platform also available for Android and iOS

10/14/2015 Introduction to OpenCV 2

Page 3: Introduction to OpenCV 3.0 (with Java)

What is used for?

Human-Computer Interaction (HCI)

Object Identification

Object Recognition

Face Recognition

Gesture Recognition

Motion Tracking

Image Processing

Mobile Robotics

… and so on

10/14/2015 Introduction to OpenCV 3

Page 4: Introduction to OpenCV 3.0 (with Java)

Why OpenCV?

We looked for a software/library:

that covers most of Computer Vision techniques

widely used or de-facto standard

free/open, possibly

in a language known by most students

in which a UI can be created

able to handle real-time image processing

10/14/2015 Introduction to OpenCV 4

Page 5: Introduction to OpenCV 3.0 (with Java)

Alternatives?

10/14/2015 Introduction to OpenCV 5

vs.

Page 6: Introduction to OpenCV 3.0 (with Java)

OpenCV: pros

Specificity OpenCV was made for computer vision/image

processing

Matlab is quite generic

Speed 30+ frames processed per seconds in real time image

processing with OpenCV

around 4-5 frames processed per seconds in real time image processing with Matlab

Efficiency Matlab needs more system resources than OpenCV

10/14/2015 Introduction to OpenCV 6

Page 7: Introduction to OpenCV 3.0 (with Java)

OpenCV: cons

Easy of use

Matlab won hands down!

Integrated Development Environment (IDE)

you can use Eclipse, Netbeans, Visual Studio, Qt,

XCode, … even a simple text editor for OpenCV

Matlab has is own IDE

Memory management

10/14/2015 Introduction to OpenCV 7

Page 8: Introduction to OpenCV 3.0 (with Java)

OpenCV: pros (final)

Price (!)

OpenCV Wrappers

SimpleCV, Emgu CV, ruby-opencv…

More similar to industry-level frameworks

10/14/2015 Introduction to OpenCV 8

Page 9: Introduction to OpenCV 3.0 (with Java)

OpenCV in C++: why not?

The OpenCV APIs have few capabilities for user interfaces

i.e., you can open a window with an image inside. Do you want a button? You cannot have it!

to have a “decent” user interface you need to install “something else” (e.g., Qt for C++)…

… and then compile OpenCV with Qt support

C++ learning curve is quite… steep

source: experiences from teaching this course in the first years

10/14/2015 Introduction to OpenCV 9

Page 10: Introduction to OpenCV 3.0 (with Java)

OpenCV in C++: why not?

OpenCV C++ APIs exist since OpenCV 1.0

but they dramatically changed starting from

OpenCV 2.0

it is possible, but not recommended, to “mix”

different OpenCV version

typically, it is a good way to get into trouble!

It is possible to use OpenCV C APIs together

with C++ APIs

typically it is a good way to get into trouble!

10/14/2015 Introduction to OpenCV 10

Page 11: Introduction to OpenCV 3.0 (with Java)

OpenCV in Java: why?

OpenCV provides also Java APIs nothing to compile (on Windows)

they works on Android, too

Java 7/8 has a first-grade user interface not Swing, but JavaFX

https://docs.oracle.com/javase/8/javase-clienttechnologies.htm

Almost everybody here should have some (basic) experience with Java

Performances between the C++ APIs and the Java APIs are comparable

10/14/2015 Introduction to OpenCV 11

Page 12: Introduction to OpenCV 3.0 (with Java)

OpenCV in Java: why?

The OpenCV Java APIs are really similar to the C++ version knowledge can be transferred!

examples: to store an image in memory both use the Mat object

to write an image on disk both use the imwrite method

Something changes a bit examples:

CV_RGB2GRAY (C++) becomes COLOR_RGB2GRAY (Java)

Point, Point2d, Point2f (C++) becomes 3 overloaded constructor of Point (Java)

10/14/2015 Introduction to OpenCV 12

Page 13: Introduction to OpenCV 3.0 (with Java)

OPENCV IN JAVA

Getting started…

10/14/2015 Introduction to OpenCV 13

Page 14: Introduction to OpenCV 3.0 (with Java)

Main Modules

10/14/2015 Introduction to OpenCV 14

core

basic structures and algorithms

imgproc

image processing algorithms (such as image filtering,

geometrical image transformations, histograms, etc.)

videoio

media I/O

OpenCV has a modular structure:

the package includes several shared or static libraries

New

Page 15: Introduction to OpenCV 3.0 (with Java)

Main Modules

10/14/2015 Introduction to OpenCV 15

video video analysis (such as motion estimation and object

tracking)

imgcodecs image file reading and writing

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, …)

New

Page 16: Introduction to OpenCV 3.0 (with Java)

Main Modules

10/14/2015 Introduction to OpenCV 16

highgui

provides basic UI capabilities (in C, C++, and Python)

ml

machine learning classes used for statistical

classification, regression and clustering of data

photo

computational photography

cuda*

various modules with CUDA-optimized algorithms

New

Not available in Java

Page 17: Introduction to OpenCV 3.0 (with Java)

Data Structures

10/14/2015 Introduction to OpenCV 17

We speak about Java APIs

All the OpenCV classes and methods are placed into the org.opencv.* packages

Mat

the primary image structure in OpenCV 2.x/3.x

overcomes the “old” IplImage/CvMat problems (OpenCV 1.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

Page 18: Introduction to OpenCV 3.0 (with Java)

Duplicate Mat objects…

Mat first = Mat.eye(3, 3, CvType.CV_8U1C);

10/14/2015 Introduction to OpenCV 18

1 0 0

0 1 0

0 0 1

Header

first

Page 19: Introduction to OpenCV 3.0 (with Java)

Duplicate Mat objects…

Mat first = Mat.eye(3, 3, CvType.CV_8U1C);

Mat second = first;

10/14/2015 Introduction to OpenCV 19

1 0 0

0 1 0

0 0 1

Header

first

Header

second

Page 20: Introduction to OpenCV 3.0 (with Java)

Duplicate Mat objects…

Mat first = Mat.eye(3, 3, CvType.CV_8U1C);

Mat second = first;

first.put(0, 0, 2, 2, 2);

10/14/2015 Introduction to OpenCV 20

Header

first

Header

second

2 2 2

0 1 0

0 0 1

Page 21: Introduction to OpenCV 3.0 (with Java)

Duplicate Mat objects…

Mat first = Mat.eye(3, 3, CvType.CV_8U1C);

Mat second = new Mat();

second = first.clone(); // first.copyTo(second);

first.put(0, 0, 2, 2, 2);

10/14/2015 Introduction to OpenCV 21

2 2 2

0 1 0

0 0 1

Header

first

Header

second1 0 0

0 1 0

0 0 1

Page 22: Introduction to OpenCV 3.0 (with Java)

Data Structures

10/14/2015 Introduction to OpenCV 22

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

Page 23: Introduction to OpenCV 3.0 (with Java)

Basic Drawing Operations

10/14/2015 Introduction to OpenCV 23

Imgproc.circle()

draws a simple or filled circle with a given center and radius

on a given image

Imgproc.line()

draws a line between two point in the given image

Imgproc.ellipse()

draws an ellipse outline, a filled ellipse, an elliptic arc, a filled

ellipse sector, …

Imgproc.rectangle()

draws a rectangle outline or a filled rectangle

note that negative thickness will fill the rectangle

Page 24: Introduction to OpenCV 3.0 (with Java)

Basic Image I/O

10/14/2015 Introduction to OpenCV 24

Imgcodecs.imread()

loads an image from file and return the corresponding Mat objectMat Imgcodecs.imread(String filename,

int flags)

Imgcodecs.imwrite()

save an image on diskbool Imgcodecs.imwrite(String filename,

Mat img, MatOfInt params)

Page 25: Introduction to OpenCV 3.0 (with Java)

Color Spaces

10/14/2015 Introduction to OpenCV 25

Imgproc.cvtColor

converts an input image from one color space to another

examples:

cvtColor(src, dest, Imgproc.COLOR_RGB2GRAY);

cvtColor(src, dest, Imgproc.COLOR_HSV2BGR);

cvtColor(src, dest, Imgproc.COLOR_RGB2BGR);

Important! Images in OpenCV uses BGR instead of

RGB

Page 26: Introduction to OpenCV 3.0 (with Java)

Getting started (without going crazy)

LET’S GET PRACTICAL!

10/14/2015 26 Introduction to OpenCV

Page 27: Introduction to OpenCV 3.0 (with Java)

How can we use OpenCV?

10/14/2015 Introduction to OpenCV 27

LABINF: already installed under Windows

Java 7 and OpenCV version 3.0

Eclipse (Luna) is the IDE

At home: we recommend Eclipse Mars/Luna, Java 8 and

OpenCV 3.0

feel free to use any Java IDE you like

Installation: see the PDF document in the teaching portal

Page 28: Introduction to OpenCV 3.0 (with Java)

What if I got problems?

10/14/2015 Introduction to OpenCV 28

Small problems

drop me a line

[email protected]

Normal problems

come to office hours

please send an e-mail beforehand

Enormous problems

pray?

no, seriously, we can schedule an extra “lesson”

Awesome student to me

Hi,

[…] I’m using “cvtColor(image, gray, COLOR_BGR2GRAY);” but it give this exception: […]

Can you help me?

Regards,

Problems with JavaFX and a gray scale image

Page 29: Introduction to OpenCV 3.0 (with Java)

What if I got problems?

10/14/2015 Introduction to OpenCV 29

Small problems

drop me a line

[email protected]

Normal problems

come to office hours

please send an e-mail beforehand

Enormous problems

pray?

no, seriously, we can schedule an extra “lesson”

Not-So-Awesome student to me

Hi,

[…] I followed the guide for installing OpenCV on my Mac but I have an error after step 3. Can

we meet on next Wednesday to solve the problem?

Thanks!

Regards,

OpenCV installation

Page 30: Introduction to OpenCV 3.0 (with Java)

What if I got problems?

10/14/2015 Introduction to OpenCV 30

Small problems

drop me a line

[email protected]

Normal problems

come to office hours

please send an e-mail beforehand

Enormous problems

pray?

no, seriously, we can schedule an extra “lesson”

Good student to me

Hi,

[…] I see the solution of Exercise 2.1 but I don’t understand the following expressions:

- main();

- System.out.println();

- @Override.

Can you explain to me what they are?

Regards,

Help with OpenCV

Page 31: Introduction to OpenCV 3.0 (with Java)

An e-mail not to be sent!

10/14/2015 Introduction to OpenCV 31

Page 32: Introduction to OpenCV 3.0 (with Java)

Useful Resources…

10/14/2015 Introduction to OpenCV 32

OpenCV Wiki https://github.com/Itseez/opencv/wiki

OpenCV Official Documentation http://docs.opencv.org/3.0.0/index.html

User Q&A forum http://answers.opencv.org/questions/

OpenCV Javadocs http://docs.opencv.org/java/3.0.0/

JavaFX Documentation https://docs.oracle.com/javase/8/javase-clienttechnologies.htm

Daniel Lélis Baggio, OpenCV 3.0 Computer Vision with Java, PACKT Publishing, 2015, ISBN 978-1-78328-397-2

Page 33: Introduction to OpenCV 3.0 (with Java)

Put everything together

DEMO HOUR

10/14/2015 33 Introduction to OpenCV

Page 34: Introduction to OpenCV 3.0 (with Java)

License

10/14/2015 Introduction to OpenCV 34

This work is licensed under the Creative Commons “Attribution-NonCommercial-ShareAlike Unported (CC BY-NC-SA 3,0)” License.

You are free: to Share - to copy, distribute and transmit the work

to Remix - to adapt the work

Under the following conditions: Attribution - You must attribute the work in the manner

specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).

Noncommercial - You may not use this work for commercial purposes.

Share Alike - If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one.