Week9 All

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 119

Video 9.

1
Jianbo Shi

Property of Penn Engineering, Jianbo Shi 1


Exmples of resizing

Property of Penn Engineering, Jianbo Shi 2


820 × 546 × 3
Property of Penn Engineering, Jianbo Shi 3
420 × 546 × 3

(a) (b) (c)

Guess
• We use “crop”, “scaling” and “carving” for
resizing the given image
• Guess which one is for carving?

Property of Penn Engineering, Jianbo Shi 4


Property of Penn Engineering, Jianbo Shi 5
Property of Penn Engineering, Jianbo Shi 6
• Guess which one is for carving?
Property of Penn Engineering, Jianbo Shi 7
Property of Penn Engineering, Jianbo Shi 8
• Guess which one is for carving?
Property of Penn Engineering, Jianbo Shi 9
Property of Penn Engineering, Jianbo Shi 10
Property of Penn Engineering, Jianbo Shi 11
Property of Penn Engineering, Jianbo Shi 12
Property of Penn Engineering, Jianbo Shi 13
Expanded

Property of Penn Engineering, Jianbo Shi 14


Property of Penn Engineering, Jianbo Shi 15
Property of Penn Engineering, Jianbo Shi 16
Property of Penn Engineering, Jianbo Shi 17
Property of Penn Engineering, Jianbo Shi 18
Video 9.2
Jianbo Shi

Property of Penn Engineering, Jianbo Shi 19


Property of Penn Engineering, Jianbo Shi 20
Property of Penn Engineering, Jianbo Shi 21
Property of Penn Engineering, Jianbo Shi 22
k
Energy matrix
e

𝑖-1
𝑖
M

Seam: S y : (i,y(i))|i = 1,...,M s.t.| y(i)  y(i 1)| k

Property of Penn Engineering, Jianbo Shi 23


k
Energy matrix
e

𝑖-1
𝑖
M

S y : (i,y(i))|i = 1,...,M s.t.| y(i)  y(i 1)| k

M
Seam Cost: E(S y )= i 1
e(S y (i ))

Property of Penn Engineering, Jianbo Shi 24


>𝑘 Energy matrix
e

𝑖-1
𝑖
M

• Seam 𝑆 𝑦 : 𝑖, 𝑦 𝑖 |𝑖 = 1, ⋯ , 𝑀 𝑠. 𝑡. 𝑦 𝑖 − 𝑦(𝑖 − 1) ≤ 𝑘
• Seam Cost:
M
E(S y )=  e(S y (i ))
i 1

* y
• Goal: S = minE(S ) 0.46
0.12
0.08 25
Property of Penn Engineering, Jianbo Shi
Where does the energy matrix come
from?

Property of Penn Engineering, Jianbo Shi 26


Energy matrix
e
abs

+ =

abs

Energy matrix

For example: L1 norm of the edge gradients for the


energy function

Property of Penn Engineering, Jianbo Shi 27


How to find the best seam?

Property of Penn Engineering, Jianbo Shi 28


k Energy matrix
e
𝑖-1
𝑖
M

N
Idea 1: Brute force search
𝑖-1 • 1st row: N

𝑖 • 2nd row: (2k  1)N


• 3rd row: (2k  1)2 N
𝑖+1 ……
• Mth row: (2k  1)(M 1)N

• How many possibilities for Seam 𝑆 𝑦 ?


Property of Penn Engineering, Jianbo Shi 29
k Energy matrix
e
𝑖-1
𝑖
M

N
Idea 1: Brute force search
𝑖-1 • Mth row: (2k  1)(M 1)N

𝑖 • Given 𝐌 = 768, 𝐍 = 1024, 𝑘 = 1

𝑖+1

• How many possibilities for Seam 𝑆𝑦? 1024 × 3767


Property of Penn Engineering, Jianbo Shi 30
Too many possibilities!

1024 × 3767

Property of Penn Engineering, Jianbo Shi 31


M

Idea 2: Find the shortest path from first


row to the the last row in the energy
domain!
Property of Penn Engineering, Jianbo Shi 32
𝑖 row

𝑖 + 1 row

Construct the directed graph where


each pixel (node) is connected to the (2k+1) neighbors in
the next row

Property of Penn Engineering, Jianbo Shi 33


1st row

V(u)

