Top Banner
1 Hebrew University Image Processing - 2006 Digital Image Processing • Teacher Assistance: Yael Pritch course email : [email protected] personal email : [email protected] • Reception hour: Thursday 14:00-15:00 • Newsgroup: local.course.impr • Web site: www.cs.huji.ac.il/~impr Hebrew University Image Processing - 2006 Today Outline • Matlab Basics • Intensity transform and Histogram Equalization • Exercise one – Basic Image Processing Hebrew University Image Processing - 2006 Matlab Desktop to operate Matlab : type matlab or matlab7 from linux xterm Hebrew University Image Processing - 2006 Getting Help >> help help – explain how to get help >> helpbrowser – open the online Matlab help browser with “browser” interface >> help images– list of all commands in the Image Processing Toolbox >> demo >> lookfor read (display list of function with ‘read’ in the name or help first line text) >> type imread >> help imread (function name + % block) >> doc imread (function documentation in help browser) >> mathworks website: http://www.mathworks.com/products/matlab Hebrew University Image Processing - 2006 Matlab Basics • Digital image representation : 2D function f(x,y) -> finite discrete quantities • Coordinate Conventions img(r,c) r – rows (height) c – cols (width) The first pixel: img(1,1) Color/Gray Hebrew University Image Processing - 2006 Vector indexing row vector (1xN) >> v = [1 3 5 7]; (elements separated by space or comma (,)) >> v(2) = 3; column vector (MX1) >> w = [1;3;5;7]; (elements separated semi-comma (;)) >> w = v’ (transpose operation) w = 1 3 5 7 To Access blocks of elements we use colon notation >> v(2:4) ans = 3 5 7 >> v(1:end) end is the last element in the vector >> v(:) produce a column vector >> v(1:2:end) enables steps (jumps) >> v(end:-2:1) steps can be negative as well Vector can be used as an index into another vector >> v([1 3 4]) ans = 1 5 7
10

Hebrew University Image Processing - 2006 Today Outline Digital Image Processing · 2006. 10. 26. · Hebrew University Image Processing - 2006 Matlab Basics •Digital image representation

Sep 10, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Hebrew University Image Processing - 2006 Today Outline Digital Image Processing · 2006. 10. 26. · Hebrew University Image Processing - 2006 Matlab Basics •Digital image representation

1

Hebrew University Image Processing - 2006

Digital Image Processing

• Teacher Assistance: Yael Pritch

course email : [email protected] email : [email protected]

• Reception hour: Thursday 14:00-15:00

• Newsgroup: local.course.impr

• Web site: www.cs.huji.ac.il/~impr

Hebrew University Image Processing - 2006

Today Outline

• Matlab Basics

• Intensity transform and Histogram Equalization

• Exercise one – Basic Image Processing

Hebrew University Image Processing - 2006

Matlab Desktopto operate Matlab : type matlab or matlab7 from linux xterm

Hebrew University Image Processing - 2006

Getting Help

>> help help – explain how to get help

>> helpbrowser – open the online Matlab help browser with “browser”interface

>> help images– list of all commands in the Image Processing Toolbox

>> demo

>> lookfor read (display list of function with ‘read’ in the name or help first line text)

>> type imread>> help imread (function name + % block)

>> doc imread (function documentation in help browser)

>> mathworks website: http://www.mathworks.com/products/matlab

Hebrew University Image Processing - 2006

Matlab Basics

• Digital image representation : 2D function f(x,y) -> finite discrete quantities

• Coordinate Conventionsimg(r,c)r – rows (height)c – cols (width)

• The first pixel:img(1,1)

• Color/Gray

Hebrew University Image Processing - 2006

Vector indexing• row vector (1xN)

>> v = [1 3 5 7]; (elements separated by space or comma (,))>> v(2) = 3;

• column vector (MX1)>> w = [1;3;5;7]; (elements separated semi-comma (;))>> w = v’ (transpose operation)w =

1357

• To Access blocks of elements we use colon notation>> v(2:4)ans =

