counting stars - software carpentry · multimedia programming counting stars a "blob" is...

31
Counting Stars Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See http://software-carpentry.org/license.html for more information. Multimedia Programming Multimedia Programming Counting Stars Problem: how many stars are in this image?

Upload: others

Post on 10-Oct-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Counting Stars

Copyright © Software Carpentry 2010

This work is licensed under the Creative Commons Attribution License

See http://software-carpentry.org/license.html for more information.

Multimedia Programming

Multimedia Programming Counting Stars

Problem: how many stars are in this image?

Page 2: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

Converted to black and white

Multimedia Programming Counting Stars

Converted to black and white

How many black blobs?

Page 3: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

A "blob" is a group of adjacent pixels

Multimedia Programming Counting Stars

A "blob" is a group of adjacent pixels

Decide that "adjacent" means 4-way, not 8-way

Page 4: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

A "blob" is a group of adjacent pixels

Decide that "adjacent" means 4-way, not 8-way

Want to count each blob once

Multimedia Programming Counting Stars

A "blob" is a group of adjacent pixels

Decide that "adjacent" means 4-way, not 8-way

Want to count each blob once

Scan the image left to right, top to bottom

Page 5: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

A "blob" is a group of adjacent pixels

Decide that "adjacent" means 4-way, not 8-way

Want to count each blob once

Scan the image left to right, top to bottom

Increment count each time we find a new blob

Multimedia Programming Counting Stars

A "blob" is a group of adjacent pixels

Decide that "adjacent" means 4-way, not 8-way

Want to count each blob once

Scan the image left to right, top to bottom

Increment count each time we find a new blob

But how do we tell?

Page 6: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

A "blob" is a group of adjacent pixels

Decide that "adjacent" means 4-way, not 8-way

Want to count each blob once

Scan the image left to right, top to bottom

Increment count each time we find a new blob

But how do we tell?

Turn black pixels red

Multimedia Programming Counting Stars

A "blob" is a group of adjacent pixels

Decide that "adjacent" means 4-way, not 8-way

Want to count each blob once

Scan the image left to right, top to bottom

Increment count each time we find a new blob

But how do we tell?

Turn black pixels red

If there is a red pixel before this one in scan order,

we have already counted this blob

Page 7: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

Scan to black pixel count: 0

Multimedia Programming Counting Stars

Scan to black pixel

Mark it seen

count: 0

Page 8: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

Scan to black pixel

Mark it seen

Nothing red ahead of it

in scan order, so it must be

a new star

count: 1

Multimedia Programming Counting Stars

Scan to black pixel

Mark it seen

Keep scanning

count: 1

Page 9: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

Scan to black pixel

Mark it seen

Keep scanning

Does have red predecessor,

so this star already counted

count: 1

Multimedia Programming Counting Stars

A new star! count: 2

Page 10: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

A new star!

But this looks like a new star

as well…

count: 2

Multimedia Programming Counting Stars

A new star!

But this looks like a new star

as well…

So look in more directions

count: 2

Page 11: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

A new star!

But this looks like a new star

as well…

So look in more directions

But this also gives the wrong

answer

count: 2

Multimedia Programming Counting Stars

A new star!

But this looks like a new star

as well…

So look in more directions

But this also gives the wrong

answer

Change our definition so that

these two blobs are one star?

count: 2

Page 12: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

Still gives the wrong answer

Multimedia Programming Counting Stars

Solution: use flood fill to color in each star when it

is first encountered

Page 13: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

Solution: use flood fill to color in each star when it

is first encountered

Find an uncolored pixel

Multimedia Programming Counting Stars

Solution: use flood fill to color in each star when it

is first encountered

Find an uncolored pixel

Look at its neighbors

Page 14: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

Solution: use flood fill to color in each star when it

is first encountered

Find an uncolored pixel

Look at its neighbors

For each that needs coloring…

Multimedia Programming Counting Stars

Solution: use flood fill to color in each star when it

is first encountered

Find an uncolored pixel

Look at its neighbors

For each that needs coloring…

Look at its neighbors, and

for each that needs coloring…

Page 15: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

Solution: use flood fill to color in each star when it

is first encountered

Find an uncolored pixel

Look at its neighbors

For each that needs coloring…

Look at its neighbors, and

for each that needs coloring…

Stop when whole star colored

Multimedia Programming Counting Stars

Solution: use flood fill to color in each star when it

is first encountered

Find an uncolored pixel

Look at its neighbors

For each that needs coloring…

Look at its neighbors, and

for each that needs coloring…

Stop when whole star colored

Then start scanning again

Page 16: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

defdefdefdef count(picture):

xsize, ysize = picture.size

temp = picture.load()

result = 0

forforforfor x inininin range(xsize):

forforforfor y inininin range(ysize):

ifififif temp[x, y] == BLACK:

result += 1

fill(temp, xsize, ysize, x, y)

returnreturnreturnreturn result

Multimedia Programming Counting Stars

defdefdefdef count(picture):

xsize, ysize = picture.size

temp = picture.load()

result = 0

