Multidimensional Arrays Eric Roberts CS 106A February 17, 2016.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Arrays.
TOPIC 5 INTRODUCTION TO PICTURES 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
The Binary Numbering Systems
Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Arrays and ArrayLists C H A P T E R slides partially adapted from.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Level ISA3: Information Representation
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
R-1 University of Washington Computer Programming I Lecture 17: Multidimensional Arrays © 2000 UW CSE.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
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.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
1 Chapter 20 — Creating Web Projects Microsoft Visual Basic.NET, Introduction to Programming.
Sets Eric Roberts CS 106B February 22, Outline Sets in mathematics1. High-level set operations3. Implementing the Set class4. Sets and efficiency5.
Chapter 11—Arrays and ArrayLists The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Arrays and ArrayLists C H A P T E R 1.
Java Unit 9: Arrays Declaring and Processing Arrays.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
CS 102 Computers In Context (Multimedia)‏ 01 / 28 / 2009 Instructor: Michael Eckmann.
Image Processing & Perception Sec 9-11 Web Design.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
ECE291 Computer Engineering II Lecture 9 Josh Potts University of Illinois at Urbana- Champaign.
Introducing Arrays We will often need to store collections of information –a list of names to sort –a list of values to compute averages, standard deviation,
Input, Output, and Processing
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Chapter 7—Objects and Memory The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Memory C H A P T E R 7 Yea, from.
ManipulatingPictures-part11 Manipulating Pictures, Arrays, and Loops part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
CSC1401 Manipulating Pictures. What we have done to date We have modified pictures by writing on top of them Using Turtles and using Graphics Drawing.
Lecture 7: Intro to Computer Graphics. Remember…… DIGITAL - Digital means discrete. DIGITAL - Digital means discrete. Digital representation is comprised.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Introducing Arrays We will often need to store collections of information –a list of names to sort –a list of values to compute averages, standard deviation,
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Arrays.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Five-Minute Review 1.What elements may a class definition contain? 2.Which perspectives on class design do we distinguish? 3.What is a mutable/immutable.
Windows Programming Lecture 03. Pointers and Arrays.
AP CSP: Pixelation – B&W/Color Images
Multidimensional Arrays
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Five-Minute Review What elements may a class definition contain?
Binary Notation and Intro to Computer Graphics
© 2016 Pearson Education, Ltd. All rights reserved.
Image Processing & Perception
JavaScript: Functions.
Chapter 11—Arrays and ArrayLists
Images Presentation Name Course Name Unit # – Lesson #.# – Lesson Name
Please visit
One-Dimensional Array Introduction Lesson xx
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Looping through pixels.
CS 106A, Lecture 18 Practice with 1D and 2D Arrays
Multidimensional Arrays and Image Manipulation
MSIS 655 Advanced Business Applications Programming
Images Presentation Name Course Name Unit # – Lesson #.# – Lesson Name
Multidimensional Arrays
Arrays.
Presentation transcript:

Multidimensional Arrays Eric Roberts CS 106A February 17, 2016

Once upon a time...

Pictures and Pixels The idea of representing an image as a two-dimensional array of dots is much older than modern computing and has several antecedents: –Halftone pictures in newspapers –Pointillist art –Mechanical weaving Of these examples, the most amazing connections to modern computing are in weaving, which contributed substantially to early computing. First printed halftone picture The Daily Graphic, December 7, 1873 Vincent Van Gogh, Self Portrait, 1887 The focus of today’s lecture is on how to represent images using two-dimensional arrays of tiny dots called pixels. Jacquard Loom, invented in 1801

Weavings on the Jacquard Loom

Midcourse Correction Assignment #4 will be due on Friday. If you turn in Assignment #4 by 5:00pm today, your score will be raised by one bucket. Assignments #5 and #6 will each be due one class day later than on the syllabus (or on today’s handout). Assignment #5 is therefore due on Monday, February 29; Assignment #6 will be due on Friday, March 11.