3 5 7>> v(1:end) end is the last element in the vector>> v(:) produce a column vector>> v(1:2:end) enables steps (jumps)>> v(end:-2:1) steps can be negative as well

• Vector can be used as an index into another vector>> v([1 3 4])ans =1 5 7

Page 2: Hebrew University Image Processing - 2006 Today Outline Digital Image Processing · 2006. 10. 26. · Hebrew University Image Processing - 2006 Matlab Basics •Digital image representation

2

Hebrew University Image Processing - 2006

Matrix indexing

• Image – 2D array, matrix• Matrix can be represented as a sequence of row vectors

>>A = [1 2 3; 4 5 6; 7 8 9]A =

1 2 34 5 67 8 9

• To access element 2 indexes are used – row index and column index>> A(2,3) � 6>> A(:,3)

36

9>> A(2,:)

4 5 6>> a(1:2,1:3)

1 2 34 5 6

>> B = A;>> B(:,3) = 0B =

1 2 04 5 0

7 8 0

• Using vectors to index into a matrix provide a powerful tool for element selectionA([1 3], [2 3])

2 38 9

Hebrew University Image Processing - 2006

Matrix Addressing

• A very useful approach is to use logical matrix as an index to the matrix>> D = logical([1 0 0; 0 0 1; 0 0 0])

D =

1 0 0

0 0 1

0 0 0

>> A(D)

ans =

1

6

• The use of column operation on a matrix produce a single column vector from the matrix (on a column by column basis). It is veryuseful for image operations like sum or max

>> s = sum(f(:)) (equivalent to: sum(sum(f)))

Hebrew University Image Processing - 2006

Examples - Matrix indexing

>> i = imread('sowrds0040.bmp');

>> i = rgb2gray(double(i)/255);

>> imshow(i)

Hebrew University Image Processing - 2006

Examples - Matrix indexing

>> i = imread('sowrds0040.bmp');

>> i = rgb2gray(double(i)/255);

>> imshow(i)

Hebrew University Image Processing - 2006

Examples - Matrix indexing

>> i = imread('sowrds0040.bmp');

>> i = rgb2gray(double(i)/255);

>> imshow(i)

>> s = i(end:-1:1,:);

>> imshow(s);

Hebrew University Image Processing - 2006

Examples - Matrix indexing

>> i = imread('sowrds0040.bmp');

>> i = rgb2gray(double(i)/255);

>> imshow(i)

>> s = i(end:-1:1,:);

>> imshow(s);

Page 3: Hebrew University Image Processing - 2006 Today Outline Digital Image Processing · 2006. 10. 26. · Hebrew University Image Processing - 2006 Matlab Basics •Digital image representation

3

Hebrew University Image Processing - 2006

Examples - Matrix indexing

>> i = imread('sowrds0040.bmp');

>> i = rgb2gray(double(i)/255);

>> imshow(i)

>> s = i(end:-1:1,:);

>> imshow(s);

>> a = i(200:300,200:400);

>> imshow(a);

Hebrew University Image Processing - 2006

Examples - Matrix indexing

>> i = imread('sowrds0040.bmp');

>> i = rgb2gray(double(i)/255);

>> imshow(i)

>> s = i(end:-1:1,:);

>> imshow(s);

>> a = i(200:300,200:400);

>> imshow(a);

Hebrew University Image Processing - 2006

Examples - Matrix indexing

>> i = imread('sowrds0040.bmp');

>> i = rgb2gray(double(i)/255);

>> imshow(i)

>> s = i(end:-1:1,:);

>> imshow(s);

>> a = i(200:300,200:400);

>> imshow(a);

>> t = i(1:2:end, 1:2:end);

>> imshow(t);

Hebrew University Image Processing - 2006

Examples - Matrix indexing

>> i = imread('sowrds0040.bmp');

>> i = rgb2gray(double(i)/255);

>> imshow(i)

>> s = i(end:-1:1,:);

>> imshow(s);

