Images and Programming

Slides:



Advertisements
Similar presentations
Creative Computing. \\ aims By the end of the session you will be able to: 1.Explain the difference between various image file formats 2.Load in and display.
Advertisements

Matlab Intro Simple introduction to some basic Matlab syntax. Declaration of a variable [ ] Matrices or vectors Some special (useful) syntax. Control statements.
Introduction to MATLAB The language of Technical Computing.
1 A L L A H. Command-Window Workspace & Directory Command- History The Matlab Command window - Finding your way around.
Introduction to Matlab
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
CS231A Matlab Tutorial Philip Lee Winter Overview  Goals › Introduction to Matlab › Matlab Snippets › Basic image manipulations › Helpful Matlab.
Introduction to MATLAB and image processing. MATLAB and images The help in MATLAB is very good, use it! An image in MATLAB is treated as a matrix Every.
MATLAB for Image Processing April 10 th, Outline Introduction to MATLAB –Basics & Examples Image Processing with MATLAB –Basics & Examples.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Images and MATLAB Source of images: Science&subcategory=Digital Image Processing&isbn=
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Data starts with width and height of image Then an array of pixel values (colors) The number of elements in this array is width times height Colors can.
Computational Tools for Image Processing Lecture 1, Jan 22nd, 2007 Part 2 (8:10-9:20pm) by Lexing Xie EE4830 Digital Image Processing
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU.
1 Perception, Illusion and VR HNRS 299, Spring 2008 Lecture 14 Introduction to Computer Graphics.
Digital Image: Representation & Processing (2/2) Lecture-3
Introduction to MATLAB
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Input, Output, and Processing
September 21, COMPUTER VISION WEB PAGE IS UP !! OR Simply go to computer science homepage.
Gulsah Tumuklu Ozyer MATLAB IMAGE PROCESSING TOOLBOX.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
Image Processing:Fundementals Lecture: Introduction –An image is digitized to convert it to a form which can be stored in a computer's memory or.
Information Processes and Technology Multimedia: Graphics.
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
CS112 Scientific Computation Department of Computer Science Wellesley College Numb3rs Number and image types.
© 2004 R. C. Gonzalez, R. E. Woods, and S. L. Eddins Digital Image Processing Using MATLAB ® Chapter 2 Fundamentals Chapter.
What Matlab can do for me? Matlab stands for MATrix LABoratory Matlab is a software package for high-performance numerical computation and visualization.
Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.
1 © 2010 Cengage Learning Engineering. All Rights Reserved. 1 Introduction to Digital Image Processing with MATLAB ® Asia Edition McAndrew ‧ Wang ‧ Tseng.
Graphics and Images Graphics and images are both non-textual information, that can be displayed and printed. These images may appear on screen as well.
Quiz # 1 Chapters 1,2, & 3.
Digital Image Processing Lecture4: Fundamentals. Digital Image Representation An image can be defined as a two- dimensional function, f(x,y), where x.
Multimedia Basics (1) Hongli Luo CEIT, IPFW. Topics r Image data type r Color Model : m RGB, CMY, CMYK, YUV, YIQ, YCbCr r Analog Video – NTSC, PAL r Digital.
MATLAB Lecture Two Tuesday 5 July Chapter 3.
Fundamentals of WWW Imaging Fort Collins, CO Copyright © XTR Systems, LLC Fundamentals of Web Imaging Instructor: Joseph DiVerdi, Ph.D., MBA.
Outline Introduction to MATLAB Image Processing with MATLAB
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
Introduction to MATLAB Ehsan Adeli M. Iran University of Science and Technology, E-Learing Center, Fall 2008 (1387)
Graphics and Image Data Representations 1. Q1 How images are represented in a computer system? 2.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
An Introduction to Programming in Matlab Emily Blumenthal
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
8th Lecture – Intro to Bitmap or Raster Images
Computer Application in Engineering Design
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
Digital Image Processing using MATLAB
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Fundamentals of Image Processing Digital Image Representation
Presentation transcript:

Images and Programming Basic MATLAB programming Grayscale Images RGB Images Indexed Color Images Data Types and Conversions Image Files and Formats

MATLAB Basic Use of MATLAB Where to find more helps? Textbook: Appendix A Where to find more helps? http://www.mathworks.com/access/helpdesk/help/toolbox/images/

Introduction to MATLAB MATLAB is a data analysis and visualization tool It has been designed with powerful support for matrices and matrix operations. It has excellent graphics capabilities and its own powerful programming language. There are sets of MATLAB programs designed to support particular tasks called toolboxes Image Processing Toolbox