• Create an `interior’ set S , initialize with the first row


• Growing S with `least-resistant’ step
• Construct a ‘Value’ matrix with value V(u)encoding the
shortest path cost to each node in S
Property of Penn Engineering, Jianbo Shi 34
1st row

• Initialize Sto include the first row, and set the Value function

V(u)= e(u),u S

For the first row, the shortest path contains only itself

Property of Penn Engineering, Jianbo Shi 35


1st row
S
~
S
u

~
S S
Iterate: find the step expansion of least resistant from

v = argmin[ V(u)  e(v)] where (u  v) is a graph edge


~
uS,vS

Property of Penn Engineering, Jianbo Shi 36


1st row
S
~
S
u

~
S S
Iterate: find the step expansion of least resistant from

v = argmin[ V(u)  e(v)] where (u  v) is a graph edge


~
uS,vS

(v  u) P(v)  u
Remember the path back from

Property of Penn Engineering, Jianbo Shi 37


1st row
S
~
S

~ ~
• Updating the “interior” set: S= S v , S  S- v.

• Updating the Value function:V(v)= min[


~
V(u)  e(v)]
vS

V(v): Is the contingent cost


E of the shortest path
connecting the pixel v to the first row
Property of Penn Engineering, Jianbo Shi 38
1st row
S
~
S

~ ~
• Updating the “interior” set:S= S v , S  S- v.

• Updating the Value function:V(v)= min[


~
V(u)  e(v)]
vS

• Until reaching one of the pixels on the last row.


Property of Penn Engineering, Jianbo Shi 39
Energy Matrix e Value functionV(u) Path Matrix P

Property of Penn Engineering, Jianbo Shi 40


Video 9.3
Jianbo Shi

Property of Penn Engineering, Jianbo Shi 41


Dynamic Programming
Frontier growing row by row

Property of Penn Engineering, Jianbo Shi 42


M

Idea 3: Dynamic programming!

Property of Penn Engineering, Jianbo Shi 43


𝑖 row

𝑖 + 1 row

Use the same graph structure


Propagate the frontier row by row to construct the Value
Matrix

Property of Penn Engineering, Jianbo Shi 44


Value Matrix V
1st row

Path Matrix P

• Still start from first row, and initialize the Value function

V(1, j)= e(1, j)

• Set the Path function

P(1, j)= 0
Property of Penn Engineering, Jianbo Shi 45
1st row
• Updated

• Propagate to the second row, and update the value matrix

V(2, j)= e(2, j)  min(V(1,j-k),…,V(1,j),…,V(1,j+k))

V(2, j): Is the contingent cost of the shortest path


connecting the pixel (2,j) to the first row

Property of Penn Engineering, Jianbo Shi 46


Value Matrix V
1st row
• Updated

Path Matrix P

• Propagate to the second row, and update the value matrix


V(2, j)= e(2, j)  min(V(1,j-k),…,V(1,j),…,V(1,j+k))

• Set the Path function


P(2, j)= argmin(V(1,j-k),…,V(1,j),…,V(1,j+k))

Property of Penn Engineering, Jianbo Shi 47


• Updated

• Iteratively propagate to the ith row, and update the value matrix
V(i, j)= e(i, j)  min(V(i-1,j-k),…,V(i-1,j),…,V(i-1,j+k))

• Set the Path function

P(i, j)= argmin(V(i-1,j-k),…,V(i-1,j),…,V(i-1,j+k))

Property of Penn Engineering, Jianbo Shi 48


Value Matrix V

Energy Matrix e Path Matrix P

Property of Penn Engineering, Jianbo Shi 49


Value Matrix V

Path Matrix P

-1 0 1

• Localize the minimum of the last row of

argmin V(M,:)
Property of Penn Engineering, Jianbo Shi 50
Value Matrix V

Path Matrix P

-1 0 1

Property of Penn Engineering, Jianbo Shi 51


Value Matrix V

Path Matrix P

1
0

-1 0 1

Property of Penn Engineering, Jianbo Shi 52


Value Matrix V

Path Matrix P

1
0

-1 0 1

Property of Penn Engineering, Jianbo Shi 53


Value Matrix V

Path Matrix P

0
0
1
0

Property of Penn Engineering, Jianbo Shi 54


Value Matrix V

Path Matrix P

