Counting Stars Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See

Slides:



Advertisements
Similar presentations
Polygon Scan Conversion – 11b
Advertisements

Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
Images Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Lecture 9 Feb 26 Announcement/discussion: mid-term # 1 (March 10?) Goals: Queue – implementation using array Application to BFS (breadth-first search)
Depth-first search COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar to pre-order.
CSE 113 Week 5 February , Announcements  Module 2 due 2/15  Exam 3 is on 2/15  Module 3 due 2/22  Exam 4 is on 2/25  Module 4 due 2/29.
Fractions, Decimals, & Percent Conversions
Depth-First Search Lecture 24 COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar.
Rubik's Cube Algorithm Rianna Richardson.
SampleFreq Software to measure frequency from digital images for ecological monitoring.
IT- 601: Computer Graphics Lecture-04 Scan Conversion Jesmin Akhter Lecturer,IIT Jahangirnagar University, Savar, Dhaka,Bangladesh 9/3/2015.
CGMB 314 Intro to Computer Graphics Fill Area Primitives.
 Jim has six children.  Chris fights with Bob,Faye, and Eve all the time; Eve fights (besides with Chris) with Al and Di all the time; and Al and Bob.
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Steganography Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
CS 6825: Binary Image Processing – binary blob metrics
Invasion Percolation: Assembly Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Applications of Stacks and Queues Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Computational Biology, Part 22 Biological Imaging II Robert F. Murphy Copyright  1996, 1999, All rights reserved.
Recursion Pepper. Another way to loop Call yourself repeatedly until you say to stop. Example: add up 10 numbers using addUp. -- Input – number to count.
How to Create a Chapter Overview…. Expectations for Each Chapter Overview… 1.Hold the paper so the long side goes left to right.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Nottingham Image Analysis School, 23 – 25 June NITS Image Segmentation Guoping Qiu School of Computer Science, University of Nottingham
Image Operations Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Dictionaries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: Graphs Data Structures.
Computer Graphics Lecture 08 Taqdees A. Siddiqi Computer Graphics Filled Area Primitives I Lecture 08 Taqdees A. Siddiqi
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
CS552: Computer Graphics Lecture 16: Polygon Filling.
Flood fill algorithm Also called seed fill, is an algorithm that determines the area connected to a given node in a multi-dimensional array, When applied.
Adapted from slides by Marty Stepp and Stuart Reges
Polygon Fill Flood fill algorithm. The algorithm starts at a pixel and perform a depth first traversal of the adjacent pixels and fills the visited pixel.
Sets and Dictionaries Examples Copyright © Software Carpentry 2010
Recursion CSC 202.
CSC317 Graph algorithms Why bother?
Program Design Invasion Percolation: Resolving Ties
Program Design Invasion Percolation: Neighbors
Multiply Decimals.
More Recursion.
CSc 110, Spring 2017 Lecture 6: Parameters (cont.) and Graphics
Depth-First Search.
7.4 Problem Solving with Recursion
Programming Abstractions
CMSC201 Computer Science I for Majors Lecture 16 – Recursion
Vision processing for robot navigation
Graph Search Applications
Algorithm design and Analysis
INTERMEDIATE PROGRAMMING LESSON
Data Structures – Stacks and Queus
Flooding © 2018.
Agenda Polygon Terminology Types of polygons Inside Test
Gray Scale picture def pixBW(pixel): # given a pixel, change to BW
INTERMEDIATE PROGRAMMING LESSON
Agenda Polygon Terminology Types of polygons Inside Test
Search Related Algorithms
Lectures on Graph Algorithms: searching, testing and sorting
Version Control Introduction Copyright © Software Carpentry 2010
Program Design Invasion Percolation: Bugs
COMP171 Depth-First Search.
Review Mathematics Skills
Rasterizing Polygons Lecture 29 Wed, Dec 7, 2005.
Recursion Pepper.
Directions: Fill in the blanks with the correct answers then color in the picture with the given color. Color White: A full rotation around a.
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Op Art Directions.
Presentation transcript:

Counting Stars Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See for more information. Multimedia Programming

Counting Stars Problem: how many stars are in this image?

Multimedia ProgrammingCounting Stars Converted to black and white

Multimedia ProgrammingCounting Stars Converted to black and white How many black blobs?

Multimedia ProgrammingCounting Stars A "blob" is a group of adjacent pixels

Multimedia ProgrammingCounting Stars A "blob" is a group of adjacent pixels Decide that "adjacent" means 4-way, not 8-way

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

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

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

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?

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

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

Multimedia ProgrammingCounting Stars Scan to black pixelcount: 0

Multimedia ProgrammingCounting Stars Scan to black pixel Mark it seen count: 0

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

Multimedia ProgrammingCounting Stars Scan to black pixel Mark it seen Keep scanning count: 1

Multimedia ProgrammingCounting Stars Scan to black pixel Mark it seen Keep scanning Does have red predecessor, so this star already counted count: 1

Multimedia ProgrammingCounting Stars A new star!count: 2

Multimedia ProgrammingCounting Stars A new star! But this looks like a new star as well… count: 2

Multimedia ProgrammingCounting Stars A new star! But this looks like a new star as well… So look in more directions count: 2

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

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

Multimedia ProgrammingCounting Stars Still gives the wrong answer

Multimedia ProgrammingCounting Stars Solution: use flood fill to color in each star when it is first encountered

Multimedia ProgrammingCounting Stars Solution: use flood fill to color in each star when it is first encountered Find an uncolored pixel

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

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…

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…

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

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

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

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

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

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

Multimedia ProgrammingCounting Stars Use a work queue

Multimedia ProgrammingCounting Stars Use a work queue Keep list of (x, y) coordinates to be examined

Multimedia ProgrammingCounting Stars Use a work queue Keep list of (x, y) coordinates to be examined Loop until queue is empty:

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

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

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))

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))

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))

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))

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))

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))

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))

Multimedia ProgrammingCounting Stars Filling in action [(1, 2)]

Multimedia ProgrammingCounting Stars Filling in action [(0, 2), (2, 2), (1, 1), (1, 3)] X 2XX 3X

Multimedia ProgrammingCounting Stars Filling in action [(1, 3)]

Multimedia ProgrammingCounting Stars Filling in action [(1, 2), (0, 3), (2, 3), (1, 4)] X 3XX 4X

Multimedia ProgrammingCounting Stars Filling in action [(1, 2), (0, 3), (2, 3), (1, 4)] X 3XX 4X

Multimedia ProgrammingCounting Stars Filling in action [(1, 2), (0, 3), (2, 3), (1, 4)] X 3XX 4X Exercise: never look at a pixel twice

Multimedia ProgrammingCounting Stars Or use a recursive algorithm

Multimedia ProgrammingCounting Stars Or use a recursive algorithm Keep the work to be done on the call stack

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)

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)

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)

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)

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)

Multimedia ProgrammingCounting Stars Call stack holds pixels currently being examined fill(..., 3, 4) fill(..., 3, 3) fill(..., 2, 3) fill(..., 1, 3) fill(..., 1, 2) first call most recent call

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 for more information.