Commit 2bee8c46 by O'Reilly Media, Inc.

Initial commit

parents
/* *************** License:**************************
Oct. 3, 2008
Right to use this code in any way you want without warrenty, support or any guarentee of it working.
BOOK: It would be nice if you cited it:
Learning OpenCV: Computer Vision with the OpenCV Library
by Gary Bradski and Adrian Kaehler
Published by O'Reilly Media, October 3, 2008
AVAILABLE AT:
http://www.amazon.com/Learning-OpenCV-Computer-Vision-Library/dp/0596516134
Or: http://oreilly.com/catalog/9780596516130/
ISBN-10: 0596516134 or: ISBN-13: 978-0596516130
OTHER OPENCV SITES:
* The source code is on sourceforge at:
http://sourceforge.net/projects/opencvlibrary/
* The OpenCV wiki page (As of Oct 1, 2008 this is down for changing over servers, but should come back):
http://opencvlibrary.sourceforge.net/
* An active user group is at:
http://tech.groups.yahoo.com/group/OpenCV/
* The minutes of weekly OpenCV development meetings are at:
http://pr.willowgarage.com/wiki/OpenCV
************************************************** */
TOP := .
BINARIES := \
ch2_ex2_1 \
ch2_ex2_2 \
ch2_ex2_3 \
ch2_ex2_4 \
ch2_ex2_5 \
ch2_ex2_6 \
ch2_ex2_7 \
ch2_ex2_8 \
ch2_ex2_9 \
ch2_ex2_10 \
ch3_ex3_3 \
ch3_ex3_4 \
ch3_ex3_5 \
ch3_ex3_9 \
ch3_ex3_11 \
ch3_ex3_12 \
ch3_ex3_13 \
ch3_ex3_14 \
ch3_ex3_15 \
ch3_ex3_17 \
ch3_ex3_19 \
ch3_ex3_20 \
ch4_ex4_1 \
ch4_ex4_2 \
ch4_ex4_3 \
ch5_ex5_1 \
ch5_ex5_2 \
ch5_ex5_3 \
ch5_ex5_4 \
ch6_ex6_1 \
ch6_ex6_2 \
ch6_ex6_3 \
ch6_ex6_4 \
ch6_ex6_5 \
ch7_ex7_1 \
ch7_ex7_3_expanded \
ch7_ex7_5_HistBackProj \
ch7_ex7_5 \
ch8_ex8_2 \
ch8_ex8_3 \
ch9_ex9_1 \
ch9_background \
ch9_watershed \
ch10_ex10_1 \
ch10_ex10_1b_Horn_Schunck \
ch10_motempl \
ch10_ex10_2 \
ch11_ex11_1 \
ch11_ex11_1_fromdisk \
ch12_ex12_1 \
ch12_ex12_2 \
ch12_ex12_3 \
ch12_ex12_4 \
ch13_ex13_1 \
ch13_ex13_2 \
ch13_ex13_3 \
ch13_ex13_4
SUBDIRS :=
FLAGS = -I/usr/local/include/opencv # -Wall
include $(TOP)/common.mk
ch2_ex2_1: ch2_ex2_1.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch2_ex2_2: ch2_ex2_2.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch2_ex2_3: ch2_ex2_3.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch2_ex2_4: ch2_ex2_4.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch2_ex2_5: ch2_ex2_5.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch2_ex2_6: ch2_ex2_6.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch2_ex2_7: ch2_ex2_7.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch2_ex2_8: ch2_ex2_8.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch2_ex2_9: ch2_ex2_9.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch2_ex2_10: ch2_ex2_10.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch3_ex3_3: ch3_ex3_3.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch3_ex3_4: ch3_ex3_4.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch3_ex3_5: ch3_ex3_5.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch3_ex3_9: ch3_ex3_9.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch3_ex3_11: ch3_ex3_11.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch3_ex3_12: ch3_ex3_12.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch3_ex3_13: ch3_ex3_13.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch3_ex3_14: ch3_ex3_14.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch3_ex3_15: ch3_ex3_15.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch3_ex3_17: ch3_ex3_17.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch3_ex3_19: ch3_ex3_19.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch3_ex3_20: ch3_ex3_20.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch4_ex4_3: ch4_ex4_3.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch4_ex4_1: ch4_ex4_1.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch4_ex4_2: ch4_ex4_2.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch5_ex5_1: ch5_ex5_1.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch5_ex5_2: ch5_ex5_2.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch5_ex5_3: ch5_ex5_3.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch5_ex5_4: ch5_ex5_4.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch6_ex6_1: ch6_ex6_1.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch6_ex6_2: ch6_ex6_2.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch6_ex6_3: ch6_ex6_3.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch6_ex6_4: ch6_ex6_4.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch6_ex6_5: ch6_ex6_5.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch7_ex7_1: ch7_ex7_1.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch7_ex7_3_expanded: ch7_ex7_3_expanded.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch7_ex7_5_HistBackProj: ch7_ex7_5_HistBackProj.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch7_ex7_5: ch7_ex7_5.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch8_ex8_2: ch8_ex8_2.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch8_ex8_3: ch8_ex8_3.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch9_ex9_1: ch9_ex9_1.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch9_background: ch9_AvgBackground.o cv_yuv_codebook.o ch9_backgroundAVG.o
$(LINK) -o $@ $^ -lcv -lhighgui -lcxcore
ch9_watershed: ch9_watershed.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch10_ex10_1: ch10_ex10_1.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch10_ex10_1b_Horn_Schunck: ch10_ex10_1b_Horn_Schunck.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch10_motempl: ch10_motempl.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch10_ex10_2: ch10_ex10_2.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch9_pg11: ch9_pg11.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch9_pg14: ch9_pg14.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch9_pg15: ch9_pg15.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch11_ex11_1: ch11_ex11_1.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch11_ex11_1_fromdisk: ch11_ex11_1_fromdisk.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch12_ex12_1: ch12_ex12_1.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch12_ex12_2: ch12_ex12_2.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch12_ex12_3: ch12_ex12_3.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch12_ex12_4: ch12_ex12_4.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch13_ex13_1: ch13_ex13_1.o
$(LINK) -o $@ $^ -lcv -lhighgui
ch13_ex13_2: ch13_ex13_2.o
$(LINK) -o $@ $^ -lcv -lhighgui -lml
ch13_ex13_3: ch13_ex13_3.o
$(LINK) -o $@ $^ -lcv -lhighgui -lml
ch13_ex13_4: ch13_ex13_4.o
$(LINK) -o $@ $^ -lcv -lhighgui
# EXAMPLES OF HOW TO RUN THE PROGRAMS ASSUMING YOU ARE IN THEIR DIRECTORY
# On Linux, make the files using "make" (which will invoke Makefile in the directory)
# If you just copy programs from the pdf of the book, watch out that the
# quote characters aren't special characters.
#
# I put a calibration pattern, OpenCV_Chessboard.png in this directory
# print it out and tape it to any rigid object like a book or binder. It's
# for use with the camera calibration programs in Chapter 11.
#
#------------------------------------------------------------------------#
#CHAPTER 2 PROGRAMS:
./ch2_ex2_1 stuff.jpg
./ch2_ex2_2 test.avi
./ch2_ex2_3 test.avi
./ch2_ex2_4 stuff.jpg
./ch2_ex2_5 stuff.jpg
./ch2_ex2_6 stuff.jpg
./ch2_ex2_7 stuff.jpg
./ch2_ex2_8 stuff.jpg
# With video camera:
./ch2_ex2_9
# Using movie file:
./ch2_ex2_9 test.avi
# Writing won't work if you are using gstreamer. Use ffmpeg but might have to have developer libs from ffmpeg
./ch2_ex2_10 test.avi log_polar_out.avi
#------------------------------------------------------------------------#
#CHAPTER 3 PROGRAMS:
ch3_ex3_1.txt [not a program]
ch3_ex3_2.txt [not a program]
./ch3_ex3_3
./ch3_ex3_4
./ch3_ex3_5
ch3_ex3_6.txt [not a program]
ch3_ex3_7.txt [not a program]
ch3_ex3_8.txt [not a program]
./ch3_ex3_9
ch3_ex3_10.txt [not a program]
./ch3_ex3_11 stuff.jpg
./ch3_ex3_12 stuff.jpg 50 100 300 200 100
./ch3_ex3_13 stuff.jpg 50 100 300 200 100
./ch3_ex3_14 stuff.jpg adrian.jpg 50 100 300 200 0.5 0.5
./ch3_ex3_15
ch3_ex3_16.txt [not a program]
./ch3_ex3_17
ch3_ex3_18.xml [not a program]
./ch3_ex3_19
./ch3_ex3_20
#------------------------------------------------------------------------#
#CHAPTER 4 PROGRAMS
./ch4_ex4_1
./ch4_ex4_2 test.avi
./ch4_ex4_3 test.avi
#------------------------------------------------------------------------#
#CHAPTER 5 PROGRAMS
./ch5_ex5_1 stuff.jpg
./ch5_ex5_2 adrian.jpg
./ch5_ex5_3 adrian.jpg
./ch5_ex5_4 15 1 1 71 15 adrian.jpg
#------------------------------------------------------------------------#
#CHAPTER 6
./ch6_ex6_1 stuff.jpg
./ch6_ex6_2 stuff.jpg
./ch6_ex6_3 adrian.jpg
./ch6_ex6_4 stuff.jpg 100
./ch6_ex6_5
#------------------------------------------------------------------------#
#CHAPTER 7
./ch7_ex7_1 adrian.jpg
ch7_ex7_2.txt [not a program]
./ch7_ex7_3_expanded HandIndoorColor.jpg HandOutdoorSunColor.jpg HandOutdoorColor.jpg adrian.jpg
ch7_ex7_4.txt [not a program]
./ch7_ex7_5_HistBackProj BlueCup.jpg adrian.jpg 0
./ch7_ex7_5 faceTemplate.jpg faceScene.jpg
#------------------------------------------------------------------------#
#CHAPTER 8
ch8_ex8_1.txt [not a program]
./ch8_ex8_2 adrian.jpg
./ch8_ex8_3 faceTemplate.jpg
#------------------------------------------------------------------------#
#CHAPTER 9
./ch9_ex9_1 test.avi
# The background demo shows 2 background modeling methods: Averaging and YUV Codebook
# I've set the parameters to work well with tree.avi == we learn the model for 50 frames
# The parameters following tree.avi below are optional, but pretty good for that sequence
# You can adjust them on the fly. Best to pause "p", adjust, single step a bit with "s"
# and then resume running with "r" or "p". To adjust, the video window and NOT the consul
# window must be active. I put a built in pause in the short tree.avi sequence so you
# can experiment with parmeters as you single step "s", adjust" and step "s" along. -- Gary
./ch9_background 1 50 tree.avi 9 2 35 16 2 15 11 16
# Copy of watershed.cpp in samples/c directory is also here
./ch9_watershed fruits.jpg
#This one isn't a stand alone program, it is a function that does frame differencing as in the book
ch9_backgroundDiff.cpp [not built as a program]
#This is a potentially add on function to ch9_backgroundAVG.cpp -- it clears stale codebook entries
ch9_ClearStaleCB_Entries.cpp [not built as a program]
#------------------------------------------------------------------------#
#CHAPTER 10
./ch10_ex10_1
# Decided to do the same example, except using the Horn-Schunck algorithm
./ch10_ex10_1b_Horn_Schunck
# For fun, here's motion template code copied from samples/c. Uses a video camera. Point the camera
# away from you. Start the program, wait a few seconds until it goes black. Then move in front
# of the camera.
./ch10_motempl
./ch10_ex10_2
#------------------------------------------------------------------------#
#CHAPTER 11
# Calibration using video camera
# Parameters are chessboard_width height num_to_collect wait_frames
# The num_to_collect is how many valid chessboard captures we should use to calibrate
# The wait_frames is how many frames to skip before attempting to find a chessboard (this allows you to move it around).
# NOTE: The "9 6" work with OpenCV_Chessboard.png which you can print out and use for this.
./ch11_ex11_1 9 6 10 60
# We can also calibrate from a list of images on disk. You will need to run this in order for the birdseye demo to work.
./ch11_ex11_1_fromdisk 12 12 ch11_chessboards.txt
#------------------------------------------------------------------------#
#CHAPTER 12
./ch12_ex12_1 12 12 Intrinsics.xml Distortion.xml ch12_birdseye.jpg
# Just a belabored example of computing the fundamental matrix
# NOTE: The "9 6" work with OpenCV_Chessboard.png which you can print out and use for this.
./ch12_ex12_2 9 6 8 40
# Stereo calibration, rectification and depth
./ch12_ex12_3
./ch12_ex12_4
#------------------------------------------------------------------------#
#CHAPTER 13
./ch13_ex13_1
./ch13_ex13_2
./ch13_ex13_3
./ch13_ex13_4
#------------------------------------------------------------------------#
#CHAPTER 14
# You fill in these
This source diff could not be displayed because it is too large. You can view the blob instead.
// Example 10-1. Pyramid Lucas-Kanade optical flow code
//
/* *************** License:**************************
Oct. 3, 2008
Right to use this code in any way you want without warrenty, support or any guarentee of it working.
BOOK: It would be nice if you cited it:
Learning OpenCV: Computer Vision with the OpenCV Library
by Gary Bradski and Adrian Kaehler
Published by O'Reilly Media, October 3, 2008
AVAILABLE AT:
http://www.amazon.com/Learning-OpenCV-Computer-Vision-Library/dp/0596516134
Or: http://oreilly.com/catalog/9780596516130/
ISBN-10: 0596516134 or: ISBN-13: 978-0596516130
OTHER OPENCV SITES:
* The source code is on sourceforge at:
http://sourceforge.net/projects/opencvlibrary/
* The OpenCV wiki page (As of Oct 1, 2008 this is down for changing over servers, but should come back):
http://opencvlibrary.sourceforge.net/
* An active user group is at:
http://tech.groups.yahoo.com/group/OpenCV/
* The minutes of weekly OpenCV development meetings are at:
http://pr.willowgarage.com/wiki/OpenCV
************************************************** */
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <stdio.h>
const int MAX_CORNERS = 500;
int main(int argc, char** argv) {
// Initialize, load two images from the file system, and
// allocate the images and other structures we will need for
// results.
//
IplImage* imgA = cvLoadImage("OpticalFlow0.jpg",CV_LOAD_IMAGE_GRAYSCALE);
IplImage* imgB = cvLoadImage("OpticalFlow1.jpg",CV_LOAD_IMAGE_GRAYSCALE);
CvSize img_sz = cvGetSize( imgA );
int win_size = 10;
IplImage* imgC = cvLoadImage("OpticalFlow1.jpg",CV_LOAD_IMAGE_UNCHANGED);
// The first thing we need to do is get the features
// we want to track.
//
IplImage* eig_image = cvCreateImage( img_sz, IPL_DEPTH_32F, 1 );
IplImage* tmp_image = cvCreateImage( img_sz, IPL_DEPTH_32F, 1 );
int corner_count = MAX_CORNERS;
CvPoint2D32f* cornersA = new CvPoint2D32f[ MAX_CORNERS ];
cvGoodFeaturesToTrack(
imgA,
eig_image,
tmp_image,
cornersA,
&corner_count,
0.01,
5.0,
0,
3,
0,
0.04
);
cvFindCornerSubPix(
imgA,
cornersA,
corner_count,
cvSize(win_size,win_size),
cvSize(-1,-1),
cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03)
);
// Call the Lucas Kanade algorithm
//
char features_found[ MAX_CORNERS ];
float feature_errors[ MAX_CORNERS ];
CvSize pyr_sz = cvSize( imgA->width+8, imgB->height/3 );
IplImage* pyrA = cvCreateImage( pyr_sz, IPL_DEPTH_32F, 1 );
IplImage* pyrB = cvCreateImage( pyr_sz, IPL_DEPTH_32F, 1 );
CvPoint2D32f* cornersB = new CvPoint2D32f[ MAX_CORNERS ];
cvCalcOpticalFlowPyrLK(
imgA,
imgB,
pyrA,
pyrB,
cornersA,
cornersB,
corner_count,
cvSize( win_size,win_size ),
5,
features_found,
feature_errors,
cvTermCriteria( CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, .3 ),
0
);
// Now make some image of what we are looking at:
//
for( int i=0; i<corner_count; i++ ) {
if( features_found[i]==0|| feature_errors[i]>550 ) {
// printf("Error is %f/n",feature_errors[i]);
continue;
}
// printf("Got it/n");
CvPoint p0 = cvPoint(
cvRound( cornersA[i].x ),
cvRound( cornersA[i].y )
);
CvPoint p1 = cvPoint(
cvRound( cornersB[i].x ),
cvRound( cornersB[i].y )
);
cvLine( imgC, p0, p1, CV_RGB(255,0,0),2 );
}
cvNamedWindow("ImageA",0);
cvNamedWindow("ImageB",0);
cvNamedWindow("LKpyr_OpticalFlow",0);