1 0
0 0
-1 1
0

• Since V is the contingency table, we can start from any pixel


and find a shortest path to the first row!

Property of Penn Engineering, Jianbo Shi 55


Video 9.4
Jianbo Shi

Property of Penn Engineering, Jianbo Shi 56


Illustration for synthetic case

Property of Penn Engineering, Jianbo Shi 57


Energy matrix

Property of Penn Engineering, Jianbo Shi 58


Value matrix

5.5 8 4.5 6 3

18.5 13.5 34.5 30 22


Energy matrix
5.5 8 4.5 6 3 20 20.5 19.5 34.5 30
13 9 30 27 19

6.5 7 6 12.5 8
36 43.5 46.5 30.5 43
16 24 27 11 13
39 42 35 38.5 36
3 6 4.5 8 5.5

Goal:
• Construct value and path matrix from the energy matrix
• Value matrix records the energy of the shortest path from the
starting row to the current pixel

Property of Penn Engineering, Jianbo Shi 59


Value matrix

5.5 8 4.5 6 3

18.5 13.5 34.5 30 22


Energy matrix
5.5 8 4.5 6 3 20 20.5 19.5 34.5 30
13 9 30 27 19

6.5 7 6 12.5 8
36 43.5 46.5 30.5 43
16 24 27 11 13
39 42 35 38.5 36
3 6 4.5 8 5.5

Goal 1:
• Construct Value matrix from the energy matrix
• Value matrix records the energy of the shortest path from the
starting row to the current pixel
• Property: every entry encodes the minimal shortest path to that node
from starting row Property of Penn Engineering, Jianbo Shi 60
-1 0 1
Goal 2:
• Construct Path matrix
• The Path matrix records the immediate predecessor in the shortest
path, for every node Property of Penn Engineering, Jianbo Shi 61
𝑒: energy function
Energy function
5.5 8 4.5 6 3

13 9 30 27 19 • Energy function records


the cost of a pixel.
• Typically it is the image
6.5 7 6 12.5 8 gradient magnitude

16 24 27 11 13

3 6 4.5 8 5.5

Property of Penn Engineering, Jianbo Shi 62


How to generate the two matrices?

Property of Penn Engineering, Jianbo Shi 63


Energy matrix

5.5 8 4.5 6 3

13 9 30 27 19

Step1: Initializing two matrices


• Set value and path matrix the same size as the energy matrix
• Initialize its first row of Value matrix with that of the energy
matrix
• Initialize its first row of path matrix to zero
Property of Penn Engineering, Jianbo Shi 64
Value matrix V

5.5 8 4.5 6 3

18.5 13.5 34.5 30 22

20 20.5 19.5 34.5 30

36 43.5 46.5 30.5 43

39 42 35 38.5 36

Step2: Propagation
• Start with 2nd row, and propagate row by row

Property of Penn Engineering, Jianbo Shi 65


Value matrix V

5.5 8 4.5 6 3

? 13.5 34.5 30
18.5 22

20 20.5 19.5 34.5 30

36 43.5 46.5 30.5 43

39 42 35 38.5 36

Step2: Propagation
• Start with 2nd row

Property of Penn Engineering, Jianbo Shi 66


Value matrix
Neighborhoods of the
pixel in previous row 5.5 8 4.5 6 3

18.5 13.5 34.5 30 22

20 20.5 19.5 34.5 30

36 43.5 46.5 30.5 43

39 42 35 38.5 36

Step2: Propagation
• Start with 2nd row
• Find the neighbors of the pixel in the previous row

Property of Penn Engineering, Jianbo Shi 67


Value matrix V

5.5 8 4.5 6 3

18.5 13.5 34.5 30


V 2,1 = min V (1,1) 5.5 √ 22

V(1,2) 8 20 20.5 19.5 34.5 30


+ e 2,1
36 43.5 46.5 30.5 43

39 42 35 38.5 36

Step2: Propagation

• Start with 2nd row


• Find the neighbors of the pixel in the previous row
• Find the minimum among neighbors

Property of Penn Engineering, Jianbo Shi 68


Step2: Propagation

• Start with 2nd row


• Find the neighbors of the pixel in the previous row
• Find the minimum among neighbors, record it in Path matrix

Property of Penn Engineering, Jianbo Shi 69


