Top Banner
OpenCV Overview Nate Kent [email protected] January 28, 2016 Nate Kent [email protected] OpenCV Overview January 28, 2016 1 / 33
33

OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Feb 22, 2018

Download

Documents

doancong
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

OpenCV Overview

Nate [email protected]

January 28, 2016

Nate Kent [email protected] OpenCV Overview January 28, 2016 1 / 33

Page 2: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Table Of Contents

1 Class Information

2 Which Version?

3 Tutorials

4 Installing OpenCV

5 How It All Works

6 Sample Programs

Nate Kent [email protected] OpenCV Overview January 28, 2016 2 / 33

Page 3: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Class Information

1 Class Information

2 Which Version?

3 Tutorials

4 Installing OpenCV

5 How It All Works

6 Sample Programs

Nate Kent [email protected] OpenCV Overview January 28, 2016 3 / 33

Page 4: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Book - Learning OpenCV

Nate Kent [email protected] OpenCV Overview January 28, 2016 4 / 33

Page 5: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Which Version?

1 Class Information

2 Which Version?

3 Tutorials

4 Installing OpenCV

5 How It All Works

6 Sample Programs

Nate Kent [email protected] OpenCV Overview January 28, 2016 5 / 33

Page 6: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Differences

V1: Old version. Written in C.

V2: Recent version. Written in C++ with interfaces and wrappersfor other language. Most tutorials will be focused on thisversion. Use this version unless you have a good reason touse one of the other two.

V3: Released June 2015. Uses newer C++ features with newalgorithms. Small differences in API from V2.

Nate Kent [email protected] OpenCV Overview January 28, 2016 6 / 33

Page 7: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Interfaces and Wrappers

Interfaces

PythonJavaMATLAB/OCTAVE (v2.5 and after)

Wrappers

C#

PerlChRuby

Nate Kent [email protected] OpenCV Overview January 28, 2016 7 / 33

Page 8: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Tutorials

1 Class Information

2 Which Version?

3 Tutorials

4 Installing OpenCV

5 How It All Works

6 Sample Programs

Nate Kent [email protected] OpenCV Overview January 28, 2016 8 / 33

Page 9: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Where To Find Tutorials

A huge number of tutorials are available on the OpenCV Website.

docs.opencv.org/doc/tutorials/tutorials.html

Nate Kent [email protected] OpenCV Overview January 28, 2016 9 / 33

Page 10: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Installing OpenCV

1 Class Information

2 Which Version?

3 Tutorials

4 Installing OpenCV

5 How It All Works

6 Sample Programs

Nate Kent [email protected] OpenCV Overview January 28, 2016 10 / 33

Page 11: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Windows

This method requires the latest Microsoft Visual Studio IDE.

1 Download OpenCV binaries

2 Extract along the build path

3 Set the OpenCV environment variable. This can be done in thecommand prompt:

setx -m OPENCV_DIR D:\OpenCV\Build\x86\vc10

setx -m OPENCV_DIR D:\OpenCV\Build\x64\vc10

setx -m OPENCV_DIR D:\OpenCV\Build\x86\vc11

setx -m OPENCV_DIR D:\OpenCV\Build\x64\vc11

Nate Kent [email protected] OpenCV Overview January 28, 2016 11 / 33

Page 12: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

OSX

1 Download the OpenCV source code

2 Install CMake

3 Open the terminal and navigate to the OpenCV folder

4 In the terminal, run the following commands:

$ cmake -G "Unix Makefiles"

$ make -j8 && make install

Nate Kent [email protected] OpenCV Overview January 28, 2016 12 / 33

Page 13: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Linux

Do it through your package manager. I believe almost all distros haveOpenCV available.

Nate Kent [email protected] OpenCV Overview January 28, 2016 13 / 33

Page 14: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Building With OpenCV

Tutorials for your specific IDE are available online.Warning: I’ve only ever used OpenCV’s C and C++ interfaces.

Nate Kent [email protected] OpenCV Overview January 28, 2016 14 / 33

Page 15: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

How It All Works

1 Class Information

2 Which Version?

3 Tutorials

4 Installing OpenCV

5 How It All WorksAn Example ProgramThe Mat ClassWindows And TrackbarsExample OperationsWorking With Videos And Cameras

6 Sample Programs

Nate Kent [email protected] OpenCV Overview January 28, 2016 15 / 33

Page 16: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

A Complete Example Program

Link!

Nate Kent [email protected] OpenCV Overview January 28, 2016 16 / 33

Page 17: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

The Mat Class

Mat is simply a header — not the actual data!

// Creates just the header parts

Mat A, C;

// Here we’ll know the method used (allocate matrix)

A = imread(argv[1], CV_LOAD_IMAGE_COLOR);

// Use the copy constructor

Mat B(A);

// Assignment operator

C = A;

Nate Kent [email protected] OpenCV Overview January 28, 2016 17 / 33

Page 18: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Creating Submatrices

These don’t allocate any new data. They simply point the header toportions of a matrix that is already in memory.

// Using a rectangle

Mat D (A, Rect(10, 10, 100, 100) );

// Using row and column boundaries

Mat E = A(Range::all(), Range(1,3));

Nate Kent [email protected] OpenCV Overview January 28, 2016 18 / 33

Page 19: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Duplicating Mats

These actually allocate a new matrix.

// Clone method

Mat F = A.clone();

// Copy method

Mat G;

A.copyTo(G);

Nate Kent [email protected] OpenCV Overview January 28, 2016 19 / 33

Page 20: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Explicitly Creating Mats

