DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages...
Transcript of DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages...
![Page 1: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/1.jpg)
DarkRoom YEAH HoursPeter Maldonado and Andrew Tierno
![Page 2: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/2.jpg)
Outline For Today
● Array Review
● 2D Arrays (Grids)
● GImages and Pixels
● DarkRoom Overview
● Tips and Tricks
![Page 3: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/3.jpg)
Array ReviewHow do we efficiently store data?
![Page 4: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/4.jpg)
ArraysWhy Arrays?● Great for representing a fixed size list of homogenous data● Efficient and lightweight
Arrays access data using numerical indices with zero-indexing● First element at index 0● Last element at index length - 1
Can store both objects (references) and primitives (values)
Arrays are objects but don’t have any methods!
![Page 5: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/5.jpg)
Array OperationsTo create a new Array, we specify a Type and max SIZE in a call to new
Type[] myArray = new Type[SIZE];To access an element in the array, use square brackets to select the index
myArray[index];(This expression is to a reference to that position in the array)
Arrays have a single variable that stores their lengthmyArray.length;
![Page 6: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/6.jpg)
Flipping an Array
orig
2 4 6 8
0 1 2 3
How many elements do we flip?
Is this number relative to the size of the array?
![Page 7: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/7.jpg)
Flipping an Array
flip
2 8
4 6
0 1 2 3
How many elements do we flip?
Is this number relative to the size of the array?
![Page 8: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/8.jpg)
Flipping an Array
flip
8 2
4 6
0 1 2 3
How many elements do we flip?
Is this number relative to the size of the array?
![Page 9: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/9.jpg)
Flipping an Array
flip
8 4 6 2
0 1 2 3
How many elements do we flip?
Is this number relative to the size of the array?
![Page 10: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/10.jpg)
Flipping an Array
flip
4 6
8 2
0 1 2 3
How many elements do we flip?
Is this number relative to the size of the array?
![Page 11: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/11.jpg)
Flipping an Array
flip
6 4
8 2
0 1 2 3
How many elements do we flip?
Is this number relative to the size of the array?
![Page 12: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/12.jpg)
Flipping an Array
flip
8 6 4 2
0 1 2 3
How many elements do we flip?
Is this number relative to the size of the array?
![Page 13: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/13.jpg)
Flipping an Array
How many elements do we flip? Swap each element at the front with one at the back.
Is this number relative to the size of the array?Only need to perform orig.length swaps!
orig
2 4 6 8
0 1 2 3
flip
8 6 4 2
0 1 2 3
![Page 14: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/14.jpg)
Flipping an int Array
private void flip(int[] toFlip) { for (int i = 0; i < toFlip.length / 2; i++) { int tmp = toFlip[i]; toFlip[i] = toFlip[toFlip.length – i – 1]; toFlip[toFlip.length - i - 1] = tmp; }}
How would we change this to work with types other than int?
How might we flip a GImage? (Think about this…)
![Page 15: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/15.jpg)
2D Arrays! (Grids)What if our Type was an array? (e.g. int[])
![Page 16: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/16.jpg)
Type[] myGrid = new Type[numElems];
![Page 17: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/17.jpg)
Type[] myGrid = new Type[numElems];Let’s try setting Type to int[]
![Page 18: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/18.jpg)
int[][] myGrid = new int[numArrays][numElems];
![Page 19: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/19.jpg)
int[][] myGrid = new int[numArrays][numElems];Why does this work?
Because each int[] is an object!
![Page 20: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/20.jpg)
int[][] myGrid = new int[numArrays][numElems];We make an array of integer arrays!(Why we say the array spans two dimensions)
![Page 21: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/21.jpg)
int[][] myGrid = new int[numArrays][numElems];Note: we specify the size of each dimension (numArrays and numElems)
![Page 22: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/22.jpg)
Type[][] myGrid = new Type[rows][cols];We say the grid is “row-major”● First dimension is rows● Second dimension is columns
Each row of the grid is an array
Each column is defined by an index in each row array
Each element is located in a specific column in a specific row
2D Arrays (or Grid)
![Page 23: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/23.jpg)
Interpreting Multidimensional Arrays
As a 2D Grid
Looking up arr[row][col] selects the element in the grid at position (row, col)
Remember that the bracket order matters!The first set of brackets is row, the second is column
As an Array of Arrays
Looking up arr[n] gives back a one-dimensional array representing the n+1-th rowRemember that expression is a reference to that array!
First dimension indexes into different arrays, the second indexes elements in a given array
![Page 24: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/24.jpg)
int[][] multiArr = new int[4][5];What type do these expressions evaluate to? (What is returned?)
multiArr[1]?int[5] -> a reference to an array of five integers
multiArr[3][4]?int -> a single integer value
2D Arrays Examples
![Page 25: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/25.jpg)
Changing 2D Arrays with References
public void run() { int[][] full = new int[3][3]; int[] row = full[1]; row[1] = 4;
int[] newRow = new int[3]; newRow[0] = 6; full[2] = newRow;}
What does this do?
![Page 26: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/26.jpg)
Changing 2D Arrays with References
public void run() { int[][] full = new int[3][3]; int[] row = full[1]; row[1] = 4;
int[] newRow = new int[3]; newRow[0] = 6; full[2] = newRow;}
What does this do?
![Page 27: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/27.jpg)
full
- - - full[0]
full[1]
full[2]
- - -
- - -
Changing 2D Arrays with References
public void run() { int[][] full = new int[3][3]; int[] row = full[1]; row[1] = 4;
int[] newRow = new int[3]; newRow[0] = 6; full[2] = newRow;}
![Page 28: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/28.jpg)
row
full
- - - full[0]
full[1]
full[2]
- 4 -
- - -
Changing 2D Arrays with References
public void run() { int[][] full = new int[3][3]; int[] row = full[1]; row[1] = 4;
int[] newRow = new int[3]; newRow[0] = 6; full[2] = newRow;}
![Page 29: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/29.jpg)
6 - -newRow
row
full
- - - full[0]
full[1]
full[2]
- 4 -
- - -
Changing 2D Arrays with References
public void run() { int[][] full = new int[3][3]; int[] row = full[1]; row[1] = 4;
int[] newRow = new int[3]; newRow[0] = 6; full[2] = newRow;}
![Page 30: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/30.jpg)
6 - -newRow
row
full
- - - full[0]
full[1]
full[2]
- 4 -
Changing 2D Arrays with References
public void run() { int[][] full = new int[3][3]; int[] row = full[1]; row[1] = 4;
int[] newRow = new int[3]; newRow[0] = 6; full[2] = newRow;}
![Page 31: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/31.jpg)
6 - -newRow
row
full
- - - full[0]
full[1]
full[2]
- 4 -
Changing 2D Arrays with References
public void run() { int[][] full = new int[3][3]; int[] row = full[1]; row[1] = 4;
int[] newRow = new int[3]; newRow[0] = 6; full[2] = newRow;}
![Page 32: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/32.jpg)
[0][0] [0][1] [0][2] [0][3]
[1][0] [1][1] [1][2] [1][3]
[2][0] [2][1] [2][2] [2][3]
swap[1] swap[2][3]columns
rowsDimension Swapping — int[][] swap = new int[3][4];
![Page 33: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/33.jpg)
[0][0] [0][1] [0][2] [0][3]
[1][0] [1][1] [1][2] [1][3]
[2][0] [2][1] [2][2] [2][3]
swap[1] swap[2][3]columns
rowsDimension Swapping — int[][] swap = new int[3][4];
![Page 34: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/34.jpg)
[0][0] [0][1] [0][2] [0][3]
[1][0] [1][1] [1][2] [1][3]
[2][0] [2][1] [2][2] [2][3]
swap[1] swap[2][3]columns
rowsDimension Swapping — int[][] swap = new int[3][4];
![Page 35: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/35.jpg)
[0][0] [0][1] [0][2] [0][3]
[1][0] [1][1] [1][2] [1][3]
[2][0] [2][1] [2][2] [2][3]
swap[1] swap[2][3]columns
rowsDimension Swapping — int[][] swap = new int[3][4];
![Page 36: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/36.jpg)
[0][0] [0][1] [0][2] [0][3]
[1][0] [1][1] [1][2] [1][3]
[2][0] [2][1] [2][2] [2][3]
swap[1] swap[2][3]columns
rowsDimension Swapping — int[][] swap = new int[3][4];
![Page 37: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/37.jpg)
[0][0] [0][1] [0][2]
[1][0] [1][1] [1][2]
[2][0] [2][1] [2][2]
[3][0] [3][1] [3][2]
swap[1]
swap[2][3]columns
Dimension Swapping — int[][] swap2 = new int[4][3];
rows
???
![Page 38: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/38.jpg)
Dimension SwappingSwapping the order of dimension sizes has real consequences on how data is stored!Remember that 2D arrays always follow int[][] myGrid = new int[numArrays][numElems];
This doesn’t change the total number of elements, but flips number of rows with number of columns
Often times we’ll iterate through an entire grid and won’t see struggle with this directly
![Page 39: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/39.jpg)
Iterating Through 2D Arrays
int[][] arr = /* ... */
/* arr.length is numArrays from declaration */for (int row = 0; row < arr.length; row++) {
/* arr[row] is an array of size numElems */ for (int col = 0; col < arr[row].length; col++) { /* access arr[row][col] */ }}
![Page 40: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/40.jpg)
Iterating Through 2D Arrays Example 1
double[][] foo = new double[2][3];
for (int row = 0; row < foo.length; row++) { for (int col = 0; col < foo[row].length; col++) { arr[row][col] = col / (double) (row + 1); }}
0.0 1.0 2.0
0.0 0.5 1.0
![Page 41: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/41.jpg)
Iterating Through 2D Arrays Example 2
int[][] bar = new int[2][3];for (int row = 0; row < bar.length; row++) { for (int col = 0; col < bar[row].length; col++) { if (row == col) { arr[row][col] = 1; } else { arr[row][col] = 0; } }}
1 0 0
0 1 0
![Page 42: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/42.jpg)
There are two main ways to think about a 2D Array
Type[][] myGrid = new Type[rows][cols];
Type[][] myArr = new Type[numArr][numElem];When using a 2D Array● First dimension represents rows and indexing evaluates to an array● Second dimension represents columns and indexing evaluates to a value● Must index into rows before columns
2D Array Summary
![Page 43: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/43.jpg)
GImages and PixelsHow do manipulate images?
(Based on slides by Gaby Candes)
![Page 44: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/44.jpg)
What’s a GImage?GImages are grids (or 2D Arrays!) of pixels
GImage img = new GImage(“watermelon.jpg”);We represent pixels as integers, and can convert GImage to int[][]
int[][] pixels = img.getPixelArray();
![Page 45: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/45.jpg)
ProTip: Make Helper Methods!When using a pixel array from a GImages, the image height is the array numRows and the image width is the array numCols int[][] pixelArr = img.getPixelArray(); //int[numRows][numCols
Use helper methods to keep track of your image height/width!
private int numRows(int[][] pixels) { return pixels.length; // height}
private int numCols(int[][] pixels) { return pixels[0].length; // width}
![Page 46: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/46.jpg)
What’s a pixel? Why is it an int?Each pixel is an integer whose value represents the red, green, blue (RGB) intensity at that location.
We use a scale of 0-255 for the intensity of each color component
// get the R, G, B values for a particular pixel (type int)int red = GImage.getRed(pixel);int green = GImage.getGreen(pixel);int blue = GImage.getBlue(pixel);
// make a new pixel (type int) from certain R, G, B valuesint newPixel = GImage.createRGBPixel(red, green, blue);
![Page 47: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/47.jpg)
Iterating Through Pixel Array// get the dimensions of the arrayint imgRows = numRows(pixels);int imgCols = numCols(pixels);
// iterate over pixel arrayfor (int r = 0; r < imgRows; r++) { for (int c = 0; c < imgCols; c++) { // get a specific pixel from your image int newPixel = pixels[r][c];
// do something to newPixel, like make it cardinalpixels[r][c] = GImage.createRGBPixel(196, 30, 58);
}}
![Page 48: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/48.jpg)
// 1. make (or get) a 2D arrayint[][] pixels = /* ... */
// 2. fill (or modify) the array
// 3. make a GImage from 2D arrayGImage newImage = new GImage(pixels);
Making a New GImage
![Page 49: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/49.jpg)
DarkRoom OverviewAssignment 5
![Page 50: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/50.jpg)
Big PictureDarkRoom.java and DarkRoomAlgorithmsInterface.java are written for you and provided with the starter code● Together, these files handle user interaction (making buttons, handling
clicks, loading images, etc)● You aren’t responsible for understanding the code in these
You need to write DarkRoomAlgorithms.java● Consists of writing different methods that take in GImage parameters and
return modified GImages
What methods do you have to do?
![Page 51: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/51.jpg)
Rotations!
![Page 52: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/52.jpg)
Rotations!
0 1 2 3 4 50 A B C D E F
1 G H I J K L
2 M N O P Q R
3 S T U V W X
0 1 2 30 F L R X
1 E K Q W
2 D J P V
3 C I O U
4 B H N T
5 A G M S
What are the dimensions of orig? What are the dimensions of rotated?
What are the coordinates of Q in orig? What are they in rotated?
orig rotated
![Page 53: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/53.jpg)
Rotations!
0 1 2 3 4 50 A B C D E F
1 G H I J K L
2 M N O P Q R
3 S T U V W X
0 1 2 30 F L R X
1 E K Q W
2 D J P V
3 C I O U
4 B H N T
5 A G M S
What are the dimensions of orig? What are the dimensions of rotated? orig has 4 rows and 6 columns, rotated has 6 rows and 4 columns
What are the coordinates of Q in orig? What are they in rotated? orig: row 2, column 4, rotated: row 1, column 2
orig rotated
![Page 54: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/54.jpg)
Flipping!
![Page 55: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/55.jpg)
Flipping!
0 1 2 3 4 50 A B C D E F
1 G H I J K L
2 M N O P Q R
3 S T U V W X
What are the dimensions of orig? What are the dimensions of flipped?
What are the coordinates of Q in orig? What are they in flipped?
orig flipped
0 1 2 3 4 50 F E D C B A
1 L K J I H G
2 R Q P O N M
3 X W V U T S
![Page 56: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/56.jpg)
Flipping!
0 1 2 3 4 50 A B C D E F
1 G H I J K L
2 M N O P Q R
3 S T U V W X
What are the dimensions of orig? What are the dimensions of flipped? orig has 4 rows and 6 columns, flipped has 4 rows and 6 columns
What are the coordinates of Q in orig? What are they in flipped? orig: row 2, column 4, flipped: row 2, column 1
Check out flipVertical from Section 6!
orig flipped
0 1 2 3 4 50 F E D C B A
1 L K J I H G
2 R Q P O N M
3 X W V U T S
![Page 57: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/57.jpg)
Negative!
The red, blue and green values of every pixel should be set to 255 - k where k represents each of these values (make 255 a constant!)
Remember we can use:
int red = GImage.getRed(pixel);//Same for blue and green//Write more cool codeint newPixel = GImage.createRGBPixel(newRed, newGreen, newBlue);
![Page 58: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/58.jpg)
Green Screen
![Page 59: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/59.jpg)
Green Screen
Super Duper Karel?
Capoorel?
![Page 60: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/60.jpg)
Green Screen
Make any pixel whose green component is twice as big as the larger of its red or blue components transparent. Hint: Try using Math.max(a,b)!
Opacity is called alpha in CS terms. An alpha of 255 is completely opaque, an alpha of 0 is completely transparent. We can make a pixel with a particular alpha value using
int pixel = GImage.createRGBPixel(r, g, b, alpha);So for transparency:
int transparentPixel = GImage.createRGBPixel(42, 88, 19, 0);
![Page 61: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/61.jpg)
Blur
Set a pixel’s RGB values to the average of the pixel and all of its neighbors’ RGB values!
Things to consider:How do you get all the neighbors of a pixel?
Don’t write out nine lines of code! Think for loops… How do you account for the edges?
Sum over fewer values (make sure to adjust the denominator)
(r-1,c-1) (r-1,c) (r-1,c+1)
(r,c-1) (r,c) (r,c+1)
(r+1,c-1) (r+1,c) (r+1,c+1)
![Page 62: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/62.jpg)
Cropping!
![Page 63: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/63.jpg)
Cropping!
0 1 2 3 4 50 A B C D E F
1 G H I J K L
2 M N O P Q R
3 S T U V W X
What are the dimensions of orig? What are the dimensions of cropped? orig has 4 rows and 6 columns, cropped has 2 rows and 3 columns
origcropped
0 1 20 I J K
1 O P Q
crop(1,2,3,2)
crop(image,x,y,width,height)
![Page 64: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/64.jpg)
e q u a l i z e
We are trying to spread out the luminosity of an image (how bright it is). We can get the luminosity (brightness) of a pixel with
int luminosity = computeLuminosity(r, g, b);
We are going to be working with grayscale images meaning their r, g, and b values will all be set to the same number! (0, 0, 0) is black, (255, 255, 255) is white, and (x, x, x) for some other number x will be a shade of gray.
Three parts (that should be decomposed!): Compute the luminosity histogramCompute a cumulative luminosity histogramModify each pixel based on cumulative luminosity
![Page 65: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/65.jpg)
Compute the Luminosity Histogram
0 1 2 3 4 50 0 2 2 1 1 3
1 2 1 1 0 1 2
2 0 0 1 3 3 2
3 1 1 0 0 2 1
0 1 2 36 9 6 3
The histogram represents the number of times we’ve seen each pixel.What’s the best way to represent this in our code?
![Page 66: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/66.jpg)
Compute the Cumulative Luminosity Histogram
0 1 2 36 15 21 24
Given the original histogram, how can we generate these cumulative values?
0 1 2 36 9 6 3
![Page 67: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/67.jpg)
Generate the New Image
0 1 2 3 4 50 63 223 223 159 159 255
1 223 159 159 63 159 223
2 63 63 159 255 255 223
3 159 159 63 63 223 159
0 1 2 36 15 21 24
If the pixel at (r, c) has luminosity L:New RGB at (r,c) = 255 * (# of pixels with luminosity <= L) / # of pixels in image
Why did we make you compute the cumulative histogram?
![Page 68: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/68.jpg)
Tips and Tricks
![Page 69: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/69.jpg)
Questions?
![Page 70: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/70.jpg)
Sample TitleSample body
![Page 71: DarkRoom YEAH Hours - web.stanford.edu · Outline For Today Array Review 2D Arrays (Grids) GImages and Pixels DarkRoom Overview Tips and Tricks](https://reader035.fdocuments.in/reader035/viewer/2022070617/5e0bf5face38d43f5b0a466a/html5/thumbnails/71.jpg)
Style Standards Type something #FF5272 Type something #7FFFA6
Type something #BD93F9 Type something #FFB86C
Type something #66D9EF Type something #DADAD4
Type something #FFFFFF Type something #2f2f2f
Text font - Overpass Normal I’m a header
Code font - Inconsolata I’m a subheader