Introduction to MATLAB MATLAB’s standard data type is the matrix, all data are considered to be matrices of some sort. Images are matrices whose elements are the gray values or RGB values of its pixels. A single value is considered to be a 1x1 matrix A string is a 1xn matrix of characters where n is the string’s length

Introduction to MATLAB When you start up MATLAB, the MATLAB desktop will appear as shown in the Figure. The prompt consists of two right arrows: >>

Basic Use of MATLAB >> 2+2 MATLAB is command line driven; all commands are entered by typing them after the prompt symbol. >> 2+2 ans = 4

Basic Use of MATLAB MATLAB does all its calculations internally to double precision. The default display format is to use only four decimal places. We can change this by using the format function. >> 11/7 1.5714 >> format long ans = 1.57142857142857 Entering the command format returns to the default format

Basic Use of MATLAB MATLAB has all the elementary mathematical functions built in: sqrt, sin, log, log10, pi, etc.

Variables and the Workspace Variables are used to store values. >> a = 5^(7/2) a = 279.5085 >> log(a^2)/log(5) ans = 7

Variables and the Workspace It lists all your currently defined variables, their numeric data types, and their sizes in bytes. The same information can be obtained using the whos function >> whos Name Size Bytes Class a 1x1 8 double array ans 1x1 8 double array Grand total is 2 elements using 16 bytes

Variables and the Workspace Workspace (cont) Note that ans is a variable. It is automatically created by MATLAB. A listing of the variable names is obtained using who function: >> who Your variables are a ans MATLAB’ standard data type for numbers is double, and they are stored as double-precision 8-byte values.

Dealing with Matrices MATLAB has an enormous number of commands for generating and manipulating matrices. We can use some of these commands to investigate aspects of the image. We can enter a small matrix by listing its elements row by row, using spaces or commas as delimiters for the elements in each row and semicolons to separate the rows, for example, >> a = [4 -2 -4 7;1 5 -3 2; 6 -8 -5 -6; -7 3 0 1]

Dealing with Matrices Matrix Elements >> a(2, 3) ans = -3 Matrix elements can be obtained by using the standard row, column-indexing scheme. >> a(2, 3) ans = -3 Returns the element of the matrix in row 2 and column 3. MATLAB allows matrix elements to be obtained using a single number; this number being the position where the matrix is written out as a single column. Thus, the order of elements in a is 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16 a(2,3) = a(10) = -3

Dealing with Matrices In general, for a matrix M with r rows and c columns, element m(i, j) corresponds to m(i + r*(j - 1)). To obtain a row of values, or block of values, we use MATLAB’s colon operator (:), for example, >> 2:3:16 ans = 2 5 8 11 14

Dealing with Matrices >> a(3,1:3) Apply to the matrix a, ans = 6 -8 -5 >> a(2:4,3) -3 -5 >> a(2:3,3:4) -3 2 -5 -6 >> a(3,:) 6 -8 -5 -6

Dealing with Matrices ans = -2 -5 -8 3 4 1 6 -7 . 7 2 -6

Dealing with Matrices Matrix Operations >> flipud(a) All the standard operations are supported (add, subtract, multiply, invert matrix, matrix power, transpose) >> 2*a-3*b >> a^3*b^4 >> inv(a) >> a’ MATLAB supports some geometric operations on matrices; flipud - flip a matrix up and down, fliplr - flip a matrix left and right., rot90 rotates a matrix by 90 degree. >> flipud(a) >> fliplr(a) >> rot90(a) ans = -7 3 0 -1 7 -4 -2 4 7 2 -6 1 6 -8 -5 -6 2 -3 5 1 -4 -3 -5 0 1 5 -3 2 -6 -5 -8 6 -2 5 -8 3 4 -2 -4 7 1 0 3 -7 4 1 6 -7

Dealing with Matrices >> reshape([1:20],5,4) ? The reshape function produces a matrix with elements taken column by column from the given matrix. >> c=[1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20] C = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 >> reshape(c,2,10) >> reshape(c,5,4) Reshape produces an error if the product of the two values is not equal to the number of elements of the matrix. >> reshape([1:20],5,4) ? >> reshape([1:20],5,4)’ ?

Dealing with Matrices Array and Matrix arithmetic operators Dot operator = Array operator Note: 1/0 return Inf

Dealing with Matrices Vectorization Special Matrices zeros(m,n) produces a zeros matrix of size m by n ones(m,n) produces an ones matrix pf size m by n Vectorization Refers to an operation carried out over entire matrix or vector >> tic, for i=1:10^6, sin(i); end, toc elapsed_time = 27.4969 >>tic, i=1:10^6; sin(i); toc elasped_time = 1.3522 Vectorized commands perform very quickly. They should be used instead of for loop.

