View the step-by-step solution to:

Programming Assignment 3 Points: 500 Due: April 22, 11:59PM Late Submission April 23, 11: Your programs must be in Java, preferably Java 8.

Write a class named ImageProcessor.

3 Methods:

ImageProcessor(String imageFile). Parameter imageFile holds the name of the image that will be manipulated. We will use W to denote the width and H to denote the height of the image.

reduceWidth(double x) Returns a new Picture whose width is âx à W â. Note that the type

of this method must be Picture. Your method must use the algorithm described earlier to reduce the image width. And your method must use the static method minCostVC to compute the vertical cut.

minCostVC(int[][] M): Returns a min-cost vertical cut. Type of this method must be array list of integers. Note that if M has n rows, the the returned array list has exactly 2n integers. 

Programming Assignment 3 Points: 500 Due: April 22, 11:59PM Late Submission April 23, 11:59PM (25% penalty) Description of a programming assignment is not a linear narrative and may require multiple readings before things start to click. You are encouraged to consult instructor/Teaching Assis- tants for any questions/clariFcations regarding the assignment. Your programs must be in Java, preferably Java 8.1. All your classes must be in the default package (even though it is not a good programming practice). ±or this PA, you may work in teams of 2 . It is your responsibility to Fnd a team member. If you can not Fnd a team member, then you must work on your own. 1 Dynamic Programming You will use dynamic programming to compute min cost vertical cut of a matrix and to perform string alignment . You will design a class named DynamicProgramming . 1.1 Cuts Let M be a n m integer matrix (with n rows and m columns). Assume that rows are numbered 0 , 1 , 2 , ··· ,n - 1 and columns are numbered 0 , 1 , 2 , ··· m - 1. Let M [ i, j ]referstothece l lin i th row and j th column. A vertical cut V of M is a sequence of 2 n integers [ x 0 ,y 0 ,x 1 ,y 1 , ··· ,x n - 1 ,y n - 1 ] such that the following holds 1. x 0 = 0, y 0 2 { 0 , ··· ,m - 1 } 2. ±or 1 i<n , x i = x i - 1 +1 3. ±or 1 i<ny i 2 { y i - 1 ,y i - 1 - 1 ,y i - 1 +1 } The cost of a vertical cut V =[ x 0 ,y 0 ,x 1 ,y 1 , ··· ,x n ,y n ] is deFned as Cost ( V )= M [ x 0 ,y 0 ]+ M [ x 1 ,y 1 ]+ ··· + M [ x n - 1 ,y n - 1 ] Given a matrix, the min-cost vertical cut , denoted MinVC ( M ), is a vertical cut whose cost is the smallest. 1
Background image of page 1
1.1.1 Your Task—Compute MinVC Include the with the following public static method in the class DynamicProgramming. minCostVC(int[][] M) : Returns a min-cost vertical cut. Type of this method must be array list of integers . Note that if M has n rows, the the returned array list has exactly 2 n integers. You must use dynamic programming paradigm to arrive at your code. For this, ±rst de±ne the recurrence relation. Then arrive at an iterative solution. Your code must be iterative, not recursive and should not use use memoization . Otherwise you will receive zero credit. 1.2 Alignment Given two characters a and b , we de±ne a function penalty ( a, b ) as follows: if a equals b , penalty ( a, b )= 0. If a or b equals $, then penalty ( a, b ) = 4; otherwise penalty ( a, b ) = 2. Given two strings x = x 1 x 2 ··· x n and y = y 1 y 2 ··· y n alignment cost between x and y is AlignCost ( x, y )= n X i =1 penalty ( x i ,y i ) . Let x = x 1 x 2 , ··· x n and y = y 1 y 2 ··· y m two strings where n m . assume that neither string has the character $. Our task is to align the two strings. The alignment is done by creating a new string z formed by inserting the character $ at n - m indices in y . Note that length of z is n . Note that there are many choices for z . The goal is to ±nd a z for which AlignCost ( x, z )ism in im ized . We would like to write a method that gets two strings x (of length n ) and y (of length m ) as parameters (assume that n m ) and computes a z such that AlignCost ( x, z ) AlignCost ( x, z 0 ) for every z 0 (obtained by inserting n - m many $’s in y ). 1.2.1 Your Task—Compute Min Cost Alignment Include the following static method in the class DynamicProgramming stringAlignment(String x, String y) . Assume that x is a string of length n and y is a string of length m such that n m .T h i sm e t h o dr e t u r n sas t r i n g z (obtained by inserting $ at n - m indices in y ) such that AlignCost ( x, z ) AlignCost ( x, z 0 ) over all possible z 0 (obtained by inserting n - m many $’s in y ). You may assume that length of x is at least the length of y and neither of x or y has the character $. Note that the length of the returned string z must equal the length of x . You must use dynamic programming paradigm to arrive at your code. For this, ±rst de±ne the recurrence relation. Then arrive at an iterative solution. Your code must be iterative, not recursive and should not use use memoization . Otherwise you will receive zero credit. 2 An Application of MinCost Vertical Cut The notion of min-cost vertical cut has applications in image processing—in resizing images. Recall that an image is a 2-dimensional array of pixels. Each pixel has represented as 3-tuple in the RGB format. Given an image of width W and height H ,i ti sr ep r e s en t edbya H W matrix (2-D array). For example an image of width 4 and height 3 is represented as the following 2-D array of pixels M : 2
Background image of page 2
Show entire document
/****************************************************************************** * Compilation: javac Picture.java * Execution: java Picture imagename * Dependencies: none * * Data type for manipulating individual pixels of an image. The original * image can be read from a file in jpg, gif, or png format, or the * user can create a blank image of a given size. Includes methods for * displaying the image in a window on the screen or saving to a file. * * % java Picture mandrill.jpg * * Remarks * ------- * - pixel (x, y) is column x and row y, where (0, 0) is upper left * * - see also GrayPicture.java for a grayscale version * ******************************************************************************/ import java.awt.Color; import java.awt.FileDialog; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.net.URL; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.KeyStroke; /** * This class provides methods for manipulating individual pixels of * an image. The original image can be read from a {@code .jpg}, {@code .gif}, * or {@code .png} file or the user can create a blank image of a given size. * This class includes methods for displaying the image in a window on * the screen or saving it to a file. * <p> * Pixel (<em>col</em>, <em>row</em>) is column <em>col</em> and row <em>row</em>. * By default, the origin (0, 0) is the pixel in the top-left corner, * which is a common convention in image processing. * The method {@code setOriginLowerLeft()} change the origin to the lower left. * <p> * For additional documentation, see * <a href="http://introcs.cs.princeton.edu/31datatype">Section 3.1</a> of * <i>Computer Science: An Interdisciplinary Approach</i> * by Robert Sedgewick and Kevin Wayne. * * @author Robert Sedgewick * @author Kevin Wayne */ public final class Picture implements ActionListener { private BufferedImage image; // the rasterized image
Background image of page 1
private JFrame frame; // on-screen view private String filename; // name of file private boolean isOriginUpperLeft = true; // location of origin private final int width, height; // width and height /** * Initializes a blank {@code width}-by-{@code height} picture, with {@code width} columns * and {@code height} rows, where each pixel is black. * * @param width the width of the picture * @param height the height of the picture * @throws IllegalArgumentException if {@code width} is negative * @throws IllegalArgumentException if {@code height} is negative */ public Picture(int width, int height) { if (width < 0) throw new IllegalArgumentException("width must be nonnegative"); if (height < 0) throw new IllegalArgumentException("height must be nonnegative"); this.width = width; this.height = height; image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // set to TYPE_INT_ARGB to support transparency filename = width + "-by-" + height; } /** * Initializes a new picture that is a deep copy of the argument picture. * * @param picture the picture to copy * @throws IllegalArgumentException if {@code picture} is {@code null} */ public Picture(Picture picture) { if (picture == null) throw new IllegalArgumentException("constructor argument is null"); width = picture.width(); height = picture.height(); image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); filename = picture.filename; for (int col = 0; col < width(); col++) for (int row = 0; row < height(); row++) image.setRGB(col, row, picture.get(col, row).getRGB()); } /** * Initializes a picture by reading from a file or URL. * * @param filename the name of the file (.png, .gif, or .jpg) or URL. * @throws IllegalArgumentException if cannot read image * @throws IllegalArgumentException if {@code filename} is {@code null} */ public Picture(String filename) { if (filename == null) throw new IllegalArgumentException("constructor argument is null"); this.filename = filename; try { // try to read from file in working directory File file = new File(filename); if (file.isFile()) { image = ImageIO.read(file); }
Background image of page 2
Show entire document
Sign up to view the entire interaction

Top Answer

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask a homework question - tutors are online