+ All Categories
Home > Documents > Lecture 20 Regular Expressions and More Image...

Lecture 20 Regular Expressions and More Image...

Date post: 23-Aug-2020
Category:
Upload: others
View: 6 times
Download: 0 times
Share this document with a friend
49
Lecture 20 Regular Expressions and More Image Processing 1
Transcript
Page 1: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Lecture 20Regular Expressions and More Image Processing

1

Page 2: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Lecture 20 Goals

•••••

•••

2

Page 3: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

String Searching

contains().

>> s1 = "Puppies vs. Babies";>> contains(s1,'Babies')ans = logical 1>> contains(s2,'Kitties')ans = logical 0

3

Page 4: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

String Searching

contains()

>> s1 = "Puppies vs. Babies";>> contains(s1,'puppies')ans = logical 0>> contains(lower(s1),'puppies')ans = logical 1>> contains(s1,'puppies','IgnoreCase',true)ans = logical 1

4

Page 5: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

String Searching

strfind()

>> s1 = "Puppies vs. Babies";>> strfind(s1,'puppies')ans = 13>> strfind(s1,'ies')ans = 5 16>> strfind(s1,'dogs')ans = []

findstr5

Page 6: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Directories and Filenames

dir dir

>> f = dirf = 10×1 struct array with fields: name folder date bytes isdir datenum>> f = dir('data'); % specify a relative path>> f = dir('/home/cs4user/'); % specify an absolute path

6

Page 7: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

MATLAB Structs

7

>> a = struct('a',1,'b',2,'c',3)a = struct with fields:

a: 1 b: 2 c: 3>> b = struct('type',true,'color','red','data',[4 5 6])b = struct with fields:

type: 1 color: 'red' data: [4 5 6]>> c.first = 1;>> c.second = 2;

Page 8: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

MATLAB Structs

8

>> b = struct('type',true,'color','red','data',[4 5 6]);>> fieldnames(b)ans = 3×1 cell array {'type' } {'color'} {'data' }>> isfield(b,'data')ans = logical 1>> isfield(b,'date')ans = logical 0

Page 9: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

MATLAB Structs

>> b = struct('type',true,'color','red','data',[4 5 6]);>> b.typeans = logical 1>> b.colorans = 'red'>> c.dataans = 4 5 6

9

Page 10: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

MATLAB Structs

10

>> f = dir('*.m');>> for fnum = 1:numel(f)

f(fnum).bytes endans = 278ans = 44ans = 54

Page 11: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

>> f(1).myVariable = true>> f(2).myVariable = 5>> f.myVariableans = logical 1ans = 5ans = []

MATLAB Structs

11

Page 12: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Searching Directories

dir

>> file = dir('data_20190415.mat');>> file = dir('data/data_20190415.mat');>> file = dir(['data',filesep,'data_20190415.mat']);>> file struct with fields:

name: data_20190415.mat' folder: '/MATLAB Drive/data' date: '15-Apr-2019 21:50:21' bytes: 240 isdir: 0 datenum: 7.3753e+05

12

Page 13: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Searching Directories

>> files = dir('*.m');f = 3×1 struct array with fields: name folder date bytes isdir datenum>> f.nameans = 'genData.m'ans = 'myFunc.m'ans = 'test_myFunc.m'

13

Page 14: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Searching Directories

>> files = dir('data_2019*.mat');>> files = dir('*.m*');

'data_20190415.mat' 'data_20190521.mat' 'data_2019_aux.mat'?

14

Page 15: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Regular Expressions

15

https://xkcd.com/208/

Page 16: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Regular Expressions

>> help regexpregexp Match regular expression

S = regexp(STR,EXPRESSION) matches the regular expression, EXPRESSION, in the input argument, STR. The indices of the beginning of the matches are returned....

>> help regexpiregexpi Match regular expression, ignoring case

START = regexpi(STR,EXPRESSION) matches the regular expression, EXPRESSION, in the input argument, STR, regardless of case. The indices of the beginning of the matches are returned.

See also:16

Page 17: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Regular Expressions

>> str = 'bat cat can car coat court cut ct caoueouat';>> regexp(str, 'can')ans = 9>> regexp(str, 'c[aeiou]+t')ans = 5 17 28 35

