Finding Red Pixels – Part 1 Prof. Noah Snavely CS1114

Slides:



Advertisements
Similar presentations
Tutorial on Matlab and OpenCV Rui Ma TA of CMPT 414 May 14, 2013 Office hours: Fridays 11:00-12:00, CSIL TA Office 1 (ASB 9838)
Advertisements

R for Macroecology Aarhus University, Spring 2011.
RAPTOR Syntax and Semantics By Lt Col Schorsch
Polygons and the convex hull Prof. Noah Snavely CS1114
Sorting and selection – Part 2 Prof. Noah Snavely CS1114
CS 1114: Introduction to Computing Using MATLAB and Robotics Prof. Noah Snavely
Interpolation Prof. Noah Snavely CS1114
Linked lists Prof. Noah Snavely CS1114
Image transformations, Part 2 Prof. Noah Snavely CS1114
Finding Red Pixels – Part 2 Prof. Noah Snavely CS1114
Announcements Kevin Matzen office hours – Tuesday 4-5pm, Thursday 2-3pm, Upson 317 TA: Yin Lou Course lab: Upson 317 – Card access will be setup soon Course.
CS 1114: Introduction to Computing Using MATLAB and Robotics Prof. Graeme Bailey (notes modified from Noah Snavely, Spring.
Robustness and speed Prof. Noah Snavely CS1114
Finding Red Pixels – Part 1 Prof. Noah Snavely CS1114
R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE.
Linked lists and memory allocation Prof. Noah Snavely CS1114
First Bytes - LabVIEW. Today’s Session Introduction to LabVIEW Colors and computers Lab to create a color picker Lab to manipulate an image Visual ProgrammingImage.
Manipulating 2D arrays in Java
Computing transformations Prof. Noah Snavely CS1114
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Robust fitting Prof. Noah Snavely CS1114
CS 1114: Introduction to Computing Using MATLAB and Robotics Prof. Noah Snavely CS1114
Chapter 5. Loops are common in most programming languages Plus side: Are very fast (in other languages) & easy to understand Negative side: Require a.
CS 102 Computers In Context (Multimedia)‏ 01 / 28 / 2009 Instructor: Michael Eckmann.
Voronoi diagrams and applications Prof. Ramin Zabih
Arrays II Robin Burke IT 130. Outline Homework #6 Portfolio Array review Array examples Array algorithms Multi-dimensional arrays.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.
Image segmentation Prof. Noah Snavely CS1114
CS 1699: Intro to Computer Vision Matlab Tutorial Prof. Adriana Kovashka University of Pittsburgh September 3, 2015.
Interpolation Prof. Noah Snavely CS1114
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Finding Red Pixels Prof. Ramin Zabih
Medians and blobs Prof. Ramin Zabih
Robustness and speed Prof. Noah Snavely CS1114
Hard problems in computer science Prof. Noah Snavely CS1114
Picture Lab. Manipulating Pictures Discussion and activities with java.awt.Color introduce students to megapixels, pixels, the RGB color model, binary.
Clustering Prof. Ramin Zabih
Finding Red Pixels – Part 2 Prof. Noah Snavely CS1114
Matlab tutorial course Lesson 4: Writing your own functions: programming constructs
1 Computer Science 631 Multimedia Systems Prof. Ramin Zabih Computer Science Department CORNELL UNIVERSITY.
Fitting image transformations Prof. Noah Snavely CS1114
June 14, ‘99 COLORS IN MATLAB.
Sorting and selection – Part 2 Prof. Noah Snavely CS1114
Breadth-first and depth-first traversal Prof. Noah Snavely CS1114
Improving Matlab Performance CS1114
CS 1114: Finding things Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
CS 115 Lecture 17 2-D Lists Taken from notes by Dr. Neil Moore.
CompSci 101 Introduction to Computer Science March 8, 2016 Prof. Rodger.
Recognizing objects Prof. Noah Snavely CS1114
Image Processing Objectives To understand pixel based image processing
2-D Lists Taken from notes by Dr. Neil Moore
Image transformations
Image transformations
Each column represents another power of the base
CS 2770: Computer Vision Linear Algebra and Matlab
CS 1674: Intro to Computer Vision Linear Algebra Review
Data Structures – Stacks and Queus
More Image Manipulation
Looping through pixels.
Numbers and their bases
CS 106A, Lecture 18 Practice with 1D and 2D Arrays
Quickselect Prof. Noah Snavely CS1114
CS 1114: Introduction to Computing Using MATLAB and Robotics
Linked lists Prof. Noah Snavely CS1114
Bounding Boxes, Algorithm Speed
CSC1401 Manipulating Pictures 2
2-D Lists Taken from notes by Dr. Neil Moore
Sorting and selection Prof. Noah Snavely CS1114
Presentation transcript:

Finding Red Pixels – Part 1 Prof. Noah Snavely CS1114

2 Administrivia  Everyone should have a lab account, and cardkey access to CS319 –If not, let me know  Assignment 1 will be out tomorrow, due Friday, Feb. 12 –Will be graded in demo sessions  Office hours: –Prof. Snavely: Th 1:30 – 3:00 Upson 4157 –TA office hours are posted on the webpage  Quiz 1 will be next Thursday

3 Tracking a lightstick  We will spend the first part of CS1114 trying to track a red lightstick  On the way we will cover important CS themes –Fundamental algorithms –Good programming style –Computational problem solving

Human vision 4 Question: How many people are in this image? Source: “80 million tiny images” by Torralba, et al.

Human vision has its shortcomings… 5 Sinha and Poggio, Nature, 1996 Credit: Steve Seitz

Human vision has its shortcomings… 6 by Ted Adelson, slide credit Steve Seitz

What is an image?  A grid of numbers (intensity values)  In Matlab, a matrix 7 [ … ; … ; … ; … ] { { x 220 matrix

Matrices in Matlab  1D matrix is often called a vector –Similar to arrays in other languages 8 A = [ ] Row vector (or 1 x 5 matrix) B = [ 10 ; 30 ; 40 ; 106 ; 123 ] Column vector (or 5 x 1 matrix) A(1) == 10 A(4) == 106

Matrices in Matlab 9 C = [ ; ; ] 3 x 5 matrix C(1,1) == ? C(2,4) == ? can also assign to a matrix entries C(1,1) = C(1,1) + 1

Image processing  We often want to modify an image by “doing something” to each pixel: 10 BlurBrighten

Brightening an image 11 Q: What does this mean in terms of matrix entries? [ ; ; ] A: Increase each element by some amount (say, 20)

Brightening an image (Take 1) 12 D = [ ] 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(8) + 20; D(10) = D(10) + 20; D(11) = D(11) + 20; D(12) = D(12) + 20; D(13) = D(13) + 20; D(14) = D(14) + 20; D(15) = D(15) + 20; x

13 Avoiding duplicate code  Programming languages are designed to make this easy –It’s a huge theme in language design –Many new programming techniques are justified by this Object-oriented programming, higher-order procedures, functional programming, etc.

Why is it a bad idea to duplicate code? 14  Hard to write  Hard to modify  Hard to get right  Hard to generalize  Programmer’s “intent” is obscured 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(8) + 20; D(10) = D(10) + 20; D(11) = D(11) + 20; D(12) = D(12) + 20;

15 Brightening an image (Take 2)  Using iteration 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;  Much easier to understand and modify the code  Better expresses programmer’s “intent”

Many advantages to iteration  Can do things with iteration that you can’t do by just writing lots of statements  Example: increment every vector cell –Without knowing the length of the vector! len = length(D); % New Matlab function for i = 1:len D(i) = D(i) + 20; end 16

17 Introducing iteration into code  Programming often involves “clichés” –Patterns of code rewriting –I will loosely call these “design patterns”  Iteration is our first example

Brightening 2D images 18 C = [ ; ; ] 3 x 5 matrix for row = 1:3 for col = 1:5 C(row,col) = C(row,col) + 20; end Called a “nested” for loop

Brightening 2D images 19 for row = 1:3 for col = 1:5 C(row,col) = C(row,col) + 20 end  What if it’s not a 3x5 matrix? [nrows,ncols] = size(C) for row = 1:nrows for col = 1:ncols C(row,col) = C(row,col) + 20 end

20 Using iteration to count nzeros = 0; [nrows,ncols] = size(D); for row = 1:nrows for col = 1:ncols if D(row,col) == 0 nzeros = nzeros + 1; end; D = [ ; ; ]

21 Using iteration to count nzeros = 0; [nrows,ncols] = size(D); for row = 1:nrows for col = 1:ncols if D(row,col) == 0 nzeros = nzeros + 1; end; If D is an image, what are we counting?

What about red pixels?  A grayscale image is a 2D array –Brightest = 255, darkest = 0 22

What about red pixels?  A color image is 3 different 2D arrays –For red/green/blue values (RGB) –We provide a way to create these 3 arrays 23 =

What about red pixels?  Example colors: red(1,1) == 255, green(1,1) == blue(1,1) == 0 red(2,1) == 100 == green(2,1) == blue(2,1) red(3,1) == 0 == green(3,1) == blue(3,1) red(3,1) == 255 == green(3,1), blue(3,1) == 0 24

25 How many red pixels? img = imread(‘wand1.bmp’); [red, green, blue] = image_rgb(img); nreds = 0; [nrows,ncols] = image_size(img); for row = 1:nrows for col = 1:ncols if red(row,col) == 255 nreds = nreds + 1; end;

26 Are we done?  We’ve counted the red pixels in Matlab –Or have we? What can go wrong?  Suppose we can pick a good constant instead of 255  You need to look at green and blue also  Color perception is really hard! – PPT demo

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

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

How many black pixels? 29 nzeros = 0; [nrows,ncols] = size(D); for row = 1:nrows for col = 1:ncols if D(row,col) == 0 nzeros = nzeros + 1; end What if we need to execute this code many times?

Turning this into a function 30 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([ ])

For next time  Visit the lab, try out the rest of the Matlab tutorial  Watch out for assignment 1 31