Energy matrix Value matrix V
5.5 8 4.5 6 3
5.5 8 4.5 6 3
13 9 30 27 19 13+5.
5 = 13.5 34.5 30
18.5 22
18.5
20 20.5 19.5 34.5 30
V 2,1 = min V (1,1) 5.5 √
V(1,2) 8 36 43.5 46.5 30.5 43
+ e 2,1 13
39 42 35 38.5 36

Step2: Propagation
• Start with 2nd row,
• Find the neighbors of the pixel in the previous row
• Find the minimum among neighbors
• Add the energy value of the pixel with the minimum
Property of Penn Engineering, Jianbo Shi 70
Value matrix V

5.5 8 4.5 6 3

18.5 13.5 34.5 30 22

? 34.5
20 20.5 19.5 30

36 43.5 46.5 30.5 43

39 42 35 38.5 36

Step2: Propagation for every row

Property of Penn Engineering, Jianbo Shi 71


Value matrix V
Neighborhoods of the
pixel in previous row 5.5 8 4.5 6 3

18.5 13.5 34.5 30 22

? 34.5
20 20.5 19.5 30

36 43.5 46.5 30.5 43

39 42 35 38.5 36

Step2: Propagation for every row


• Find the neighbors of the pixel in the previous row

Property of Penn Engineering, Jianbo Shi 72


Value matrix V

5.5 8 4.5 6 3
13.
𝑉(𝑖 − 1, 𝑗 − 1) 5
34.

18.5 13.5 34.5 30 22
min V (𝑖 − 1, 𝑗) 5
V(𝑖 − 1, 𝑗 + 1) 30
? 34.5
20 20.5 19.5 30
+ e 𝑖, 𝑗
36 43.5 46.5 30.5 43

39 42 35 38.5 36

• Find the neighbors of the pixel in the previous row


• Find the minimum among neighbors

Property of Penn Engineering, Jianbo Shi 73


• Find the neighbors of the pixel in the previous row
• Find the minimum among neighbors, record it in Path matrix

Property of Penn Engineering, Jianbo Shi 74


Value matrix V
5.5 8 4.5 6 3
13 9 30 27 19 5.5 8 4.5 6 3
6.5 7 6 12.5 8
18.5 13.5 34.5 30 22

13. ? 34.5
20 20.5 19.5 30
𝑉(𝑖 − 1, 𝑗 − 1) 5
34.

min V (𝑖 − 1, 𝑗) 36 43.5 46.5 30.5 43
5
V(𝑖 − 1, 𝑗 + 1) 30
39 42 35 38.5 36
+ e 𝑖, 𝑗 6

• Find the neighbors of the pixel in the previous row


• Find the minimum among neighbors
• Add the energy value of the pixel with the minimum

Property of Penn Engineering, Jianbo Shi 75


Value matrix V

5.5 8 4.5 6 3
5.5 8 4.5 6 3
13 9 30 27 19
6.5 7 6 12.5 8 18.5 13.5 34.5 30 22

20 20.513.5+
? 34.5
19.5 30
6=
36 43.5 19.5
46.5 30.5 43

39 42 35 38.5 36

• Find the neighbors of the pixel in the previous row


• Get the minimum among neighbors
• Add the energy value of the pixel with the minimum

Property of Penn Engineering, Jianbo Shi 76


Step3: path
Value matrix resolving
5.5 8 4.5 6 3 Path matrix
18.5 13.5 34.5 30 22 0 0 0 0 0
0 1 0 1 0
20 20.5 19.5 34.5 30 1 0 -1 1 0
0 1 0 -1 0
36 43.5 46.5 30.5 43
0 -1 1 0 -1

39 42 35 38.5 36

Property of Penn Engineering, Jianbo Shi 77


Value matrix

5.5 8 4.5 6 3

18.5 13.5 34.5 30 22

20 20.5 19.5 34.5 30

36 43.5 46.5 30.5 43

39 42 35 38.5 36

• Find the minimum of the last row of the Value matrix,


• Find the predecessor of that pixel

Property of Penn Engineering, Jianbo Shi 78


Value matrix

5.5 8 4.5 6 3

18.5 13.5 34.5 30 22 Path matrix

20 20.5 19.5 34.5 30 0 0 0 0 0