Image Manipulation

Midterm Histogram

Multidimensional Arrays Because the elements of an array can be of any Java type, those elements can themselves be arrays. Arrays of arrays are called multidimensional arrays. In Java, you can create a multidimensional array by using multiple brackets in both the type and the initialization parts of the declaration. For example, you can create array space for a 3 x 3 tic-tac-toe board using the following declaration: char[][] board = new char[3][3]; This declaration creates a two-dimensional array of characters that is organized like this: board[0][0]board[0][1]board[0][2] board[1][0]board[1][1]board[1][2] board[2][0]board[2][1]board[2][2]

Arrays of Arrays Internally, Java represents multidimensional arrays as arrays of arrays. It is often important to keep this fact in mind when you are creating array structures. For example, Java often requires you to initialize the rows of a two-dimensional array individually, as in the following revised code for creating the tic-tac-toe board: char[][] board = new char[3][]; for (int i = 0; i < 3; i++) { board[i] = new char[3]; }

Static Initialization Java makes it easy to initialize the elements of an array as part of a declaration. The syntax is type [] name = { elements }; where elements is a list of the elements of the array separated by commas. The length of the array is automatically set to be the number of values in the list. For example, the following declaration initializes the variable powersOfTen to the values 10 0, 10 1, 10 2, 10 3, and 10 4 : int[] powersOfTen = { 1, 10, 100, 1000, }; This declaration creates an integer array of length 5 and initializes the elements as specified.

Initializing Multidimensional Arrays You can initialize a multidimensional array when you declare it by using nested braces to reflect the levels of array nesting. For example, you can declare and initialize a multiplication table for the digits 0 to 9 like this: private static final int[][] MULTIPLICATION_TABLE = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 }, { 0, 3, 6, 9, 12, 15, 18, 21, 24, 27 }, { 0, 4, 8, 12, 16, 20, 24, 28, 32, 36 }, { 0, 5, 10, 15, 20, 25, 30, 35, 40, 45 }, { 0, 6, 12, 18, 24, 30, 36, 42, 48, 56 }, { 0, 7, 14, 21, 28, 35, 42, 49, 56, 63 }, { 0, 8, 16, 24, 32, 40, 48, 56, 64, 72 }, { 0, 9, 18, 27, 36, 45, 54, 63, 72, 81 } };

rnbqkbnr pppppppp PPPPPPPP RNBQKBNR rnbqkbnr pppppppp PPPPPPPP RNBQKBNR Exercise: Representing a Chess Board

Exercise: Crossword Numbering Write a program to number the squares in a crossword grid if they appear at the beginning of a word running either across or down. What type might you use to represent the information in this grid? How would you represent black squares? What rules can you supply to determine if a square is numbered?

Multidimensional Arrays and Images One of the best examples of multidimensional arrays is a Java image, which is logically a two- dimensional array of pixels. Consider, for example, the logo for the Java Task Force at the top right. That logo is actually an array of pixels as shown in the expanded diagram at the bottom. The GImage class allows you to convert the data for the image into a two-dimensional array of pixel values. Once you have this array, you can work with the data to change the image.

Pixel Arrays If you have a GImage object, you can obtain the underlying pixel array by calling the getPixelArray, which returns a two-dimensional array of type int. For example, if you wanted to get the pixels from the image file JTFLogo.png, you could do so with the following code: GImage logo = new GImage("JTFLogo.png"); int[][] pixels = logo.getPixelArray(); The first subscript in a pixel array selects a row in the image, beginning at the top. The height of the image is therefore given by the expression pixels.length. The second subscript in a pixel array selects an individual pixel within a row, beginning at the left. You can use the expression pixels[0].length to determine the width of the image.

