Download presentation
Presentation is loading. Please wait.
Published byGeorgiana Palmer Modified over 8 years ago
1
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
2
Counting Stars Problem: how many stars are in this image?
3
Multimedia ProgrammingCounting Stars Converted to black and white
4
Multimedia ProgrammingCounting Stars Converted to black and white How many black blobs?
5
Multimedia ProgrammingCounting Stars A "blob" is a group of adjacent pixels
6
Multimedia ProgrammingCounting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way
7
Multimedia ProgrammingCounting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way Want to count each blob once
8
Multimedia ProgrammingCounting 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
9
Multimedia ProgrammingCounting 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
10
Multimedia ProgrammingCounting 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?
11
Multimedia ProgrammingCounting 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
12
Multimedia ProgrammingCounting 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
13
Multimedia ProgrammingCounting Stars Scan to black pixelcount: 0
14
Multimedia ProgrammingCounting Stars Scan to black pixel Mark it seen count: 0
15
Multimedia ProgrammingCounting 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
16
Multimedia ProgrammingCounting Stars Scan to black pixel Mark it seen Keep scanning count: 1
17
Multimedia ProgrammingCounting Stars Scan to black pixel Mark it seen Keep scanning Does have red predecessor, so this star already counted count: 1
18
Multimedia ProgrammingCounting Stars A new star!count: 2
19
Multimedia ProgrammingCounting Stars A new star! But this looks like a new star as well… count: 2
20
Multimedia ProgrammingCounting Stars A new star! But this looks like a new star as well… So look in more directions count: 2
21
Multimedia ProgrammingCounting 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
22
Multimedia ProgrammingCounting 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
23
Multimedia ProgrammingCounting Stars Still gives the wrong answer
24
Multimedia ProgrammingCounting Stars Solution: use flood fill to color in each star when it is first encountered
25
Multimedia ProgrammingCounting Stars Solution: use flood fill to color in each star when it is first encountered Find an uncolored pixel
26
Multimedia ProgrammingCounting Stars Solution: use flood fill to color in each star when it is first encountered Find an uncolored pixel Look at its neighbors
27
Multimedia ProgrammingCounting 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…
28
Multimedia ProgrammingCounting 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…
29
Multimedia ProgrammingCounting 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
30
Multimedia ProgrammingCounting 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
31
Multimedia ProgrammingCounting Stars def count(picture): xsize, ysize = picture.size temp = picture.load() result = 0 for x in range(xsize): for y in range(ysize): if temp[x, y] == BLACK: result += 1 fill(temp, xsize, ysize, x, y) return result
32
Multimedia ProgrammingCounting Stars def count(picture): xsize, ysize = picture.size temp = picture.load() result = 0 for x in range(xsize): for y in range(ysize): if temp[x, y] == BLACK: result += 1 fill(temp, xsize, ysize, x, y) return result
33
Multimedia ProgrammingCounting Stars def count(picture): xsize, ysize = picture.size temp = picture.load() result = 0 for x in range(xsize): for y in range(ysize): if temp[x, y] == BLACK: result += 1 fill(temp, xsize, ysize, x, y) return result
34
Multimedia ProgrammingCounting Stars def count(picture): xsize, ysize = picture.size temp = picture.load() result = 0 for x in range(xsize): for y in range(ysize): if temp[x, y] == BLACK: result += 1 fill(temp, xsize, ysize, x, y) return result
35
Multimedia ProgrammingCounting Stars Use a work queue
36
Multimedia ProgrammingCounting Stars Use a work queue Keep list of (x, y) coordinates to be examined
37
Multimedia ProgrammingCounting Stars Use a work queue Keep list of (x, y) coordinates to be examined Loop until queue is empty:
38
Multimedia ProgrammingCounting 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
39
Multimedia ProgrammingCounting 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
40
Multimedia ProgrammingCounting Stars def fill(pic, xsize, ysize, x_start, y_start): queue = [(x_start, y_start)] while queue: x, y, queue = queue[0][0], queue[0][1], queue[1:] if pic[x, y] == BLACK: pic[x, y] = RED if x > 0: queue.append((x-1, y)) if x < (xsize-1): queue.append((x+1, y)) if y > 0: queue.append((x, y- 1)) if y < (ysize-1): queue.append((x, y+1))
41
Multimedia ProgrammingCounting Stars def fill(pic, xsize, ysize, x_start, y_start): queue = [(x_start, y_start)] while queue: x, y, queue = queue[0][0], queue[0][1], queue[1:] if pic[x, y] == BLACK: pic[x, y] = RED if x > 0: queue.append((x-1, y)) if x < (xsize-1): queue.append((x+1, y)) if y > 0: queue.append((x, y- 1)) if y < (ysize-1): queue.append((x, y+1))
42
Multimedia ProgrammingCounting Stars def fill(pic, xsize, ysize, x_start, y_start): queue = [(x_start, y_start)] while queue: x, y, queue = queue[0][0], queue[0][1], queue[1:] if pic[x, y] == BLACK: pic[x, y] = RED if x > 0: queue.append((x-1, y)) if x < (xsize-1): queue.append((x+1, y)) if y > 0: queue.append((x, y- 1)) if y < (ysize-1): queue.append((x, y+1))
43
Multimedia ProgrammingCounting Stars def fill(pic, xsize, ysize, x_start, y_start): queue = [(x_start, y_start)] while queue: x, y, queue = queue[0][0], queue[0][1], queue[1:] if pic[x, y] == BLACK: pic[x, y] = RED if x > 0: queue.append((x-1, y)) if x < (xsize-1): queue.append((x+1, y)) if y > 0: queue.append((x, y- 1)) if y < (ysize-1): queue.append((x, y+1))
44
Multimedia ProgrammingCounting Stars def fill(pic, xsize, ysize, x_start, y_start): queue = [(x_start, y_start)] while queue: x, y, queue = queue[0][0], queue[0][1], queue[1:] if pic[x, y] == BLACK: pic[x, y] = RED if x > 0: queue.append((x-1, y)) if x < (xsize-1): queue.append((x+1, y)) if y > 0: queue.append((x, y- 1)) if y < (ysize-1): queue.append((x, y+1))
45
Multimedia ProgrammingCounting Stars def fill(pic, xsize, ysize, x_start, y_start): queue = [(x_start, y_start)] while queue: x, y, queue = queue[0][0], queue[0][1], queue[1:] if pic[x, y] == BLACK: pic[x, y] = RED if x > 0: queue.append((x-1, y)) if x < (xsize-1): queue.append((x+1, y)) if y > 0: queue.append((x, y- 1)) if y < (ysize-1): queue.append((x, y+1))
46
Multimedia ProgrammingCounting Stars def fill(pic, xsize, ysize, x_start, y_start): queue = [(x_start, y_start)] while queue: x, y, queue = queue[0][0], queue[0][1], queue[1:] if pic[x, y] == BLACK: pic[x, y] = RED if x > 0: queue.append((x-1, y)) if x < (xsize-1): queue.append((x+1, y)) if y > 0: queue.append((x, y- 1)) if y < (ysize-1): queue.append((x, y+1))
47
Multimedia ProgrammingCounting Stars Filling in action 012345678 0 1 2 3 4 5 6 7 8 [(1, 2)]
48
Multimedia ProgrammingCounting Stars Filling in action [(0, 2), (2, 2), (1, 1), (1, 3)] 012345678 0 1X 2XX 3X 4 5 6 7 8
49
Multimedia ProgrammingCounting Stars Filling in action [(1, 3)] 012345678 0 1 2 3 4 5 6 7 8
50
Multimedia ProgrammingCounting Stars Filling in action [(1, 2), (0, 3), (2, 3), (1, 4)] 012345678 0 1 2X 3XX 4X 5 6 7 8
51
Multimedia ProgrammingCounting Stars Filling in action [(1, 2), (0, 3), (2, 3), (1, 4)] 012345678 0 1 2X 3XX 4X 5 6 7 8
52
Multimedia ProgrammingCounting Stars Filling in action [(1, 2), (0, 3), (2, 3), (1, 4)] 012345678 0 1 2X 3XX 4X 5 6 7 8 Exercise: never look at a pixel twice
53
Multimedia ProgrammingCounting Stars Or use a recursive algorithm
54
Multimedia ProgrammingCounting Stars Or use a recursive algorithm Keep the work to be done on the call stack
55
Multimedia ProgrammingCounting Stars Or use a recursive algorithm Keep the work to be done on the call stack def fill(pic, xsize, ysize, x, y): if pic[x, y] != BLACK: return pic[x, y] = RED if x > 0: fill(pic, xsize, ysize, x- 1, y) if x < (xsize-1): fill(pic, xsize, ysize, x+1, y) if y > 0: fill(pic, xsize, ysize, x, y-1) if y < (ysize-1): fill(pic, xsize, ysize, x, y+1)
56
Multimedia ProgrammingCounting Stars Or use a recursive algorithm Keep the work to be done on the call stack def fill(pic, xsize, ysize, x, y): if pic[x, y] != BLACK: return pic[x, y] = RED if x > 0: fill(pic, xsize, ysize, x- 1, y) if x < (xsize-1): fill(pic, xsize, ysize, x+1, y) if y > 0: fill(pic, xsize, ysize, x, y-1) if y < (ysize-1): fill(pic, xsize, ysize, x, y+1)
57
Multimedia ProgrammingCounting Stars Or use a recursive algorithm Keep the work to be done on the call stack def fill(pic, xsize, ysize, x, y): if pic[x, y] != BLACK: return pic[x, y] = RED if x > 0: fill(pic, xsize, ysize, x- 1, y) if x < (xsize-1): fill(pic, xsize, ysize, x+1, y) if y > 0: fill(pic, xsize, ysize, x, y-1) if y < (ysize-1): fill(pic, xsize, ysize, x, y+1)
58
Multimedia ProgrammingCounting Stars Or use a recursive algorithm Keep the work to be done on the call stack def fill(pic, xsize, ysize, x, y): if pic[x, y] != BLACK: return pic[x, y] = RED if x > 0: fill(pic, xsize, ysize, x- 1, y) if x < (xsize-1): fill(pic, xsize, ysize, x+1, y) if y > 0: fill(pic, xsize, ysize, x, y-1) if y < (ysize-1): fill(pic, xsize, ysize, x, y+1)
59
Multimedia ProgrammingCounting Stars Or use a recursive algorithm Keep the work to be done on the call stack def fill(pic, xsize, ysize, x, y): if pic[x, y] != BLACK: return pic[x, y] = RED if x > 0: fill(pic, xsize, ysize, x- 1, y) if x < (xsize-1): fill(pic, xsize, ysize, x+1, y) if y > 0: fill(pic, xsize, ysize, x, y-1) if y < (ysize-1): fill(pic, xsize, ysize, x, y+1)
60
Multimedia ProgrammingCounting Stars Call stack holds pixels currently being examined 012345678 0 1 2 3 4 5 6 7 8 fill(..., 3, 4) fill(..., 3, 3) fill(..., 2, 3) fill(..., 1, 3) fill(..., 1, 2) first call most recent call
61
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.