0 1 0 1 0
36 43.5 46.5 30.5 43 1 0 -1 1 0
0 1 0 -1 0
39 42 35 38.5 36 0 -1 1 0 -1

• Find the minimum of the last row of the Value


matrix,
• Find the predecessor of that pixel, using Path
matrix Property of Penn Engineering, Jianbo Shi 79
Value matrix

5.5 8 4.5 6 3

18.5 13.5 34.5 30 22 Path matrix

20 20.5 19.5 34.5 30 0 0 0 0 0


0 1 0 1 0
36 43.5 46.5 30.5 43 1 0 -1 1 0
0 1 0 -1 0
39 42 35 38.5 36 0 -1 1 0 -1

• Move to its predecessor


-1 0 1

Property of Penn Engineering, Jianbo Shi 80


Value matrix

5.5 8 4.5 6 3

18.5 13.5 34.5 30 22


Path matrix
20 20.5 19.5 34.5 30 0 0 0 0 0
0 1 0 1 0
36 43.5 46.5 30.5 43 1 0 -1 1 0
0 1 0 -1 0
39 42 35 38.5 36
0 -1 1 0 -1

• Follow predecessor of current


pixel, -1 0 1
Property of Penn Engineering, Jianbo Shi 81
Value matrix

5.5 8 4.5 6 3

18.5 13.5 34.5 30 22 Path matrix

20 20.5 19.5 34.5 30 0 0 0 0 0


0 1 0 1 0
36 43.5 46.5 30.5 43 1 0 -1 1 0
0 1 0 -1 0
39 42 35 38.5 36 0 -1 1 0 -1

• Move to its predecessor


-1 0 1
Property of Penn Engineering, Jianbo Shi 82
Value matrix

5.5 8 4.5 6 3

18.5 13.5 34.5 30 22


Path matrix
20 20.5 19.5 34.5 30 0 0 0 0 0
0 1 0 1 0
36 43.5 46.5 30.5 43 1 0 -1 1 0
0 1 0 -1 0
39 42 35 38.5 36
0 -1 1 0 -1

• Find the predecessor of current


pixel -1 0 1
Property of Penn Engineering, Jianbo Shi 83
Value matrix

5.5 8 4.5 6 3

18.5 13.5 34.5 30 22


Path matrix
20 20.5 19.5 34.5 30
0 0 0 0 0
0 1 0 1 0
36 43.5 46.5 30.5 43
1 0 -1 1 0

39 42 35 38.5 36 0 1 0 -1 0
0 -1 1 0 -1

• Move to its predecessor


-1 0 1
Property of Penn Engineering, Jianbo Shi 84
Value matrix

5.5 8 4.5 6 3

18.5 13.5 34.5 30 22 Path matrix

20 20.5 19.5 34.5 30 0 0 0 0 0


0 1 0 1 0
36 43.5 46.5 30.5 43 1 0 -1 1 0
0 1 0 -1 0
39 42 35 38.5 36 0 -1 1 0 -1

• Find the predecessor of current


pixel -1 0 1
Property of Penn Engineering, Jianbo Shi 85
Value matrix

5.5 8 4.5 6 3

18.5 13.5 34.5 30 22 Path matrix

20 20.5 19.5 34.5 30 0 0 0 0 0


0 1 0 1 0
36 43.5 46.5 30.5 43 1 0 -1 1 0
0 1 0 -1 0
39 42 35 38.5 36 0 -1 1 0 -1

• Stop when reaching the first row


-1 0 1
Property of Penn Engineering, Jianbo Shi 86
Value matrix

5.5 8 4.5 6 3

18.5 13.5 34.5 30 22


Path matrix
20 20.5 19.5 34.5 30 0 0 0 0 0
0 1 0 1 0
36 43.5 46.5 30.5 43 1 0 -1 1 0
0 1 0 -1 0
39 42 35 38.5 36 0 -1 1 0 -1

• Seam carving is to delete the path with minimum


cost
Property of Penn Engineering, Jianbo Shi 87
Value matrix

5.5 8 4.5 6 3

18.5 13.5 34.5 30 22


Path matrix
20 20.5 19.5 34.5 30
0 0 0 0 0
0 1 0 1 0
36 43.5 46.5 30.5 43
1 0 -1 1 0

39 42 35 38.5 36 0 1 0 -1 0
0 -1 1 0 -1