Pixel Values Each individual element in a pixel array is an int in which the 32 bits are interpreted as follows: The first byte of the pixel value specifies the transparency of the color, which is described in more detail on a later slide The next three bytes indicate the amount of red, green, and blue in the pixel, in which each value varies from 0 to 255. Together, these three bytes form the RGB value of the color, which is typically expressed using six hexadecimal digits, as in the following examples: 0xFF0000 Color.RED 0x0000FF Color.BLUE 0xFF00FF Color.MAGENTA 0xFFC800 Color.ORANGE 0x Color.GRAY transparency (  )redgreenblue

Combining Colors of Light

Transparency The first byte of the pixel value specifies the transparency of the color, which indicates how much of the background shows through. This value is often denoted using the Greek letter alpha (  ). Transparency values vary from 0 to 255. The value 0 is used to indicate a completely transparent color in which only the background appears. The value 255 indicates an opaque color that completely obscures the background. The standard color constants all have alpha values of 255.

Image Manipulation You can use the facilities of the GImage class to manipulate images by executing the following steps: The program on the next slide shows how you can apply this technique to flip an image vertically. The general strategy for inverting the image is simply to reverse the elements of the pixel array. Read an existing image from a file into a GImage object.1. Call getPixelArray to get the pixels.2. Write the code to manipulate the pixel values in the array.3. Call the GImage constructor to create a new image.4.

The FlipVertical Program skip simulation FlipVertical public void run() { GImage original = new GImage("Candle.png"); GImage flipped = flipVertical(original); double y = (getHeight() - original.getHeight()) / 2; double x1 = (getWidth() - IMAGE_SEP) / 2; double x0 = x1 - original.getWidth() - IMAGE_SEP; add(original, x0, y); add(flipped, x1, y); } original flipped yx1x private GImage flipVertical(GImage image) { int[][] array = image.getPixelArray(); int height = array.length; for (int p1 = 0; p1 < height / 2; p1++) { int p2 = height - p1 - 1; int[] temp = array[p1]; array[p1] = array[p2]; array[p2] = temp; } return new GImage(array); } image array height 100 public void run() { GImage original = new GImage("Candle.png"); GImage flipped = flipVertical(original); double y = (getHeight() - original.getHeight()) / 2; double x1 = (getWidth() - IMAGE_SEP) / 2; double x0 = x1 - original.getWidth() - IMAGE_SEP; add(original, x0, y); add(flipped, x1, y); } private GImage flipVertical(GImage image) { int[][] array = image.getPixelArray(); int height = array.length; for (int p1 = 0; p1 < height / 2; p1++) { int p2 = height - p1 - 1; int[] temp = array[p1]; array[p1] = array[p2]; array[p2] = temp; } return new GImage(array); } image array height 100

Selecting Color Components If you want to work with the colors of individual pixels inside a pixel array, you can adopt either of two strategies: –You can use the bitwise operators described in the text to select or change individual bits in the pixel value. –You can use the static methods provided by the GImage class to achieve the same purpose. Although it is useful to remember that all information is stored as bits, there doesn’t seem to be much point in going into all the details, at least in CS 106A. We will therefore use the second strategy and employ the static methods getRed, getGreen, getBlue, getAlpha, and createRGBPixel.

Creating a Grayscale Image As an illustration of how to use the bitwise operators to manipulate colors in an image, the text implements a method called createGrayscaleImage that converts a color image into a black-and-white image, as shown in the sample run at the bottom of this slide. CreateGrayscale The code to implement this method appears on the next slide.

The createGrayscaleImage Method /* Creates a grayscale version of the original image */ private GImage createGrayscaleImage(GImage image) { int[][] array = image.getPixelArray(); int height = array.length; int width = array[0].length; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int pixel = array[i][j]; int r = GImage.getRed(pixel); int g = GImage.getGreen(pixel); int b = GImage.getBlue(pixel); int xx = computeLuminosity(r, g, b); array[i][j] = GImage.createRGBPixel(xx, xx, xx); } return new GImage(array); } /* Calculates the luminosity of a pixel using the NTSC formula */ private int computeLuminosity(int r, int g, int b) { return GMath.round(0.299 * r * g * b); }

The ImageShop Application

The End