>> a = i(200:300,200:400);

>> imshow(a);

>> t = i(1:2:end, 1:2:end);

>> imshow(t);

Hebrew University Image Processing - 2006

Examples - Matrix indexing

>> i = imread('sowrds0040.bmp');

>> i = rgb2gray(double(i)/255);

>> imshow(i)

>> s = i(end:-1:1,:);

>> imshow(s);

>> a = i(200:300,200:400);

>> imshow(a);

>> t = i(1:2:end, 1:2:end);

>> imshow(t);

>> i(200:300, 200:400) = 0;

>> imshow(i);

Hebrew University Image Processing - 2006

Examples - Matrix indexing

>> i = imread('sowrds0040.bmp');

>> i = rgb2gray(double(i)/255);

>> imshow(i)

>> s = i(end:-1:1,:);

>> imshow(s);

>> a = i(200:300,200:400);

>> imshow(a);

>> t = i(1:2:end, 1:2:end);

>> imshow(t);

>> i(200:300, 200:400) = 0;

>> imshow(i);

Page 4: Hebrew University Image Processing - 2006 Today Outline Digital Image Processing · 2006. 10. 26. · Hebrew University Image Processing - 2006 Matlab Basics •Digital image representation

4

Hebrew University Image Processing - 2006

Examples - Matrix indexing

>> i = imread('sowrds0040.bmp');

>> i = rgb2gray(double(i)/255);

>> imshow(i)

>> s = i(end:-1:1,:);

>> imshow(s);

>> a = i(200:300,200:400);

>> imshow(a);

>> t = i(1:2:end, 1:2:end);

>> imshow(t);

>> i(200:300, 200:400) = 0;

>> imshow(i);

>> imshow(i/2);

Hebrew University Image Processing - 2006

Examples - Matrix indexing

>> i = imread('sowrds0040.bmp');

>> i = rgb2gray(double(i)/255);

>> imshow(i)

>> s = i(end:-1:1,:);

>> imshow(s);

>> a = i(200:300,200:400);

>> imshow(a);

>> t = i(1:2:end, 1:2:end);

>> imshow(t);

>> i(200:300, 200:400) = 0;

>> imshow(i);

>> imshow(i/2);

Hebrew University Image Processing - 2006

Examples - Matrix indexing

>> i = imread('sowrds0040.bmp');

>> i = rgb2gray(double(i)/255);

>> imshow(i)

>> s = i(end:-1:1,:);

>> imshow(s);

>> a = i(200:300,200:400);

>> imshow(a);

>> t = i(1:2:end, 1:2:end);

>> imshow(t);

>> i(200:300, 200:400) = 0;

>> imshow(i);

>> imshow(i/2);

>> imshow((i>0.8).*i);

Hebrew University Image Processing - 2006

Examples - Matrix indexing

>> i = imread('sowrds0040.bmp');

>> i = rgb2gray(double(i)/255);

>> imshow(i)

>> s = i(end:-1:1,:);

>> imshow(s);

>> a = i(200:300,200:400);

>> imshow(a);

>> t = i(1:2:end, 1:2:end);

>> imshow(t);

>> i(200:300, 200:400) = 0;

>> imshow(i);

>> imshow(i/2);

>> imshow((i>0.8).*i);

Hebrew University Image Processing - 2006

Array dimensions

• Matlab arrays can be of any dimensions

• It is useful to operate on specific dimension of the array, for example:

>> height = size(i,1);

• Usually we deal with 2D array but there are cases (such as color images) we need to address higher dimensions>> i(200:300, 200:400,3)

• To get the number of dimensions of an array >> d = ndims(f)

Hebrew University Image Processing - 2006

Standard Arrays

• Generating simple array enables trying out simple ideas and test the syntax of a function

during development

>> zeros(m,n)>> ones(m,n)>> true(m,n)>> false(m,n)>> magic(m) >> rand(n)>> randn(n)