>> regexp(str, 'Co[\w].')ans = []>> regexpi(str, 'Co[\w].')ans = 17 22

17

Page 18: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Regular Expressions: Metacharacters

18

Page 19: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Regular Expressions: Logical Operators

19

Page 20: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Regular Expressions: Quantifiers

20

Page 21: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

What is the output of the following code?

>> regexp('acoueouat', 'c[aeiou]+t')

A)B)C)D)

21

Page 22: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

What is the output of the following code?

>> regexp('acoueouat', 'c[aeiou]+t')

A)B)C)D)

22

Page 23: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

What is the output of the following code?

>> regexp('aoueouat', 'c[aeiou]+t')

A)B)C)D)

23

Page 24: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

What is the output of the following code?

>> regexp('aoueouat', 'c[aeiou]+t')

A)B)C)D)

24

Page 25: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

What is the output of the following code?

>> regexp('aoueouat', 'c|[aeiou]+t')

A)B)C)D)

25

Page 26: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

What is the output of the following code?

>> regexp('aoueouat', 'c|[aeiou]+t')

A)B)C)D)

26

Page 27: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

What is the output of the following code?

>> regexp('puppy54.mat', '[\d]+')

A)B)C)D)

27

Page 28: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

What is the output of the following code?

>> regexp('puppy54.mat', '[\d]+')

A)B)C)D)

28

Page 29: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Saving Variables Using Regular Expressions

>> save myfile.mat -regexp \d>> save myfile.mat -regexp ^data\_2019(04|05)

29

Page 30: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

2D Data Visualization

30

[X,Y] = meshgrid(-8:.5:8);R = sqrt(X.^2 + Y.^2) + eps;

Z = sin(R)./R;figuremesh(X,Y,Z)

Page 31: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

2D Data Visualization

31

Page 32: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Mesh vs. Surf

32

>> mesh(membrane)>> surf(membrane)

Page 33: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Meshc vs. Surfc

33

>> meshc(membrane)>> surfc(membrane)

Page 34: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Surf vs. Pcolor

34

Page 35: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Image vs. Imagesc

35

>> image(membrane)>> image(membrane*60)>> imagesc(membrane)

Page 36: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Contour vs. Contourf

36

>> contour(membrane)>> contourf(membrane)

Page 37: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Lighting and Shading

37

Page 38: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Lighting and Shading

38

[X,Y] = meshgrid(-8:.5:8);R = sqrt(X.^2 + Y.^2) + eps;Z = sin(R)./R;surf(X,Y,Z)

Page 39: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Lighting and Shading

39

surf(X,Y,Z,'LineStyle','none')

Page 40: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Lighting and Shading

40

lh = light

Page 41: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Lighting and Shading

41

lightangle(lh,-90,25)

Page 42: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Lighting and Shading

42

lighting phong

Page 43: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Lighting and Shading

43

[X,Y] = meshgrid(-8:.1:8);...

Page 44: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Colorbar and ‘colormap’

44

>> pcolor(membrane)>> colorbar

colormap(jet)colormap(parula)

Page 45: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Colorbar and ‘colormap’

45

>> pcolor(membrane)>> colorbar

colormap(gray)colormap(hot)

Page 46: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Colorbar and ‘colormap’

46

>> pcolor(membrane)>> colorbar

colormap(white)colormap(colorcube)

Page 47: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Color Scale and ‘colormap’

47

>> c = hot(255); % use 255 levels in colormap>> c = flipud(c); % invert colormap>> colormap(c); % set colormap to inverted hot

Page 48: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Color Scale and Dynamic Range

48

cLim = [-0.3338, 1.0000]

Page 49: Lecture 20 Regular Expressions and More Image Processingcs.brown.edu/courses/cs004/lectures/lec20.pdf · Regular Expressions and More Image Processing 1. ... Regular Expressions >>

Color Scale and Dynamic Range

49

colorbar % turn colorbar oncLim = get(gca,'cLim'); % get current scaleset(gca,'cLim',[cLim(2)-1.0 cLim(2)]) % colormap limits [min max]

cLim = [0, 1]


Recommended