• Seam carving is to delete the path with minimum


cost

Property of Penn Engineering, Jianbo Shi 88


Energy matrix

• Seam carving is to delete the path with minimum


cost

Property of Penn Engineering, Jianbo Shi 89


Carved energy matrix

5.5 8 6 3

13 30 27 19

6.5 7 12.5 8

16 24 27 13

3 6 8 5.5

• Seam carving is to delete the path with minimum


cost

Property of Penn Engineering, Jianbo Shi 90


Value matrix

5.5 8 4.5 6 3

18.5 13.5 34.5 30 22

20 20.5 19.5 34.5 30

Path matrix 36 43.5 46.5 30.5 43


0 0 0 0 0
0 1 0 1 0 39 42 35 38.5 36
1 0 -1 1 0
0 1 0 -1 0
0 -1 1 0 -1

• What if we want to remove a seam that ends on particular


91
pixel? Property of Penn Engineering, Jianbo Shi
Video 9.5
Jianbo Shi

Property of Penn Engineering, Jianbo Shi 92


Illustration on a real image

Property of Penn Engineering, Jianbo Shi 93


Step 0: prepare the energy function
• Transform a color image into gray, Im = rgb2gray(Im)
• Calculate image gradients
• Use the L1 norm of the gradients for the energy function

Property of Penn Engineering, Jianbo Shi 94


Energy matrix
e
abs

+=

abs

Step 0: prepare the energy function


• Transform a color image into gray, Im = rgb2gray(Im)
• Calculate the gradients
• Use the L1 norm of the gradients for the energy function

Property of Penn Engineering, Jianbo Shi 95


Value Matrix Energy matrix
V e Path Matrix P

V : , 1 = e(: , 1)

P :,1 = 0

Step 1: Initializing two matrices


• Set value and path matrix the same size as the energy matrix
• Initialize the first column of value matrix with that of the energy
matrix
• Initialize the first column of path matrix to zero
Property of Penn Engineering, Jianbo Shi 96
𝑉(𝑖 − 1, 𝑗 − 1) 38
min V (𝑖, 𝑗 − 1) 24
V(𝑖 + 1, 𝑗 − 1) 13 √
+ e 𝑖, 𝑗 15

Step 2: Propagation
• Start with 2nd column
• Find the neighbors of the pixel in the previous column
• Find the minimum among neighbors
• Add the energy value of the pixel with the minimum
Property of Penn Engineering, Jianbo Shi 97
Step 2: Propagation
• Start with 2nd column
• Find the neighbors of the pixel in the previous column
• Find the minimum among neighbors
• Add the energy value of the pixel with the minimum
• Assign the direction of the minimum to path matrix
Property of Penn Engineering, Jianbo Shi 98
Step 3: Path Resolving
• Find the minimum of the last column of the Value matrix,
• Find the predecessor of that pixel, using Path matrix
• Trace back to complete the path using the Path matrix

Property of Penn Engineering, Jianbo Shi 99


Step3: Path Resolving
• Find the minimum of the last column of the Value matrix,
• Find the predecessor of that pixel, using Path matrix
• Trace back to complete the path using the Path matrix

Property of Penn Engineering, Jianbo Shi 100


Step 4: Eliminate the Seam

Property of Penn Engineering, Jianbo Shi 101


Property of Penn Engineering, Jianbo Shi 102
Property of Penn Engineering, Jianbo Shi 103
Property of Penn Engineering, Jianbo Shi 104
Property of Penn Engineering, Jianbo Shi 105
Property of Penn Engineering, Jianbo Shi 106
Property of Penn Engineering, Jianbo Shi 107
Property of Penn Engineering, Jianbo Shi 108
Property of Penn Engineering, Jianbo Shi 109
Property of Penn Engineering, Jianbo Shi 110
Property of Penn Engineering, Jianbo Shi 111
Property of Penn Engineering, Jianbo Shi 112
Property of Penn Engineering, Jianbo Shi 113
Property of Penn Engineering, Jianbo Shi 114
Property of Penn Engineering, Jianbo Shi 115
Property of Penn Engineering, Jianbo Shi 116
Property of Penn Engineering, Jianbo Shi 117
Property of Penn Engineering, Jianbo Shi 118
Property of Penn Engineering, Jianbo Shi 119

You might also like