Page 5: Hebrew University Image Processing - 2006 Today Outline Digital Image Processing · 2006. 10. 26. · Hebrew University Image Processing - 2006 Matlab Basics •Digital image representation

5

Hebrew University Image Processing - 2006

M-Function Programming

• M-files can be scripts of functions that accept input argument and produce one or more outputs.

Components of m files:• Function definition line

function [out1 out2] = name(in1, in2, in3)

• H1 line - a single comment line that follows the function definition line. % SQUARESUM compute the sum of the square of the matrix elementsit is the first text appears when user write>> help function_name>> lookfor keyword display all functions where the keyword appeared in H1 line

• Help Text - text block following the H1 line without any blank line in between the two

• Function body – the Matlab code• Comments – lines starting with % (note – add short and clear

comments to your code)

Hebrew University Image Processing - 2006

function [p, pmax, pmin, pn] = improd(f,g)% improd computes the product of two images% [p, pmax, pmin, pn] = improd(f,g) output the element by element product % of two input images, f ang g, the product maximum and minimum values % and a normalized product array with values in the range [0,1].

% The input images must be of the same size. They can be of the class % uint8, unit16 or double. The outputs are of class double

fd = double(f);gd = double(g)p = fd.*gd;pmax = max(p(:));pmin = min(p(:));% comments starts with %pn = mat2gray(p);

M-Function Programming>> edit test.m

Hebrew University Image Processing - 2006

Operators

• Arithmetic operators (numeric computations)– matrix arithmetic (linear algebra A*B)

– array arithmetic (element by element A.*B) +, -, ./, .^,:..

• Relational operators (compare)– Compare corresponding elements of arrays of equal dimensions (<, >,<=, >=, ==, ~=) or an array to scalar

• Logical operators can operate both on logical and numeric data (and: &, or: |, not: ~)true: logical 1 or non-zero numeric quantity

false: logical or numerical 0

logical functions : xor, any, all

Hebrew University Image Processing - 2006

Flow control

• if, else, elseif, end

• switch, case, otherwise, end

• return

• try,..catch

• for i=start:increment:end, end

• while, end

• break (used with for or while)

• continue (used with for or while)

Try not to use

Hebrew University Image Processing - 2006

Code optimization –vectorizing loops

• Convert for / while loops to equivalent vector or matrix operations1D indexing

>> for x = 1:k

ff(x) = 5*sin((x-1)/(2*pi));

end

• 2D indexingTry to avoid 2D loops

>> x = 0:k-1>> ff = 5*sin(x/(2*pi));

Hebrew University Image Processing - 2006

Code optimization –vectorizing loops

• 2D indexingmeshgrid – convert rows vectors to arrays C and R that can be used for evaluating function with two variables

>> for r = 1:10

>> for c = 1:10

>> b(r,c) = r.^2+ c.^2

>> end

>> end

Vectorzing code accelerates the computation significantlyFor Example: compute 2D sin using meshgrid runs on the order of 30 times faster the same computation based on

loops on Image of 512x512 pixels

>> [C, R] = meshgrid(1:c, 1:r)>> h = R.^2 + C.^2;

Page 6: Hebrew University Image Processing - 2006 Today Outline Digital Image Processing · 2006. 10. 26. · Hebrew University Image Processing - 2006 Matlab Basics •Digital image representation

6

Hebrew University Image Processing - 2006

Code Optimization –Preallocating arrays

• Simple way to improve code execution is to pre-allocate the size of the arrays in the program.The preallocation also help reduce memory fragmentation when working with large matrixes

>> f = zeros(1024);

Hebrew University Image Processing - 2006

Cell arrays and Structures

• Cell array is multidimensional array whose elements are copies of other arrays>> c = {‘gauss’,[1 0;0 1], 3}>> c{1}ans =

gauss

• Structures are similar to cell arrays (allow grouping of a collection of dissimilar data) but they addressed by fields rather than by numbers>> params.nimgs = 100;>> params.jump = 2;>> params.baseStr = ‘testImg’

Hebrew University Image Processing - 2006

Arguments