forforforfor x inininin range(xsize):

forforforfor y inininin range(ysize):

ifififif temp[x, y] == BLACK:

result += 1

fill(temp, xsize, ysize, x, y)

returnreturnreturnreturn result

Page 17: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

defdefdefdef count(picture):

xsize, ysize = picture.size

temp = picture.load()

result = 0

forforforfor x inininin range(xsize):

forforforfor y inininin range(ysize):

ifififif temp[x, y] == BLACK:

result += 1

fill(temp, xsize, ysize, x, y)

returnreturnreturnreturn result

Multimedia Programming Counting Stars

defdefdefdef count(picture):

xsize, ysize = picture.size

temp = picture.load()

result = 0

forforforfor x inininin range(xsize):

forforforfor y inininin range(ysize):

ifififif temp[x, y] == BLACK:

result += 1

fill(temp, xsize, ysize, x, y)

returnreturnreturnreturn result

Page 18: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

Use a work queue

Multimedia Programming Counting Stars

Use a work queue

Keep list of (x, y) coordinates to be examined

Page 19: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

Use a work queue

Keep list of (x, y) coordinates to be examined

Loop until queue is empty:

Multimedia Programming Counting Stars

Use a work queue

Keep list of (x, y) coordinates to be examined

Loop until queue is empty:

– Take (x, y) coordinates from queue

Page 20: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

Use a work queue

Keep list of (x, y) coordinates to be examined

Loop until queue is empty:

– Take (x, y) coordinates from queue

– If black, fill it in and add neighbors to queue

Multimedia Programming Counting Stars

defdefdefdef fill(pic, xsize, ysize, x_start, y_start):

queue = [(x_start, y_start)]

whilewhilewhilewhile queue:

x, y, queue = queue[0][0], queue[0][1], queue[1:]

ifififif pic[x, y] == BLACK:

pic[x, y] = RED

ifififif x > 0: queue.append((x-1, y))

ifififif x < (xsize-1): queue.append((x+1, y))

ifififif y > 0: queue.append((x, y-1))

ifififif y < (ysize-1): queue.append((x, y+1))

Page 21: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

defdefdefdef fill(pic, xsize, ysize, x_start, y_start):

queue = [(x_start, y_start)]

whilewhilewhilewhile queue:

x, y, queue = queue[0][0], queue[0][1], queue[1:]

ifififif pic[x, y] == BLACK:

pic[x, y] = RED

ifififif x > 0: queue.append((x-1, y))

ifififif x < (xsize-1): queue.append((x+1, y))

ifififif y > 0: queue.append((x, y-1))

ifififif y < (ysize-1): queue.append((x, y+1))

Multimedia Programming Counting Stars

defdefdefdef fill(pic, xsize, ysize, x_start, y_start):

queue = [(x_start, y_start)]

whilewhilewhilewhile queue:

x, y, queue = queue[0][0], queue[0][1], queue[1:]

ifififif pic[x, y] == BLACK:

pic[x, y] = RED

ifififif x > 0: queue.append((x-1, y))

ifififif x < (xsize-1): queue.append((x+1, y))

ifififif y > 0: queue.append((x, y-1))

ifififif y < (ysize-1): queue.append((x, y+1))

Page 22: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

defdefdefdef fill(pic, xsize, ysize, x_start, y_start):

queue = [(x_start, y_start)]

whilewhilewhilewhile queue:

x, y, queue = queue[0][0], queue[0][1], queue[1:]

ifififif pic[x, y] == BLACK:

pic[x, y] = RED

ifififif x > 0: queue.append((x-1, y))

ifififif x < (xsize-1): queue.append((x+1, y))

ifififif y > 0: queue.append((x, y-1))

ifififif y < (ysize-1): queue.append((x, y+1))

Multimedia Programming Counting Stars

defdefdefdef fill(pic, xsize, ysize, x_start, y_start):

queue = [(x_start, y_start)]

whilewhilewhilewhile queue:

x, y, queue = queue[0][0], queue[0][1], queue[1:]

ifififif pic[x, y] == BLACK:

pic[x, y] = RED

ifififif x > 0: queue.append((x-1, y))

ifififif x < (xsize-1): queue.append((x+1, y))

ifififif y > 0: queue.append((x, y-1))

ifififif y < (ysize-1): queue.append((x, y+1))

Page 23: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

defdefdefdef fill(pic, xsize, ysize, x_start, y_start):

queue = [(x_start, y_start)]

whilewhilewhilewhile queue:

x, y, queue = queue[0][0], queue[0][1], queue[1:]

ifififif pic[x, y] == BLACK:

pic[x, y] = RED

ifififif x > 0: queue.append((x-1, y))

ifififif x < (xsize-1): queue.append((x+1, y))

ifififif y > 0: queue.append((x, y-1))

ifififif y < (ysize-1): queue.append((x, y+1))

Multimedia Programming Counting Stars

defdefdefdef fill(pic, xsize, ysize, x_start, y_start):

queue = [(x_start, y_start)]

whilewhilewhilewhile queue:

x, y, queue = queue[0][0], queue[0][1], queue[1:]

