COS 126 Atomic Theory of Matter 1
Goal of the Assigmet Video Calculate Avogadro s umber Usig Eistei s equatios Usig fluorescet imagig Iput data Output Frames Blobs/Beads Estimate of Avogadro s umber 7.1833 4.7932 2.1693 5.5287 5.4292... Displacemets 2
Overview Four Classes Blob BeadFider Maximal set of coected light pixels cliet of Fid all blobs i a JPEG image List all the big blobs (aka beads) cliet of video data Avogadro BeadTracker Boltzma Avogadro Data aalysis to estimate Avogadro s umber from the motio of beads displacemet data Track beads from oe image to the ext Pictures (frames) Commad lie args 3
Atomic Theory Overview Browia Motio Radom collisio of molecules Displacemet over time fits a Gaussia distributio 4
Atomic Theory Overview Avogadro s Number Number of atoms eeded to equal substace s atomic mass i grams N A atoms of Carbo-12 = 12 grams N A == 6.0221367 x 10 +23 Ca calculate from Browia Motio Variace of Gaussia distributio is a fuctio of resistace i water, umber of molecules 5
Blob.java API for represetig particles (blobs) i water public Blob() costructor public void add(it i, it j) add pixel at i,j to Blob public it mass() umber of pixels i Blob public double distaceto(blob b) Euclidea distace betwee the ceter of masses betwee Blobs public Strig tostrig() a strig represetatio of this Blob public static void mai(strig[] args) uit tests all methods i the Blob data type Ceter of mass, ad # of pixels 6
Blob.java Ceter of mass Oly eed three istace variables Do ot store the positios of every pixel i the blob Two alteratives: umber of poits, x-coordiate ceter of mass, ad y- coordiate ceter of mass) or umber of poits, sum of x-coordiates, ad sum of y- coordiates) eeded to compute the ceter-of-mass Ceter of mass, ad # of pixels Ceter is ot iteger values! 7
Blob Challeges Format umbers i a ice way Strig.format("%2d (%8.4f, %8.4f)", mass, cx, cy); (Use same format i System.out.pritf()) E.g., "%6.3f" -> _2.354 E.g., "%10.4e" -> 1.2535e-23 Thoroughly test Create a simple mai() Test ALL methods 8
BeadFider.java Locate all blobs i a give image API Ad idetify large blobs (called beads) public BeadFider(Picture picture, double threshold) Calculate lumiace (see Lumiace.java, 3.1) Iclude pixels with a lumiace >= threshold Fid blobs with DFS (see Percolatio.java, 2.4) The hard part, ext slide public Blob[] getbeads(it misize) Returs all beads with at least misize pixels Array must be of size equal to umber of beads 9
BeadFider - Depth First Search Use boolea[][] array to mark visited Traverse image pixel by pixel Dark pixel Mark as visited, cotiue Light pixel Create ew blob, call DFS DFS algorithm Base case: simply retur if Pixel out-of-bouds Pixel has bee visited Pixel is dark (ad mark as visited) Add pixel to curret blob, mark as visited Recursively visit up, dow, left, ad right eighbors 10
BeadFider - Depth First Search Use boolea[][] array to mark visited Traverse image pixel by pixel Dark pixel Mark as visited, cotiue Light pixel Create ew blob, call DFS DFS algorithm Base case: simply retur if Pixel out-of-bouds Pixel has bee visited Pixel is dark (ad mark as visited) Add pixel to curret blob, mark as visited Recursively visit up, dow, left, ad right eighbors 11
BeadFider - Depth First Search Use boolea[][] array to mark visited Traverse image pixel by pixel Dark pixel Mark as visited, cotiue Light pixel Create ew blob, call DFS DFS algorithm Base case: simply retur if Pixel out-of-bouds Pixel has bee visited Pixel is dark (ad mark as visited) Add pixel to curret blob, mark as visited Recursively visit up, dow, left, ad right eighbors 12
BeadFider - Depth First Search Use boolea[][] array to mark visited Traverse image pixel by pixel Dark pixel Mark as visited, cotiue Light pixel Create ew blob, call DFS DFS algorithm Base case: simply retur if Pixel out-of-bouds Pixel has bee visited Pixel is dark (ad mark as visited) Add pixel to curret blob, mark as visited Recursively visit up, dow, left, ad right eighbors 13
BeadFider - Depth First Search Use boolea[][] array to mark visited Traverse image pixel by pixel Dark pixel Mark as visited, cotiue Light pixel Create ew blob, call DFS DFS algorithm Base case: simply retur if Pixel out-of-bouds Pixel has bee visited Pixel is dark (ad mark as visited) Add pixel to curret blob, mark as visited Recursively visit up, dow, left, ad right eighbors 14
BeadFider - Depth First Search Use boolea[][] array to mark visited Traverse image pixel by pixel Dark pixel Mark as visited, cotiue Light pixel Create ew blob, call DFS DFS algorithm Base case: simply retur if Pixel out-of-bouds Pixel has bee visited Pixel is dark (ad mark as visited) Add pixel to curret blob, mark as visited Recursively visit up, dow, left, ad right eighbors 15
BeadFider Challeges Data structure for the collectio of blobs Store them ay way you like But be aware of memory use ad timig 16
BeadFider Challeges Data structure for the collectio of blobs Store them ay way you like But be aware of memory use ad timig Array of blobs? But how big should the array be? Liked list of blobs? Memory efficiet, but harder to implemet Avoid traversig whole list to add a blob! Aythig else? Submit your (extra) object classes if ot i 4.3 17
BeadTracker.java Track beads betwee successive images Sigle mai fuctio Take i a series of images Output distace traversed by all beads for each time-step For each bead foud at time t+1, fid closest bead at time t ad calculate distace Not the other way aroud! Do t iclude if distace > 25 pixels (ew bead) 18
BeadTracker Challeges Readig multiple iput files java-itrocs BeadTracker ru_1/*.jpg Expads files i alphabetical order Ed up as args[0], args[1], Avoidig ruig out of memory How? Recompilig Recompile if Blob or BeadFider chage 19
BeadTracker Challeges Readig multiple iput files java-itrocs BeadTracker ru_1/*.jpg Expads files i alphabetical order Ed up as args[0], args[1], Avoidig ruig out of memory Do ot ope all picture files at same time Various ways to do this Recompilig Recompile if Blob or BeadFider chage 20
BeadTracker Challeges Avoid ruig out of memory (1) File cotaiig frame t 0 File cotaiig frame t 1 File cotaiig frame t 2 File cotaiig frame t beads t 0 beads t 1 radial displacemet 0 Oly eed two ope at a time. 21
BeadTracker Challeges Avoid ruig out of memory (1) File cotaiig frame t 0 File cotaiig frame t 1 File cotaiig frame t 2 File cotaiig frame t beads t 1 beads t 2 radial displacemet 1 Oly eed two ope at a time. 22
BeadTracker Challeges Avoid ruig out of memory (2) File cotaiig frame t 0 File cotaiig frame t 1 File cotaiig frame t 2 File cotaiig frame t beads t 0 beads t 1 beads t 2 radial displacemet 0 radial displacemet 1 No eed to re-fid beads use the beads t 1 foud i radial displacemet 0 computatio i the computatio of radial displacemet 1 23
Avogadro.java Aalyze Browia motio of all calculated displacemets Itricate formulas, all give Be careful about uits i the math, covert pixels to meters, etc. Ca test without the other parts workig We provide sample iput files Ca work o it while waitig for help 24
Coclusio: Fial Tips Avoidig subtle bugs i BeadFider Double check what happes at corer cases (e.g., at boudary pixels, or whe lumiace == tau, or mass == cutoff) Commo errors i BeadFider NullPoiterExceptio StackOverflowError (e.g., if o base case) No output (eed to add prits) Look at checklist Q&A 25
Coclusio: Fial Tips Testig with a mai() Blob Test all methods BeadFider, BeadTracker, ad Avogadro Must have a mai() that ca hadle I/O described i Testig sectio of checklist Timig aalysis Look at feedback from earlier assigmets N == umber of pixels (ot frames) How ca you ru 100 frames? 26