• Matlab arguments are always passed by value

• Checking whether an argument exist>> exist(a,’var’)

• Checking number of arguments to the functions>> nargin, nargout, nargchk

• Getting variable number of arguments>>varargin, varargout

Hebrew University Image Processing - 2006

Reading Images

>> f = imread(‘filename’);filename is a string include the file type (jpg, tiff, bmp, gif ,…)

; is used for suppressing output

>>[height, width] = size(f);

>> whos f display additional information about an arrayName Size Bytes Class

f 512x512x3 786432 uint8 array

Grand total is 786432 elements using 786432 byte

Hebrew University Image Processing - 2006

Displaying Images>> imshow(f, G)

f is an image arrayG is the number of intensity level used to display it (default 256)

>> imshow(f, [low high])display as black all values less than low and as white all values greater or equal to high (in grayscale images)

>> imshow(f, [])set low and high as the minimal and maximal values of array f

useful for low dynamic range images or that have negative values

>> pixvaldisplay intensity value of individual pixel interactively

>> figure(2), imshow(g)Open a new figure before displaying the image (the default – using the same figure)

Hebrew University Image Processing - 2006

Writing Images

>> imwrite(f, ‘filename’)f is an image array‘filename’ must include the file format (tif, jpg, bmp,..)

>> k = imfinfo(‘test1.jpg’)Filename: 'test1.jpg'

FileModDate: '22-Oct-2005 13:07:36'

FileSize: 3464

Format: 'jpg'

FormatVersion: ''

Width: 256

Height: 256

BitDepth: 24

ColorType: 'truecolor'

FormatSignature: ''

Comment: {}

The answer is a structure variable with different fields: k.Width

Page 7: Hebrew University Image Processing - 2006 Today Outline Digital Image Processing · 2006. 10. 26. · Hebrew University Image Processing - 2006 Matlab Basics •Digital image representation

7

Hebrew University Image Processing - 2006

Data Classes - representing pixel values

Converting between types : B = data_class_name(A)for example: B = double(A)

Reading 8 bit images

logical operations

Hebrew University Image Processing - 2006

Image Types

• Intensity imagesscaled to represent intensities (uint8 – [0,255], double [0,1])

• Binary imageslogical array of 0s and 1s

• Indexed imagesLook up table [x, map]

• RGB imagestruecolor, array of (m*n*3)

Checking the image type : isind, isbw, isgray, isrgbConverting image types: rgb2ind, rgb2gray, gray2ind, ind2gray,….

Hebrew University Image Processing - 2006

Conversions

• When converting between data classes and types it is important to keep the value range for each data class

>> img = double(img)/255;>> img = im2double(img);

Hebrew University Image Processing - 2006

Gui

>> guide (Graphic User Interface Development Environment) Start the GUI Layout Editor. Guide create

– fig file: complete description of the gui elements and their arrangements

– gui m-file: the code that controls the gui operations, initializations functions, callback functions

Hebrew University Image Processing - 2006

Intensity Transformation

Hebrew University Image Processing - 2006

Intensity Transformation

• Spatial Domain – image plane itselfdirect manipulation of pixels in the image.