ifififif pic[x, y] == BLACK:

pic[x, y] = RED

ifififif x > 0: queue.append((x-1, y))

ifififif x < (xsize-1): queue.append((x+1, y))

ifififif y > 0: queue.append((x, y-1))

ifififif y < (ysize-1): queue.append((x, y+1))

Page 24: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

Filling in action

8

7

6

5

4

3

2

1

0

876543210

[(1, 2)]

Multimedia Programming Counting Stars

Filling in action

[(0, 2),

(2, 2),

(1, 1),

(1, 3)]

8

7

6

5

4

X3

XX2

X1

0

876543210

Page 25: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

Filling in action

[(1, 3)]

8

7

6

5

4

3

2

1

0

876543210

Multimedia Programming Counting Stars

Filling in action

[(1, 2),

(0, 3),

(2, 3),

(1, 4)]

8

7

6

5

X4

XX3

X2

1

0

876543210

Page 26: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

Filling in action

[(1, 2),

(0, 3),

(2, 3),

(1, 4)]

8

7

6

5

X4

XX3

X2

1

0

876543210

Multimedia Programming Counting Stars

Filling in action

[(1, 2),

(0, 3),

(2, 3),

(1, 4)]

8

7

6

5

X4

XX3

X2

1

0

876543210

Exercise: never look at a pixel twice

Page 27: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

Or use a recursive algorithm

Multimedia Programming Counting Stars

Or use a recursive algorithm

Keep the work to be done on the call stack

Page 28: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

Or use a recursive algorithm

Keep the work to be done on the call stack

defdefdefdef fill(pic, xsize, ysize, x, y):

ifififif pic[x, y] != BLACK:

returnreturnreturnreturn

pic[x, y] = RED

ifififif x > 0: fill(pic, xsize, ysize, x-1, y)

ifififif x < (xsize-1): fill(pic, xsize, ysize, x+1, y)

ifififif y > 0: fill(pic, xsize, ysize, x, y-1)

ifififif y < (ysize-1): fill(pic, xsize, ysize, x, y+1)

Multimedia Programming Counting Stars

Or use a recursive algorithm

Keep the work to be done on the call stack

defdefdefdef fill(pic, xsize, ysize, x, y):

ifififif pic[x, y] != BLACK:

returnreturnreturnreturn

pic[x, y] = RED

ifififif x > 0: fill(pic, xsize, ysize, x-1, y)

ifififif x < (xsize-1): fill(pic, xsize, ysize, x+1, y)

ifififif y > 0: fill(pic, xsize, ysize, x, y-1)

ifififif y < (ysize-1): fill(pic, xsize, ysize, x, y+1)

Page 29: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

Or use a recursive algorithm

Keep the work to be done on the call stack

defdefdefdef fill(pic, xsize, ysize, x, y):

ifififif pic[x, y] != BLACK:

returnreturnreturnreturn

pic[x, y] = RED

ifififif x > 0: fill(pic, xsize, ysize, x-1, y)

ifififif x < (xsize-1): fill(pic, xsize, ysize, x+1, y)

ifififif y > 0: fill(pic, xsize, ysize, x, y-1)

ifififif y < (ysize-1): fill(pic, xsize, ysize, x, y+1)

Multimedia Programming Counting Stars

Or use a recursive algorithm

Keep the work to be done on the call stack

defdefdefdef fill(pic, xsize, ysize, x, y):

ifififif pic[x, y] != BLACK:

returnreturnreturnreturn

pic[x, y] = RED

ifififif x > 0: fill(pic, xsize, ysize, x-1, y)

ifififif x < (xsize-1): fill(pic, xsize, ysize, x+1, y)

ifififif y > 0: fill(pic, xsize, ysize, x, y-1)

ifififif y < (ysize-1): fill(pic, xsize, ysize, x, y+1)

Page 30: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

Multimedia Programming Counting Stars

Or use a recursive algorithm

Keep the work to be done on the call stack

defdefdefdef fill(pic, xsize, ysize, x, y):

ifififif pic[x, y] != BLACK:

returnreturnreturnreturn

pic[x, y] = RED

ifififif x > 0: fill(pic, xsize, ysize, x-1, y)

ifififif x < (xsize-1): fill(pic, xsize, ysize, x+1, y)

ifififif y > 0: fill(pic, xsize, ysize, x, y-1)

ifififif y < (ysize-1): fill(pic, xsize, ysize, x, y+1)

Multimedia Programming Counting Stars

Call stack holds pixels currently being examined

8

7

6

5

4

3

2

1

0

876543210

fill(..., 3, 4)

fill(..., 3, 3)

fill(..., 2, 3)

fill(..., 1, 3)

fill(..., 1, 2)

first call

most

recent

call

Page 31: Counting Stars - Software Carpentry · Multimedia Programming Counting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each

November 2010

created by

Paul Gries, Jeremy Nickurak, and Greg Wilson

Copyright © Software Carpentry 2010

This work is licensed under the Creative Commons Attribution License

See http://software-carpentry.org/license.html for more information.