29
Computer Vision SS 2008 Tutorial/Lab II MSc in CS Moritz Vieth

CV_SS08_Lab_2

Embed Size (px)

Citation preview

Page 1: CV_SS08_Lab_2

Computer Vision

SS 2008

Tutorial/Lab II

MSc in CS Moritz Vieth

Page 2: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 2

Overview

Reading

Programming in CV

− Basic steps− OpenCV− Starter kit

The CV Lab

− Equipment− Accounts− Source control (SVN)

Page 3: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 3

ReadingsWhat is the main underlying problem of CV?What is a model?What are the four representations presented in

the reading?In which way can concepts of AI/Machine

Learning aid CV?Does CV have to exactly reproduce the human

perception system?Name some CV applications (and their means)

Page 4: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 4

Programming in CVBasic tasks:

− Data acquisitionSingle imagesImage sequences (video file/camera)

− Image processingManipulationAnalysis

− OutputDisplayFilesetc.

Page 5: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 5

OpenCVOpen Source Library

− Originally developed by Intel− Written in C/C++ (but mostly C)− http://opencvlibrary.sourceforge.net/

Basic functions and structures

Advanced algorithms

“Head start into CV programming”

Nomenclature:

− Methods start with cv (lowercase)− Structures start with Cv (uppercase)

Page 6: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 6

OpenCV: Basic FunctionsStructures & methods for

− Image handling− Image acquisition

Single image fileVideo fileCamera

− Image manipulation− Output

DisplaySave to file

Page 7: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 7

OpenCV: Advanced AlgorithmsAlgorithms and functions commonly used in CV

applications

− Edge/Corner detection− Pattern matching− Structural analysis− Motion detection− Tracking− Calibration (not that good, use MATLAB instead)− Face Detection− ...

Page 8: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 8

Using OpenCVImage acquisition (single file)

#include "cv.h"#include "highgui.h"

int main( int argc, char** argv ){    IplImage* img;    if(img = cvLoadImage( “image.jpg”,

CV_LOAD_IMAGE_COLOR)) != 0 ){

        doSomething(img); // your processing method        cvWaitKey(5); // very important, contains event                      // processing loop inside

        return 0;    }    return ­1;}

Page 9: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 9

Using OpenCVImage acquisition (video file)

#include "cv.h"#include "highgui.h"

int main( int argc, char** argv ){    IplImage* img;    CvCapture* capture = cvCaptureFromFile(“video.mpg”);    while(img = cvQueryFrame(capture) != 0 ))

{        doSomething(img); // your processing method        cvWaitKey(5); // very important, contains event                      // processing loop inside

        return 0;    }

 cvDestroyCapture(&capture);    return ­1;}

Page 10: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 10

Using OpenCVImage acquisition (camera)

#include "cv.h"#include "highgui.h"

int main( int argc, char** argv ){    IplImage* img;    CvCapture* capture = cvCaptureFromCAM(0);    while(img = cvQueryFrame(capture) != 0 ))

{        doSomething(img); // your processing method        cvWaitKey(5); // very important, contains event                      // processing loop inside

        return 0;    }

 cvDestroyCapture(&capture);    return ­1;}

Page 11: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 11

Using OpenCVImage handling: Accessing a pixel

Easy, isn't it?− We'll come back to that

//Set pixel value://IplImage* image, int channel, int value

    (((uchar*)(image­>imageData + image­>widthStep*(y))))[x *image­>nChannels + channel] = (uchar)value;

//Get pixel value://IplImage* image, int channel, int value

    value = (((uchar*)(image­>imageData + image­>widthStep*(y)))[(x * image­>nChannels) + channel] ;

Page 12: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 12

Using OpenCVImage manipulation: Drawing

Same goes for cvCircle, cvLine, cvEllipse, ...

IplImage* img;if(img = cvLoadImage( “image.jpg”, CV_LOAD_IMAGE_COLOR)) != 0){        //draw a red rectangle from (20,20) to (100,100)

 cvRectangle(img, cvPoint(20,20), cvPoint(100,100),                CV_RGB(255,0,0));

    cvWaitKey(5); // very important, contains event                  // processing loop inside

}

Page 13: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 13

Using OpenCVImage manipulation: Color conversion

IplImage* img;IplImage* hsv_img;

if(img = cvLoadImage( “image.jpg”, CV_LOAD_IMAGE_COLOR)) != 0){        hsv_img = cvCreateImg(cvGetSize(img),8,3);    cvCvtColor(img, hsv_img, CV_BGR2HSV);

    cvWaitKey(5); // very important, contains event                  // processing loop inside

}

Page 14: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 14

Using OpenCVImage output: Windows

Windows are created and referenced by nameYou can use as many NamedWindows as you

like

CvCapture* capture = cvCaptureFromCAM(0);IplImage* frame = cvQueryFrame(capture)cvNamedWindow( "myWindow", CV_WINDOW_AUTOSIZE );

