Find Mii Project and Opencv Tutorial
Find Mii Project and Opencv Tutorial
Find Mii Project and Opencv Tutorial
2012.10.12
Overview
FindMii Project
OpenCV Introduction
HighGUI
Basic Operations
AdaBoost
Face Detection
Optical Flow
Template Matching
Local Feature
2
FindMii Project
It’s the default project of this class.
Matlab
OpenCV
If you prefer OpenCV, there is a tutorial later.
Dates
Oct 16 (11:59pm): Finalizing team members and proposal
submission.
Nov 6 (11:59pm): Project milestone.
Dec 3 (11:59pm): Code and writeup due.
Dec 6 (time 1-3 pm): Course project presentation.
3
FindMii Project
4 tasks
Task 1 : Find this Mii
Task 2 : Find 2 look-alikes
Task 3 : Find n odd Miis out
Task 4 : Find the fastest Mii
3 levels
Easy, medium, hard
3x4 = a lot
Min-requirement : 3 different tasks (1 level for each)
4
FindMii Data
4 .m files
ClickMii.m (visualize your results)
FindMiiMain.m (main script)
FindMiiTask1Level1.m (dummy function)
PlayMii.m (play video)
opencvexample.cpp and Makefile (for OpenCV)
Training data
/afs/ir/class/cs231a/findmii/data
Ground truth
/afs/ir/class/cs231a/findmii/gt
5
Task 1: Find This Mii
Level 1 (Easy)
6
Task 2: Find 2 Look-alikes
Level 2 (Medium)
7
Task 3: Find n Odd Miis Out
Level 1 (Easy)
8
Task 4: Find the Fastest Mii
Level 3 (Hard)
9
Compile with OpenCV
10
Evaluations
click = [12 300 100; 66 400 200];
ClickMii(‘data/t2l2.avi’, ‘gt/t2l2.gt’, click);
11
Scoring
For a given task & level
A correct click on frame 1 is worth 1 point, each frame thereafter is discounted
by 0.99. And you can NOT access (even read into memory) any frame after the
one you made your final click for a task. Score is averaged over multiple clicks
if applicable. For example, if a task requires two clicks, you return 1st click at
frame 5, and 2nd click at frame 20 (both of them are correct), your score
would be (0.99^4 + 0.99^19)/2 = 0.8934. In this case, you can NOT access any
frame after frame 20.
For a given task
The highest score you achieve on the 3 levels. When comparing the scores on
different levels, scores on level 2 are multiplied by 1.5, and scores on level 3
are multiplied by 2. For example, for task 1, your score is 1.0 on level 1, and
0.77 on level 2, and you are not doing level 3. Your final score for task 1 would
be max(1*1, 0.77*1.5, 0*2) = 1.155.
For the challenge
Sum up your scores in all 4 tasks.
12
OpenCV – Introduction
OpenCV stands for the Open Source Computer Vision Library.
It was founded at Intel in 1999, went through some lean
years after the .bust but is now under active development,
now receiving ongoing support from Willow Garage.
OpenCV is free for commercial and research use.
It has a BSD license. The library runs across many
platforms and actively supports Linux, Windows and Mac
OS.
OpenCV was founded to advance the field of computer vision.
It gives everyone a reliable, real time infrastructure to
build on. It collects and makes available the most useful
algorithms.
13
OpenCV - Features
Cross Platform
Windows, Linux, Mac OS
Portable
iPhone
Android.
Language Support
C/C++
Python
14
Robot support
OpenCV Overview:> 500 functions
opencv.willowgarage.com
Geometric
Descriptors
Segmentation Camera
Calibration,
Stereo, 3D
Features
Transforms Utilities and
Data Structures
Tracking
Machine
Learning: Fitting
•Detection,
•Recognition
Matrix Math
15
OpenCV – Getting Started
Download OpenCV
http://opencv.org
Install from macports/aptitude
Setting up
Comprehensive guide on setting up OpenCV in various environments
at the official wiki.
Online Reference:
http://docs.opencv.org
Two books
16
HighGUI
HighGUI
Image I/O, rendering
Processing keyboard and other events, timeouts
Trackbars
Mouse callbacks
Video I/O
17
HighGUI: OpenCV Functions
void cv::namedWindow(const string& winname, int
flags=WINDOW_AUTOSIZE);
Creates window accessed by its name. Window handles
repaint, resize events. Its position is remembered in
registry.
void cv::destroyWindow(const string& winname);
void cv::imshow(const string& winname, cv::Mat& mat);
Copies the image to window buffer, then repaints it when
necessary. {8u|16s|32s|32f}{C1|3|4} are supported.
Only the whole window contents can be modified.
Dynamic updates of parts of the window are done using
operations on images, drawing functions etc.
18
HighGUI: OpenCV Functions
Mat imread(const string& filename, int flags=1);
loads image from file, converts to color or grayscale, if
need, and returns it (or returns empty cv::Mat()).
image format is determined by the file contents.
bool imwrite(const string& filename, Mat& image);
saves image to file, image format is determined from
extension.
Example: convert JPEG to PNG
cv::Mat img = cv::imread(“picture.jpeg”);
if(!img.empty()) cv::imwrite( “picture.png”, img );
19
HighGUI Sample Code
Example code: load an image from disk and display it on the
screen.
#include <opencv2/opencv.hpp>
int main( int argc, char* argv[] ) {
cv::Mat image = cv::imread( argv[1] );
cv::namedWindow( “Example1”,CV_WINDOW_AUTOSIZE );
cv::imshow( “Example1”, image );
cv::waitKey(0);
cv::destroyWindow( “Example1” );
return 0;
}
20
HighGUI Sample Code
Example code: load a video file and process each frame.
#include <opencv2/opencv.hpp>
int main( int argc, char* argv[] ) {
cv::VideoCapture capture("filename.avi");
if (!capture.isOpened()) return 1;
cv::Mat frame;
while (true) {
capture >> frame; if(!frame.data) break;
//process the frame here
}
capture.release();
return 0;
}
21
Key Classes
22
Manipulate an Image (Mat)
23
AdaBoost
Boosting
Covered in Lecture 2 (Sept 7).
Can a set of weak learners create a single strong learner?
Weak classifiers weighted by its accuracy.
Adaptive Boosting
Subsequent classifiers focus on instances misclassified by
previous classifiers.
Formulated by Yoav Freund and Robert Schapire.
Y. Freund, R.E. Schapire. A Decision-theoretic Generalization
of On-line Learningand an Application to Boosting. Journal of
Computer and System Sciences. 1997
24
Face Detection
25
? +1 face
? Features Classify
-1 not face
?
x F(x) y
26
Face Detection: Viola-Jones
Robust and fast
Introduced by Paul Viola and Michael Jones
http://research.microsoft.com/~viola/Pubs/Detect/violaJones_CVPR20
01.pdf
Haar-like Features
27
Face Detection: Viola-Jones
Haar-like features
Rectangular blocks, white or black
3 types of features:
two rectangles: horizontal/vertical
three rectangles
four rectangles
In 24x24 window: 180,000 possible
features
Simple weak classifier
Result
A classifier with 200 rectangle features was learned using
AdaBoost.
95% correct detection on test set with 1 in 14084 false
positives.
Less susceptible to over-fitting.
29
Face Detection: Cascaded Classifier
50% 20% 2%
IMAGE 5 Features 20 Features
SUB-WINDOW
1 Feature FACE
F F F
32
Face Detection: OpenCV Functions
bool CascadeClassifier::load(const string& filename);
Loads a classifier from a file.
void CascadeClassifier::detectMultiScale(const Mat& image,
vector<Rect>& objects, double scaleFactor=1.1, int
minNeighbors=3, int flags=0, Size minSize=Size(), Size
maxSize=Size());
Detects objects of different sizes in the input image. The
detected objects are returned as a list of rectangles.
33
Face Detection: Example
34
Optical Flow – Theory
Optical flow: the apparent motion of brightness patterns in the
image.
Will be covered in Lecture 13 (Nov 8)
problem.
Human vision does optical flow analysis all the time – being
35
Optical Flow – Theory (Lucas-Kanade)
Key assumption
Brightness constancy
I(x+u,y+v,t+1)=I(x,y,t)
Image measurements in a small region remain the same
although their location may change.
36
Optical Flow – Theory (Lucas-Kanade)
37
Optical Flow: OpenCV Functions
void goodFeaturesToTrack(Mat& image, vector<Point2f>&
corners, int maxCorners, double qualityLevel, double
minDistance);
Determines strong corners on an image.
38
Optical Flow - Example
39
Template Match
Template matching is a technique for finding areas of an image
that match (are similar) to a template image (patch).
40
Template Match: OpenCV Functions
void matchTemplate(Mat& image, Mat& templ, Mat& result, int method);
The function slides through image, compares the overlapped patches
of size w*h against templ using the specified method and stores the
comparison results in result .
After the function finishes the comparison, the best matches can be
found as global minimums (when CV_TM_SQDIFF was used) or
maximums (when CV_TM_CCORR or CV_TM_CCOEFF was used) using
the minMaxLoc() function.
void minMaxLoc(Mat& src, double* minVal, double* maxVal=0, Point*
minLoc=0, Point* maxLoc=0, InputArray mask=noArray());
Finds the global minimum and maximum in an array.
41
Local Features
Detection: Description:
42
Local Features Sample Code
Detect interest points and extract descriptors from the given
image.
cv::Mat img = cv::imread(“test.jpg”);
cv::Ptr<cv::FeatureDetector> detector = cv::createDetector(
“FAST” );
cv::Ptr<cv::DescriptorExtractor> descriptorExtractor =
cv::createDescriptorExtractor( “SURF” );
std::vector<cv::KeyPoint> keypoints;
detector->detect( img, keypoints );
cv::Mat descriptors;
descriptorExtractor->compute( img, keypoints, descriptors );
43
Local Features Sample Code
Match descriptors from two images.
Assume that the descriptors of two images are stored in
44