Plots MATLAB has outstanding graphics capabilities. The plot function can be used to produce many different plots. >> plot(x,sin(x)) >> plot(x, sin(x), ‘.’, x, cos(x), ‘o’);

Help in MATLAB MATLAB comes with a vast amount of online help and information. To obtain information on a particular command, you can use help. >> help lookfor >> doc help >> lookfor exp

Programming in MATLAB MATLAB has a small set of built-in functions, others are written in MATLAB’s own programming language. We consider two distinct programs Script files Functions Script file It is a list of commands to be executed Function It takes an input (one or several variables) and returns one or several values.

MATLAB: Load Image Command: imread Syntax: X = imread(‘Filename.ext’); X : 8-bit/16-bit unsigned int matrix [X, MAP]= imread(‘Filename.ext’); MAP: color palette (Type: Double, Range: [0,1]) Supported format: BMP, GIF, JPEG, PBM, PCX, PMG, PNG, PNM, PPM, RAS, TIFF, …

MATLAB: Display Image Command: imshow Syntax (not completed): imshow(‘filename.ext’) imshow(X) imshow(X, MAP) Show pixel value by command pixval on. (Format: column, row = pixel)

MATLAB: Write Image Command: imwrite Syntax: imwrite(X, ‘filename.ext’) imwrite(X, MAP, ‘filename.ext’) imwrite(X, ‘filename’, format) imwrite(X, ‘filename.ext’, param1, value1, param2, value2, …)

Example: Load Grayscale Image >> w = imread(‘wombats.tif’) ; Press ENTER and finish.. What happened with this command? intensities of wombats.tif image is downloaded to matrix w. size of the matrix w = height  width No single quote (‘) if the filename is stored in variable

Example: Display Grayscale Image >> figure >> imshow(w) >> pixval on What happened with these commands? create figure window (figure command) display image inside matrix w (imshow command) show intensity of the pixel (pixval on command)

Example: Display Grayscale Image (cont) figure >> imshow(‘wombat.tif’) >> pixval on What happened with these commands? create figure window (figure command) display wombat.tif image (imshow command) show intensity of the pixel (pixval on command) No image stored in memory!!! >>

Example: Display Grayscale Image (cont) Figure 2.1 The wombats image with pixel on.

Example: Load RGB Image >> a = imread(‘lily.tif’) ; Press ENTER and finish.. What happened with this command? color of lily.tif image is downloaded to matrix a. size of the matrix a = height  width  3 >> size(a) ans = 186 230 3 index of a: a(row, column, page) page: 1 = R, 2 = G, 3 = B

RGB Color Cube (a) (b)

Example: Display Pixel Value >> impixel(a, 200, 100) What happened with this command? show pixel value of image a at column 200 and row 100. If a is the color image, show R G B values at position (100,200). If a is the grayscale image, show I I I where I is the intensity at position (100,200).

Example: Display Pixel Value >> inshow(a) >> impixel(a,88,137) ans = 162 170 182 >> a(100,200,2) >> a(100,200,1:3) >> a(100,200,:) Figure 2.2 The lily flower image with pixel on.

Example: Load Indexed Image >> em = imread(‘emu.tif’); >> imshow(em); Correct?

Example: Load Indexed Image >> em = imread(‘emu.tif’); We get something wrong. Why? em = index of emu.tif image relationship between index and color?

Example: Load Indexed Image >> [em, emap] = imread(‘emu.tif’); Press ENTER. What happened with this command? index of emu.tif image is downloaded to matrix em. size of the matrix em = height  width palette of emu.tif image is downloaded to matrix emap. size of the matrix emap = 256  3

Example: Display Indexed Image >> figure >> imshow(em, emap) >> pixval on What happened with these commands? create figure window (figure command) display color from index em with the palette emap (imshow command) show value of the pixel (pixval on command)

Example: Display Indexed Image

Example: Display Indexed Image Display without color map Display with color map

MATLAB: Image Information Command: imfinfo Syntax: imfinfo(‘filename.ext’) Information shown: Filename, FileModDate, FileSize, Format, FormatVersion, Width, Height, BitDepth(no of bits per pixel), ColorType (truecolor, grayscale, or indexed), etc

MATLAB: Data Types Data Type Description Range int8 8-bit integer -128 to 127 uint8 8-bit unsigned integer 0 to 255 int16 16-bit integer -32768 to 32767 uint16 16-bit unsigned integer 0 to 65535 double Double precision real number machine specific Note: Arithmetic operation allowed only for the data type double.

MATLAB: Data Conversion >> b = uint8(a); >> b b = 23 >> whos a b Name Size Bytes Class a 1x1 8 double array b 1x1 1 uint8 array Don’t forget to convert the image to double data before performing any computations.