// Constructor

Mat M(2,2, CV_8UC3, Scalar(0,0,255));

// Via initializers

int sz[3] = {2,2,2};

Mat L(3,sz, CV_8UC(1), Scalar::all(0));

// Create function

M.create(4,4, CV_8UC(2));

// Matlab style

Mat E = Mat::eye(4, 4, CV_64F);

Mat O = Mat::ones(2, 2, CV_32F);

Mat Z = Mat::zeros(3,3, CV_8UC1);

Nate Kent [email protected] OpenCV Overview January 28, 2016 20 / 33

Page 21: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Creating And Showing In A Named Window

Mat image;

// Create the window

namedWindow( "Display Window", WINDOW_AUTOSIZE);

// Display in the window

imshow( "Display Window", image );

// Wait for the user to press a key

waitKey(0);

Nate Kent [email protected] OpenCV Overview January 28, 2016 21 / 33

Page 22: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Creating Trackbars

String name = "Trackbar Name";

String windowName = "Display Window";

int value; // Initial position

int count; // Max position

void* userdata; // Userdata

void Callback( int, void* );

// Create our trackbar

createTrackbar(name, windowName, value,

count, Callback, userdata);

Nate Kent [email protected] OpenCV Overview January 28, 2016 22 / 33

Page 23: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Drawing On The Image

There are many functions used to draw.

http://docs.opencv.org/modules/core/doc/drawing functions.html

Nate Kent [email protected] OpenCV Overview January 28, 2016 23 / 33

Page 24: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Thresholding

A source matrix can be the destination matrix!

Mat src, dst;

double threshold;

double maxval;

int type = THRESH_BINARY; // Many different types

threshold(src, dst, threshold, maxval, type);

Nate Kent [email protected] OpenCV Overview January 28, 2016 24 / 33

Page 25: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Mathematical Operations On Mat

Mat srcA, srcB, dst;

Mat mask;

int depth; // Many defined

// Addition

add(srcA, srcB, mask, depth);

// Finding the determinant

determinant(srcA);

// Flip the matrix

// 0 is vertical, > 0 horizontal, < 0 both

int flipcode;

flip(srcA, dst, flipcode);

Nate Kent [email protected] OpenCV Overview January 28, 2016 25 / 33

Page 26: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Working With Videos And Cameras

// Try to open based on video file

VideoCapture cap(arg);

// Try to open a camera based on index

cap.open(0);

// Process each frame like a normal image

Mat frame;

for(;;) {

// Put the frame in the matrix

cap >> frame;

// ... do your thing

}

// Write the last frame to a file

imwrite(frame, "lastframe.jpg");

Nate Kent [email protected] OpenCV Overview January 28, 2016 26 / 33

Page 27: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Sample Programs

1 Class Information

2 Which Version?

3 Tutorials

4 Installing OpenCV

5 How It All WorksAn Example ProgramThe Mat ClassWindows And TrackbarsExample OperationsWorking With Videos And Cameras

6 Sample Programs

Nate Kent [email protected] OpenCV Overview January 28, 2016 27 / 33

Page 28: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Sample Code Location

Sample code is downloaded along with the rest of OpenCV for Windowsand OSX. Linux users may have to install a separate package. They canbe found in: $OPENCV HOME/samples

Nate Kent [email protected] OpenCV Overview January 28, 2016 28 / 33

Page 29: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Let’s Do Some Samples

bgfg gmg and bgfg segm:Background filtersconvexhull*: Creating convex hullaround a set of pointsdemhist*: Creates a histogram ofthe imagedrawing: Basic drawing commandsedge: Canny edge detectionfback and lkdemo: Optical flowfitellipse: Fit an ellipse around ashapegencolors*: Generate a set of neasily distinguishable colors

houghcircles and houghlines:Detect circles and linesimage: Open and display an imagekalman: Create an use a Kalmanfilterminarea: Find minimum enclosingbox and circlemorphology2: Erosion and dilationsquares: Square detectionstarter video and video dmtx:Load, modify, display, and savevideos

Nate Kent [email protected] OpenCV Overview January 28, 2016 29 / 33

Page 30: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

convexhull

// Lines 28 to 40

vector<Point> points;

for( i = 0; i < count; i++ )

{

Point pt;

pt.x = rng.uniform(img.cols/4, img.cols*3/4);

pt.y = rng.uniform(img.rows/4, img.rows*3/4);

points.push_back(pt);

}

vector<int> hull;

convexHull(Mat(points), hull, true);

Nate Kent [email protected] OpenCV Overview January 28, 2016 30 / 33

Page 31: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

demhist

// Lines 39 to 46

Mat dst, hist;

image.convertTo(dst, CV_8U, a, b);

imshow("image", dst);

calcHist(&dst, 1, 0, Mat(), hist, 1, &histSize, 0);

Mat histImage = Mat::ones(200, 320, CV_8U)*255;

Nate Kent [email protected] OpenCV Overview January 28, 2016 31 / 33

Page 32: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

gencolors

// Lines 21 to 23

vector<Scalar> colors;

theRNG() = (uint64)time(0);

generateColors( colors, colorsCount );

Nate Kent [email protected] OpenCV Overview January 28, 2016 32 / 33

Page 33: OpenCV Overview - Computer EngineeringWritten in C++ with interfaces and ... I’ve only ever used OpenCV’s C and C++ interfaces. ... Nate Kent nkent@ OpenCV Overview January 28,

Questions?

Nate Kent [email protected] OpenCV Overview January 28, 2016 33 / 33