Matlab? Chapter 3-4 Matlab and IPT Basics. Working Environment. Matlab Demo. Array. Data Type. MATLAB Desktop:

Similar documents
Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Lecture 04 FUNCTIONS AND ARRAYS

CSCI 171 Chapter Outlines

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

EL2310 Scientific Programming

C Functions. 5.2 Program Modules in C

Fundamentals of Programming

A Fast Review of C Essentials Part I

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

ME 461 C review Session Fall 2009 S. Keres

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

Fundamental of Programming (C)

Introduction to Matlab

Functions. Systems Programming Concepts

JTSK Programming in C II C-Lab II. Lecture 3 & 4

Programming in C Quick Start! Biostatistics 615 Lecture 4

Introduction to C Language

Technical Questions. Q 1) What are the key features in C programming language?

Programming and Data Structures

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

6.096 Introduction to C++ January (IAP) 2009

C: How to Program. Week /Mar/05

ANSI C Programming Simple Programs

Lecture 3. More About C

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

BLM2031 Structured Programming. Zeyneb KURT

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

2. Numbers In, Numbers Out

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Programming. C++ Basics

Chapter 2 - Introduction to C Programming

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Reserved Words and Identifiers

Chapter 5 C Functions

EL6483: Brief Overview of C Programming Language

today cs3157-fall2002-sklar-lect05 1

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

A Crash Course in C. Steven Reeves

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Kurt Schmidt. October 30, 2018

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Functions in C C Programming and Software Tools

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Review of the C Programming Language for Principles of Operating Systems

CS2141 Software Development using C/C++ C++ Basics

Introduction to Matlab

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

Data Type Fall 2014 Jinkyu Jeong

Lecture 03 Bits, Bytes and Data Types

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Presented By : Gaurav Juneja

Programming. Elementary Concepts

Main differences with Java

H192 Midterm 1 Review. Tom Zajdel

ECE264 Spring 2014 Exam 2, March 11, 2014

UNIT- 3 Introduction to C++

2. Numbers In, Numbers Out

Lecture 02 C FUNDAMENTALS

C: How to Program. Week /Apr/23

Signals, Instruments, and Systems W2. C Programming (continued)

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Functions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Chapter 14 - Advanced C Topics

High Performance Programming Programming in C part 1

printf( Please enter another number: ); scanf( %d, &num2);

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

EC 413 Computer Organization

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

Structured Programming. Jon Macey

Procedures, Parameters, Values and Variables. Steven R. Bagley

Programming for Engineers Functions

CS201 Some Important Definitions

Computers in Engineering. Moving From Fortran to C Michael A. Hawker

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Work relative to other classes

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

C introduction: part 1

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

CS3157: Advanced Programming. Outline

Transcription:

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