MATLAB: Image Conversion Function Use Format ind2gray indexedgrayscale y = ind2gray(x,map); gray2ind grayscaleindexed [y,map] =gray2int(x); rgb2gray RGBgrayscale y = rgb2gray(x); gray2rgb grayscaleRGB x = gray2rgb(y); rgb2ind RGBindexed [x,map] =rgb2int(y); ind2rgb indexedRGB y = ind2rgb(x,map);

Vector Image VS Raster Image Image = collection of line Scale up without loss of sharpness Not suitable for natural screen E.g. Adobe PostScript Image = collection of point Popular for image files Recommended for image processing E.g. PGM, BMP, JPEG, …

Microsoft BMP Format Header (RAW format, unreadable) BM………. Header format (54 bytes) File header: (Bytes 0-13) Byte 0-1 Signature BM (42 4D) Byte 2-5 FileSize File size in Byte Byte 6-9 Reserved All zeros Byte 10-13 DataOffset File offset to raster data

Microsoft BMP Format Information header (Byte 14-53) Byte 14-17 Size Size of information header Byte 18-21 Width Image width [pixel] Byte 22-25 Height Image height [pixel] Byte 26-27 Planes Number of image plane (=1) Byte 28-29 BitCount Bit/Pixel (1, 4, 8, 16, 24) Byte 30-33 Compression Type of compression 0 : no compression, 1, 2: RLE encoding (rarely used)

Microsoft BMP Format Byte 34-37 ImageSize Image size Byte 38-41 HorizontalRes Horizontal resolution [pixel/meter] Byte 42-45 VerticalRes Vertical resolution [pixel/meter] Byte 46-49 ColorsUsed #color used in the image (0=allcolor,2BitCount) Byte 50-53 ImportantColors #important color

Number Format in BMP Format: Least-endian (little-endian) LSB on the lowest address Ex. Byte 18-21 from BMP file (width): 42 00 00 00  actual number 00 00 00 42

GIF Format Data are compressed by using LZW (Lempel-Ziv-Welch) compression (lossless compression) Allowed a maximum 256 colors Not allowed for grayscale/binary images Allow multiple images in one file (can create animated GIFs) It is one of the standard formats supported by the WWW Header signature (3 bytes): GIF version (3 bytes): 87a or 89a

GIF Format Alternative format: PNG screen width (2 bytes) : unsigned screen height (2 bytes) : unsigned etc. Alternative format: PNG non-patented algorithm (zlib compression) also support binary, grayscale, truecolor and indexed images possible to include alpha channel, gamma correction, …

JPEG Format Lossy compression Header: Byte 0-1 Signature FF D8 (HEX) Byte 2-3 Application Maker FF E0 (HEX) Byte 4-5 Segment length Byte 5-10 JFIF\ 0 ASCII JFIF. Byte 11-12 JFIF version Version Byte 13 Units 0: arbitrary, 1: pixel/inch, 2: pixel/cm.

JPEG Format Byte 14-15 Horizontal pixel density Byte 16-17 Vertical pixel density Byte 18 Thumbnail width 0 : no thumbnail Byte 19 Thumbnail height 0 : no thumbnail

TIFF Format Be able to store multiple image files Allow different compression routines (LZW, JPEG, Huffman, RLE…) Allow different byte ordering (little or big-endian) Allow binary, grayscale, indexed, truecolor images Good for data exchange

TIFF Format Header: 8 bytes only Byte 0-1 Byte order 4D 4D: ASCII MM for big endian 49 49: ASCII II for little endian Byte 2-3 TIFF version Always 42 (00 2A: big endian, 2A 00: little endian) Byte 4-8 Image offset Pointer to the position of the data for the first image

DICOM Format Format of medical images Able to store multiple images Allow for describing 3D images Header (Very long and variable length!!): Byte 0-127: preamble (not used) Byte 128-131: signature (DICM)

DICOM Format Info in Header image information (size, #slices, modality used: CAT, MRI, …) patient information (name, patient ID, …) compression used (JPEG, RLE, …) Complex!!! 

Example of MATLAB function function dumphex(filename, n) % % DUMPHEX(FILENAME, N) prints the first 16*N bytes of the file FILENAME % in hex and ASCII. For example: % dumphex('picture.bmp’, 4) fid = fopen(filename,'r'); if fid == -1 error('File does not exist or is not in your Matlab path'); end; a=fread(fid,16*n,'uchar'); idx=find(a>=32 & a <=126); ah=dec2hex(a); b=repmat([' '], 16*n, 3); b2=repmat('.', 16, n); b2(idx)=char(a(idx)); b(:,1:2)=ah; [reshape(b', 48, n)' repmat(' ', n, 2) reshape(b2, 16, n)']