while(frame != 0){cvShowImage(“myWindow”, frame);cvWaitKey(5);frame = cvQueryFrame(capture);

}cvReleaseCapture(&capture);cvDestroyWindow(“myWindow”);

Page 15: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 15

Using OpenCVImage output: Saving

IplImage* img;if(img = cvLoadImage( “image.jpg”, CV_LOAD_IMAGE_COLOR)) != 0){       //draw a red rectangle from (20,20) to (100,100)

cvRectangle(img, cvPoint(20,20), cvPoint(100,100),                CV_RGB(255,0,0));

   cvWaitKey(5); // very important, contains event                 // processing loop inside

   cvSaveImage(“image.bmp”, img);

   return 0;}

Page 16: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 16

Using OpenCVInteractivity

CvCapture* capture = cvCaptureFromCAM(0);IplImage*  frame   = cvQueryFrame(capture);

cvNamedWindow( "myWindow", CV_WINDOW_AUTOSIZE );int key = 0;

while(frame != 0 && key != 'c'){cvShowImage(“mywindow”, frame);frame = cvQueryFrame(capture);key = cvWaitKey(250);

}cvReleaseCapture(&capture);cvDestroyWindow(“myWindow”);

[spot the error]

Page 17: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 17

OpenCV: CompilingIf you are compiling OpenCV from scratch,

don't forget to add ffmpeg support!To compile programs using OpenCV:

− Specify the include path:-I/usr/local/include/opencv

− Specify the library path:-L/usr/local/lib/

− (or wherever you installed it)− Tell the linker to use the shared libraries:-lcv -lcvaux -lhighgui

Page 18: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 18

OpenCV: CompilingCommon errors:

− “Undefined reference to...” when compiling/linking-> Make sure you have linked to the libs (-l...), and set

the library path correctly (-L...)

− “Undefined reference to...” when starting the programTell the shell how to find the shared libraries(Linux:) export LD_LIBRARY_PATH=/usr/local/lib

− (or wherever the missing libs are)

Page 19: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 19

OpenCV: ExamplesFind Circles in an Image:

− cvHoughCircles(...)

[Images by Thomas Hofhammer]

Page 20: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 20

OpenCV: ExamplesDetect Faces:

− cascade = (CvHaarClassifierCascade*) cvLoad( cascade_name, 0, 0, 0 );

− CvSeq* faces = cvHaarDetectObjects( small_img, cascade, storage,1.1, 2, 0, cvSize(30, 30) );

www.lenna.org

Page 21: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 21

See it liveA short demo

− Read and display a video stream− Paint circles− React to user input

Page 22: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 22

CvUtilsOpenCV is not complete

− Sometimes a little complicatedRemember pixel access?

− Some (utility) functions are just missing

“CvUtils” library (by me)− ImageUtils -> Mostly access and drawing− MathUtils -> Frequently used shortcuts− Pixel -> used to store coordinates along with color− Blob -> a collection of pixels− ParFile (by Stefan Hahne) -> Easy config file

access

Page 23: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 23

Starter KitShould accelerate the “getting started” partA bit more sophisticated than the demoMain file

− reads config file and command line

ControllerBasic input (video file or camera)Basic output (one main window)Error codesUses CvUtils

Page 24: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 24

Output LibraryShared library for outputUsed to format outputControls output level (no more searching for

those DEBUG messages)Automated loggingAutomatic display of function names (if desired)Thread-safeBacktrace support (for Linux/Unix)Silly name (niftout)

Page 25: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 25

EquipmentEquipment available for projects:

− Various IEEE1394 industrial cams Color or b/w, mostly 640x480Various lenses (wide angle and tele)

− Sony IEEE1394 zoom cams− Several USB webcams− 2 (3) PTZ cams− Tripods

Some other stuff (e.g. Stereo Cam) in the RoboCup lab

− Just ask them!

Page 26: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 26

CV-LabYou can work on your projects in the CV lab

− Get accounts on the lab server− Work on fast machines (some dual or quad core)− Multiple FireWire support

Use source control− There is a SVN repository on the lab server− Access via lab account− ->Ask!

Page 27: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 27

Project ProposalsContents Name(s) of Student(s)Student ID(s) (Matr. Nr.)Title of the CV project

(after approval of the instructor)Description of the CV project (plan of attack)

(max. 2 pages, incl. graphics)(kind of working contract between you and me)

References (min. 2 peer reviewed)of a project proposal

Page 28: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 28

Project ProposalsDescription of the CV project should contain:

− IntroductionWhat is the project about? What is the context of the

project? What is the Motivation?

− MaterialsWhat is the Equipment needed?What setup do you have in mind?

− MethodsHow are you going to achieve your goals? (CV methods,

not a “storyline”)

Page 29: CV_SS08_Lab_2

SS 2008 Tutorial/Lab I 29

Project ProposalsDescription of the CV project should contain

(cont'd):− Assumptions

What are the prerequisites for your project (e.g. lighting conditions, background, etc.)?

What are the restrictions on you project?What may be changed in the environment?

− Expected results− Distinction: who does what?