Finding Red Pixels – Part 2 Prof. Noah Snavely CS1114

Slides:



Advertisements
Similar presentations
Announcements You survived midterm 2! No Class / No Office hours Friday.
Advertisements

Working with images and scenes CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.5 TexPoint fonts used in EMF. Read the TexPoint manual before you delete.
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
End User Computing An Introduction to CSS Sujana Jyothi Research Lab1, Callan Building, Department of Computer Science
Sorting and Selection, Part 1 Prof. Noah Snavely CS1114
Polygons and the convex hull Prof. Noah Snavely CS1114
Sorting and selection – Part 2 Prof. Noah Snavely CS1114
Interpolation Prof. Noah Snavely CS1114
Linked lists Prof. Noah Snavely CS1114
CS 1114: Sorting and selection (part one) Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Finding Red Pixels – Part 2 Prof. Noah Snavely CS1114
CS503: First Lecture, Fall 2008 Michael Barnathan.
CS 1114: Introduction to Computing Using MATLAB and Robotics Prof. Graeme Bailey (notes modified from Noah Snavely, Spring.
Maze Running Robots EGR106 Project Spring Project Goal Computer control (through a Matlab program) of a Lego robot to: 1.Explore a maze (start to.
Lec 5 Feb 10 Goals: analysis of algorithms (continued) O notation summation formulas maximum subsequence sum problem (Chapter 2) three algorithms image.
Robustness and speed Prof. Noah Snavely CS1114
Finding Red Pixels – Part 1 Prof. Noah Snavely CS1114
Blobs and Graphs Prof. Noah Snavely CS1114
R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE.
Polygons and the convex hull Prof. Noah Snavely CS1114
Linked lists and memory allocation Prof. Noah Snavely CS1114
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
Search Lesson CS1313 Spring Search Lesson Outline 1.Searching Lesson Outline 2.How to Find a Value in an Array? 3.Linear Search 4.Linear Search.
Robust fitting Prof. Noah Snavely CS1114
MATLAB Programming Session
CSC589 Introduction to Computer Vision Lecture 8
Unit 1 (Chapter 5): Significant Figures
Voronoi diagrams and applications Prof. Ramin Zabih
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
CS1Q Computer Systems Lecture 8
Image segmentation Prof. Noah Snavely CS1114
Interpolation Prof. Noah Snavely CS1114
C++ crash course Class 8 statements, sort, flight times program.
Finding Red Pixels Prof. Ramin Zabih
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 23: Review.
Medians and blobs Prof. Ramin Zabih
Robustness and speed Prof. Noah Snavely CS1114
Finding Red Pixels – Part 1 Prof. Noah Snavely CS1114
Clustering Prof. Ramin Zabih
10/15/02 (c) 2002 University of Wisconsin, CS559 Who Am I? Prof Stephen Chenney These notes will be online after the lecture – in fact they’re online already.
CS 325 Introduction to Computer Graphics 04 / 12 / 2010 Instructor: Michael Eckmann.
June 14, ‘99 COLORS IN MATLAB.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
Sorting and selection – Part 2 Prof. Noah Snavely CS1114
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
CSSE463: Image Recognition Day 9 Lab 3 (edges) due Weds, 3:25 pm Lab 3 (edges) due Weds, 3:25 pm Take home quiz due Friday, 4:00 pm. Take home quiz due.
Searching Topics Sequential Search Binary Search.
Computer Science Unplugged Dr. Tom Cortina Carnegie Mellon University Sponsored by.
CS 1114: Finding things Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
Recognizing objects Prof. Noah Snavely CS1114
AP CSP: Pixelation – B&W/Color Images
Pixels, Colors and Shapes
EGR 2261 Unit 10 Two-dimensional Arrays
UNIT 2 – LESSON 3 Encoding B&W Images.
Week 9 - Monday CS 113.
Garbage collection; lightstick orientation
Each column represents another power of the base
Color Values All colors in computer images are a combination of red, green and blue Each component is encoded as a number means the color is.
CS 1114: Sorting and selection (part three)
Data Structures – Stacks and Queus
Numbers and their bases
So where is it anyway? Bounding box, median, centroids, and an introduction to algorithm analysis.
Quickselect Prof. Noah Snavely CS1114
CS 1114: Sorting and selection (part two)
CS 1114: Introduction to Computing Using MATLAB and Robotics
Bounding Boxes, Algorithm Speed
Quicksort and selection
Working with images and scenes
Sorting and selection Prof. Noah Snavely CS1114
Presentation transcript:

Finding Red Pixels – Part 2 Prof. Noah Snavely CS1114

Administrivia  You should all set up your CSUG accounts  Your card should now unlock Upson 319 2

Administrivia  Assignment 1 posted, due Friday, 2/10 by 5pm –Look under “Assignments!” –You should have seen the post on Piazza If not, let me know  Quiz 1 on Thursday 3

Academic Integrity  You may speak to others about the assignments, but may not take notes  All code you write must be your own 4

5 Administrivia  Office hours: –Prof. Snavely: Th 1:30 – 3pm Upson 4157 –All other office hours are held in the lab, see staff page for times

Even more compact code  Special Matlab “Vectorized” code  Usually much faster than loops  But please use for loops for assignment 1 6 for i = 1:12 D(i) = D(i) + 20; end D(1) = D(1) + 20; D(2) = D(2) + 20; D(3) = D(3) + 20; D(4) = D(4) + 20; D(5) = D(5) + 20; D(6) = D(6) + 20; D(7) = D(7) + 20; D(8) = D(8) + 20; D(9) = D(9) + 20; D(10) = D(10) + 20; D(11) = D(11) + 20; D(12) = D(12) + 20; D = D + 20; D = [ ]

Why 256 intensity values? 7 8-bit intensity (2^8 = 256) 5-bit intensity (2^5 = 32) 5-bit intensity with noise

Why 256 intensity values? 8 4-color CGA display Today’s (typical) displays: 256 * 256 * 256 = 16,777,216 colors 256-color Amiga display

Counting black pixels 9 function [ nzeros ] = count_zeros(D) % Counts the number of zeros in a matrix nzeros = 0; [nrows,ncols] = size(D); for row = 1:nrows for col = 1:ncols if D(row,col) == 0 nzeros = nzeros + 1; end Save in a file named count_zeros.m count_zeros([ ])

What about red pixels? 10 = + + RGB red(1,1) == 255, green(1,1) == blue(1,1) == 0 red(1,1) == 255, green(1,1) == 255, blue(1,1) == 0

Are we done?  Assignment 1: come up with a thresholding function that returns 1 if a pixel is “reddish”, 0 otherwise 11 binary images

Finding the lightstick  We’ve answered the question: is there a red light stick?  But the robot needs to know where it is! 12

13 Finding the rightmost red pixel  We can always process the red pixels as we find them: right = 0; for row = 1:nrows for col = 1:ncols if red(row,col) == 255 right = max(right,col); end

14 Finding the lightstick – Take 1  Compute the bounding box of the red points  The bounding box of a set of points is the smallest rectangle containing all the points –By “rectangle”, I really mean “rectangle aligned with the X,Y axes”

Finding the bounding box  Each red pixel we find is basically a point –It has an X and Y coordinate –Column and row Note that Matlab reverses the order 15

What does this tell us?  Bounding box gives us some information about the lightstick Midpoint  rough location Aspect ratio  rough orientation (aspect ratio = ratio of width to height) 16 Aspect ratio: 2.05/1.08 = " 2.05"

17 Computing a bounding box  Two related questions: –Is this a good idea? Will it tell us reliably where the light stick is located? –Can we compute it quickly?

Computing a bounding box  Lots of CS involves trying to find something that is both useful and efficient –To do this well, you need a lot of clever ways to efficiently compute things (i.e., algorithms) –We’re going to learn a lot of these in CS

19 Beyond the bounding box  Computing a bounding box isn’t hard –Hint: the right edge is computed by the code we showed a few slides ago –You’ll write this and play with it in A2  Does it work?

20 Finding the lightstick – Take 2  How can we make the algorithm more robust? –New idea: compute the centroid  Centroid: (average x-coordinate, average y-coordinate) –If the points are scattered uniformly, this is the same as the midpoint of the bounding box –Average is sometimes called the mean –Centroid = center of mass

21 Computing the centroid?  We could do everything we want by simply iterating over the image as before –Testing each pixel to see if it is red, then doing something to it  It’s often easier to iterate over just the red pixels  To do this, we will use the Matlab function called find

The find function 22 img thresh Your thresholding function [X,Y] = find(thresh); X: x-coords of nonzero points Y: y-coords of nonzero points

23 Using find on images  We can get the x- and y- coordinates of every red pixel using find –Now all we need to do is to compute the average of these numbers –We will leave this as a homework exercise You might have done this in high school

24 Q: How well does this work?  A: Still not that well –One “bad” red point can mess up the mean  This is a well-known problem –What is the average weight of the people in this kindergarten class photo?

How well does this work? 25

How can we do better?  What is the average weight of the people in this kindergarten class photo? kids, avg. weight= 40 lbs 1 Arnold, weight = 236 lbs Mean: (12 x ) / 13 = 55 lbs

How can we do better?  Idea: remove maximum value, compute average of the rest kids, avg. weight= 40 lbs 1 Arnold, weight = 236 lbs Mean: (12 x ) / 13 = 55 lbs Mean: 40lbs

28 How can we avoid this problem?  Consider a simple variant of the mean called the “trimmed mean” –Simply ignore the largest 5% and the smallest 5% of the values –Q: How do we find the largest 5% of the values? D.E. Knuth, The Art of Computer Programming Chapter 5, pages 1 – 391

29 Easy to find the maximum element in an array A = [ ]; m = -1; for i = 1:length(A) if (A(i) > m) m = A(i); end % At the end of this loop, m contains the % biggest element of m (in this case, 503) % Why -1?

 First, we need to know how many cells we’re dealing with –Let’s say length(array) is 100  want to remove top 5  How do we remove the biggest 5 numbers from an array? 30 How to get top 5%?

% A is a vector of length 100 for i = 1:5 % 1. Find the maximum element of A % 2. Remove it end 31 Removing the top 5% -- Take 1

32 How good is this algorithm?  Is it correct?  Is it fast?  Is it the fastest way? % A is a vector of length 100 for i = 1:5 % 1. Find the maximum element of A % 2. Remove it end

How do we define fast?  It’s fast when length(A) = 20  We can make it faster by upgrading our machine  So why do we care how fast it is?  What if length(A) = 6,706,993,152 ? 33

How do we define fast?  We want to think about this issue in a way that doesn’t depend on either: A. Getting really lucky input B. Happening to have really fast hardware 34

35 How fast is our algorithm?  An elegant answer exists  You will learn it in later CS courses –But I’m going to steal their thunder and explain the basic idea to you here –It’s called “big-O notation”  Two basic principles: –Think about the average / worst case Don’t depend on luck –Think in a hardware-independent way Don’t depend on Intel!

For next time  Attend section tomorrow in the lab  Reminder: Quiz on Thursday, beginning of class 36