Matlab? Lecture Slides ME 4060 Machine Vision and Vision-based Control Chapter 3-4 Matlab and IPT Basics By Dr. Debao Zhou 1 MATric LABoratory data analysis, prototype and visualization Matrix operation Graphical capability High level programming environment Because of its image processing toolbox Easy data operation, display and explanation Integrated image processing tools Once you understanding Matlab, it will be easier for you to use C++ and OpenCV my goal Using C++/Matlab will help you understand the image processing 2 Working Environment MATLAB Desktop: Command Window, Workspace Browser, Current Directory Window, Command History Window, and Figure Windows Matlab Editor Help System Matlab Demo Matrix/array and image display A = [1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1]; B = [1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1]*0.5; imshow (A); Imshow(B); As = Imread( tire.tif ) Imshow(As); 3 4 Data Type Array A(1), f(1,1) zeros(m,n) ones(m,n) true(m,n) false(m,n) eye(m,n) rand(m,n) 5 6 1
Script and Functions Operators Command line and functions m- files Function definition Help line: first line after function definition with % Function body Comments: % 7 8 Matrix Operations 9 10 Flow control: if else elseif switch for and while loop with break and continue Graphics 11 12 2
Image Processing Toolbox Spatial transformations (Chapter 7) Image analysis and enhancement (Chapters 8 12, 14 15, and 18) Neighborhood and block operations (Chapter 10) Linear filtering and filter design (Chapters 10 and 11) Mathematical transforms (Chapters 11 and 17) Deblurring (Chapter 12) Morphological operations (Chapter 13) Color image processing (Chapter 16) Using type function_name to see the code Demo imtool imread imshow imwrite pixval impixelinfo imageinfo imcontrast image imagesc 13 14 Tutorials 1 and 2 Lecture Slides ME 4060 Machine Vision and Vision-based Control Chapter 3-4 Visual C++ and OpenCV By Dr. Debao Zhou 15 16 Types Float and Double Float 6 digit precision Double 12 digit precision Precision is number of digits sizeof() returns size of a variable in bytes Qualifiers and Casting Type Qualifiers const variable cannot be modified volatile prevents compiler from optimizing around variable Type Casting Allows conversion of variable types float test = (float)5 3
Operators Arithmetic +, -, *, /, % (modulus) ++, -- (prefix and postfix incrementation) +=, -=, *=, /=, %= Logical &&,, ==,!= Bit Shifting <<, >>,, &, ^ Standard I/O Basic Output printf() printf( An integer is %d\n, int var); d/i = int, f = floating point, e = exponential notation, c = single char, s = string, o = octal, x/x = hex, u = unsigned int Basic Input scanf() Logical Operators Equals (Equivalence) == Non Equivalence!= Less Than < Less Than or Equal To <= Greater Than or Equal To >= Logical Operators Logical AND && 0 1 0 0 0 1 0 1 Logical OR 0 1 0 0 1 1 1 1 If Statement Do and While if (conditional){ else if(conditional){ else{ while(conditional){ do{ while(conditional) What s the difference? 4
For Loop for(int i=0; i<5; i++){ Here we are starting at 0 and incrementing until i is 4. for(int test=52; test>=6; test/=2){ A less typical example. Chars and ASCII Puts the ASCII value of a char in the variable char test = '5' Printing with %d gives the numeric value, %c gives the character Reading Input Reading input uses scanf with a format like printf scanf("%f,%f,%c", &in1, &in2, &in3); This will load floats into in1 and in2, and a char into in3 A number can be placed between the % and variable letter, to indicate how many characters will be read Logical Operators Equals (Equivalence) == Non Equivalence!= Less Than < Less Than or Equal To <= Greater Than or Equal To >= Logical Operators Logical AND && 0 1 0 0 0 1 0 1 Logical OR 0 1 0 0 1 1 1 1 If Statement if (conditional){ else if(conditional){ else{ 5
Do and While while(conditional){ do{ while(conditional) What s the difference? For Loop for(int i=0; i<5; i++){ Here we are starting at 0 and incrementing until i is 4. for(int test=52; test>=6; test/=2){ A less typical example. Chars and ASCII Puts the ASCII value of a char in the variable char test = '5' Printing with %d gives the numeric value, %c gives the character Reading Input Reading input uses scanf with a format like printf scanf("%f,%f,%c", &in1, &in2, &in3); This will load floats into in1 and in2, and a char into in3 A number can be placed between the % and variable letter, to indicate how many characters will be read Switch Statement switch(case){ label: code break; label2: code break; default: code Function Basics return type name (argument list) return type void (for none) or int, char, double, etc name starts with a letter, no special characters or keyword like a variable argument list int input, double input2, char input3 etc 6
Function Prototype Same as function declaration, just no contents Goes before main int functionname(int input); void main(int input2){ int var = functionname(4.0); int functionname(int input){ return input*2; Pass by Value / Reference By Value normal method, does not alter variable contents By Reference passes memory location int function(int *refvar){ (*refvar) +=2; Recursion Functions that call themselves Fibonacci sequence F(n) = F(n-1) + F(n-2) 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 Math #include <math.h> sin(angle); cos(angle); log(natural); log10(base10); pow(base, exponent); sqrt(input); more Pointer Basics Declare a pointer: type *name; Note: position of star does not matter so long as it s between type and name To point to a normal variable use address of operator -- & int normal = 5; int * p = &normal; Pointer Usage *p references the value at p p is the address stored in p, aka the address of the variable that p points to Be careful when using operators on pointers! *p++ is the value at the next address after p (*p)++ is the value p points to plus 1 7
Arrays Declaring an Array: type name[size]; //Values for each type name[] = {element1, element2, etc; type name[size] = {size must match #elements; Array Access Arrays are zero-indexed first element has the 0 subscript a[0] first element of array a[x] a[4] last element of a five element array a[5] for the array - int a[5] the syntax a[6] will access memory out of bounds Array Pointer Access int a[5] = {0, 1, 2, 3, 4; int *p = a (is the same as) int *p = &a[0] p++ is then &a[1] In other words, arrays are stored sequentially in memory Strings char * a = A string char a[] = A character array char *a = new char a[x] Strings are fixed once declared, the contents of the string cannot be changed The pointer can be targeted to another string, however A char array can be changed Structures struct name{ type name; type2 name2; instance_name = {name_val, name2_val; Struct Example struct point{ int x; int y; ; void move_it(struct point *source, struct point* dest){ source->x = dest->x; source->y = dest->y; 8
Struct Example int main() { struct point location = {2, 3; struct point move_to = {-1, -1; move_it(location, move_to); typedef typedef oldtypename newtypename; typedef struct newtypename{ struct contents; newtypename; newtypename varname = value; Common Libraries stdbool.h Provides: true constant = 1 false constant = 0 bool type Use with logical operators Random Numbers Random numbers are pseudorandom - there is always an algorithm behind the generation #include <stdlib.h> (gives you the rand() and srand() functions) #include <time.h> (gives you the time() function) Random Numbers srand( int ) set the seed number used as basis for sequence of random numbers same seed = same sequence of numbers default seed is 1 For more random randomness set the seed by: srand( time(null) ) rand() returns in the range from 0 to RAND_MAX Making A Library - Header Header File (filename.h) Start with: #ifndef _ID_ #define _ID_ Add prototypes for all functions in filename.c If you have type definitions put them in the header End with #endif 9
Making A Library - Source Source File (filename.c) Start with: #ifndef _ID_ #define _ID_ Add all function definitions (not prototypes) to the middle End with #endif Problem Solving Identify what data you need, and how to get it from the user, and how to convey the end result Break the processing down into simple steps Write code bit by bit, test as you go with printf diagnostic statements If something needs to be done many times, convert it into a function, or do it all at once in a loop File IO #include <fstream> #include <iostream> #include <stdlib.h> #include <stdio.h> // Read FILE *bmpinput = fopen(filename, "rb"); // Write FILE *bmpoutput = fopen(filename, "wb"); File IO fopen flags r - open for reading. w - open for writing. a - open for appending. r+ - open for both reading and writing. The stream will be positioned at the beginning of the file. w+ - open for both reading and writing. The stream will be created if it does not exist, and will be truncated if it does exist. a+ - open for both reading and writing. The stream will be positioned at the end of the existing file content. rb - open for reading. The 'b' indicates binary data (as opposed to text); by default, this will be a sequential file in Media 4 format. wb - open for writing. The 'b' indicates binary data. ab - open for appending. The 'b' indicates binary data. File IO OpenCV Environment Settings Seeking within a file: fseek(bmpinput, 54, SEEK_SET); Reading 1 byte from a file, value is in *pchar unsigned char *pchar; fread(pchar, sizeof(char), 1, bmpinput); Writing to a file fwrite(pchar, sizeof(char), 1, bmpoutput); 60 10
OpenCV Environment Settings OpenCV Environment Settings 61 62 OpenCV Environment Settings VC++ Directories Executable Directories C:\UMD\OpenCV-2.1.0\vs2010\bin;$(ExecutablePath) Include Directories C:\UMD\OpenCV-2.1.0\vs2010\include\opencv;$(IncludePath) Library directories C:\UMD\OpenCV-2.1.0\vs2010\lib;$(LibraryPath) Source directories C:\UMD\OpenCV-2.1.0\vs2010\src\cvaux;C:\UMD\OpenCV- 2.1.0\vs2010\src\cv;C:\UMD\OpenCV-2.1.0\vs2010\src\ml;C:\UMD\OpenCV- 2.1.0\vs2010\src\highgui;C:\UMD\OpenCV- 2.1.0\vs2010\src\cxcore;$(SourcePath) Linker Input C:\UMD\OpenCV-2.1.0\vs2010\src\cvaux;C:\UMD\OpenCV- 2.1.0\vs2010\src\cv;C:\UMD\OpenCV-2.1.0\vs2010\src\ml;C:\UMD\OpenCV- 2.1.0\vs2010\src\highgui;C:\UMD\OpenCV- 2.1.0\vs2010\src\cxcore;$(SourcePath) 63 11