g(x,y) = T(f(x,y)

• T is an operator of f, defined over specific neighborhood about a pixel.

• Principle approach – use a square around the pixel. In the simplest case the square size is one pixel

Page 8: Hebrew University Image Processing - 2006 Today Outline Digital Image Processing · 2006. 10. 26. · Hebrew University Image Processing - 2006 Matlab Basics •Digital image representation

8

Hebrew University Image Processing - 2006

Intensity Transformation function

• In the case of single pixel transformation – we can use a lookup table

s = T(r)

0

1

2

3

.

.

.

.

.

.

.

.

255

s1

s0

s2

s3

s255

r

s

Dark light

Dar

k

light

r – gray level of the input images – gray level of the output image

Hebrew University Image Processing - 2006

Simple Examples for Intensity Transformations

• Image negative

Hebrew University Image Processing - 2006

Simple Examples for Intensity Transformations

• Image negatives = L-1-r

r

s

Dark light D

ark

lig

ht

L – number of gray levels

Hebrew University Image Processing - 2006

Simple Examples for Intensity Transformations

• Image negatives = L-1-r

r

s

Dark light

Dar

k

light

L – number of gray levels

Hebrew University Image Processing - 2006

Examples for Intensity Transformations

• Log Transforms = c*log(1+r)

r

s

Dark light

Dar

k

light

What does the slope of the functionIndicating ?

Hebrew University Image Processing - 2006

Examples for Intensity Transformations

• Power Law Transforms = c*r

=2

γ

γ

γ

Page 9: Hebrew University Image Processing - 2006 Today Outline Digital Image Processing · 2006. 10. 26. · Hebrew University Image Processing - 2006 Matlab Basics •Digital image representation

9

Hebrew University Image Processing - 2006

Example - Gamma Correction

• What kind of correction can we try here ?

Hebrew University Image Processing - 2006

Example - Gamma Correction

• γ=3, 4, 5

Hebrew University Image Processing - 2006

Histogram Processing

kk nrh =)(

• h(rk) = nk• Normalized:

p(rk) = nk/N

0 1 255

in Matlab: imhist(img)

+

Hebrew University Image Processing - 2006

Sample of Image Histogram

Hebrew University Image Processing - 2006

Histogram Equalization

• We are interested in equal use of all gray levelN pixels, range 0,..K-1

nk – number of pixels in level k

∑=

==k

ii

kk n

NN

ys

1

1

255graylevels

#p in

leve

l

0

histogram

1graylevels

#p <

leve

l

0

Normalized Accumulative histogram

graylevels

#p <

leve

l

0

Accumulative histogram

255

∑=

=k

iik ny

1

rk=k/K

sk1

Hebrew University Image Processing - 2006

Histogram Equalization

• Equalize :For every original gray level k1. Calculate the image histogram

2. Find the accumulative sum of the histogram values - yk (in Matlab – cumsum(vec))

3. Normalize the values of the histogram accumulative sum by dividing in the total number of pixels

4. Multiple the normalized vector in the maximal gray level value (K-1)and round (shift back to the original gray level range)

5. Map the gray levels values to the result of step (3)

6. Stretch the values back to the range 1,..K (improve contrast in the end of this process)

∑=

=k

iik n

Ns

1

1

1

#p <

leve

l

0

Normalized Accumulative histogram

rk=k/K

sk

)1(*1

1

−= ∑=

LnN

sk

iik

rk sk

1

Page 10: Hebrew University Image Processing - 2006 Today Outline Digital Image Processing · 2006. 10. 26. · Hebrew University Image Processing - 2006 Matlab Basics •Digital image representation

10

Hebrew University Image Processing - 2006

Histogram Equalization • Can we get uniform histogram ?

In discrete images it is in most cases impossible to obtain uniform histogram, we

want to get as close as possible to uniform

• So what do we get ?

The distribution of gray level in area along the histogram will result in approximately same number of pixels. Note that no bins are split.

• What happen to the number of bins ?The number of bins can only decrease: bins are not split and small bins in the histogram may be united (but the effect is not visibly disturbing since it involves very small number of pixels)

• What features are kept after the equalization ?The histogram equalization process is monotonic (as the process involves

accumulative sum) and therefore the relative brightness of a pixel is preserved

Hebrew University Image Processing - 2006

When will this fail ?

• In General : on images with gray level distribution that is not ‘standard’ we may get unwanted results from the histogram equalization process.

Hebrew University Image Processing - 2006

Equalization results

Hebrew University Image Processing - 2006

Exercise 1

• Targets:

– Getting familiar with the Matlab environment

– Learning image formats and conversions between them

– Argument error checking

– Working with image histogram

– Write an efficient code

Hebrew University Image Processing - 2006

A few more important commands for image operations (look in the help)

• find • repmat• reshape• clear• save

• plot, subplot• disp