Question: Java: Overview: You are provided with the Photo interface. It defines methods for loading a photo from a file, and retreiving size and pixel information.
Java:
Overview:
You are provided with the Photo interface. It defines methods for loading a photo from a file, and retreiving size and pixel information.
You should create an enum called Pixel which defines the values WHITE, GRAY, and BLACK.
You are also provided an interface called PhotoFunc, which has methods for working with images: loading sets of images, finding the quality of a match between two images, and finding the best match for a set of images.
You will create all necessary classes to implement the functionality of these two interfaces. In particular, you must create an instantiable class called PhotoProcessor which implements PhotoFunc.
You are provided with the rules for scoring the quality of a match between photos in order to determine where they have most likely been photographed.
You are provided sample image data and a tester to test your code. Also, you are given a utility for converting images to text files for use in this project (it may not be necessary).
Rules
You may import any standard Java library if you see the need.
All fields must be declared private or protected. This will be verified manually.
You must have classes which implement the provided interfaces. In particular, you must have a PhotoProcessor class which implements PhotoFunc.
You are responsible for designing your own classes and methods. Any functionality beyond what is required by the interfaces is at your own descretion.
Any class, interface, and enumeration, as well as any public method, must be documented using JavaDoc style of comments.
Pixel: public enum Pixel
This enumeration must contain the three values, BLACK, GRAY, and WHITE.
Photo: public interface Photo
Photos are made up of a rectangle of 3-color (black/gray/white) pixels. The upper left corner of the photo is (0, 0). The x-value increases when moving to the right, and the y-value increases going downwards. When the load function is called with a filename, a photo is read from a text file. Once an image is loaded, the interface's methods can be used to access the image's pixel data.
Note that in order to be able to test your Photo, your PhotoProcessor must be partially functional as well, because it's the only way we can load photos using your code.
- public void load(String filename) throws Exception load image data from the named text file. Throws an exception if there was a problem opening the file (does not exist, etc), or if it's badly formatted (lines which are different length). Each line in the file represents one row of pixels, and each character represents a single pixel. Specifically, a ' ' (blank space) represents a white pixel, a '.' (period) represents a gray pixel, and any other character (for example a 'x') represents a black pixel.
- public String getName() returns the name of the photo, i.e. the filename which was used the last time the photo was loaded. You may return null if no photo has been loaded.
- public int getHeight() returns the height of the image in pixels. You may return zero if no photo has been loaded.
- public int getWidth() returns the width of the image in pixels. You may return zero if no photo has been loaded.
- public Pixel getPixel(int x, int y) returns the pixel value located at the requested image coordinates.
PhotoFunc: public interface PhotoFunc
This interface provides all of the image loading and comparing functionality which we need for this project. You will also need to write a class called PhotoProcessor which implements this functionality, so that we have something which we can instantiate and use. The PhotoProcessor must have a default constructor (i.e. it must be possible to instantiate it with no arguments).
The first role of this interface is for loading images. It should be possible to load a set of background images, as well as a set of foreground images. The background images represent a set of locations, and the goal is to figure out which foreground images correspond to which locations. A list of background images will be specified by a file. So for example, we may have a file bgplaces.txt containing:
johnson_center.txt washington_monument.txt fair_oaks_mall.txt
The three lines of this file are the names of three photo files corresponding to different locations. If we call the load function with the argument "bgplaces.txt", the load function will look inside this file, take the three filenames there, and load each of the three as photos. A similar process would be used for foreground images. Once the photo files are loaded, we also have methods to retrieve the photos by name or as a collection.
- public void loadBGManifest(String filename) throws Exception load all of the background photos which are listed in the specified file.
- public void loadFGManifest(String filename) throws Exception load all of the foreground photos which are listed in the specified file.
- public Photo getPhoto(String photoname) returns a Photo corresponding to the given filename, which can be either a foreground or background photo.
- public Collection extends Photo> getBGPhotos() get a list of all background photos which have been loaded.
- public Collection extends Photo> getFGPhotos() get a list of all foreground photos which have been loaded.
The second role of the interface is to find matches between photos. Given two photos, the quality of a match is defined as follows:
For a pair of pixels, if they both have the same value (WHITE and WHITE, GRAY and GRAY, or BLACK and BLACK), then the resulting score is 1.0. If exactly one of the pixels is GRAY (thus, the other one is either WHITE or BLACK), then the resulting score is 0.5. Otherwise the resulting score is 0.0.
The quality of the match is the average of the score of every pair of corresponding pixels between the two images (i.e. the (0, 0) pixel in the first image vs the (0, 0) pixel in the second image, the (1, 0) pixel vs the (1, 0) pixel, etc). Assume that both images are the same size and and are aligned with one another.
- public double getMatch(Photo foreground, Photo background) finds the quality of match score between two different photos.
- public double getMatch(Photo foreground, Photo background, int x, int y) (optional, extra credit) finds the quality of match score, assuming that the upper left corner of the foreground photo is aligned to position (x, y) in the background photo, and that the two photos may be different sizes. The quality of match score is only averaged over the overlapping pixels. If this variant is used, then the result of the first getMatch above should be the highest possible score over all possible (x, y) with at least 10 pixels of overlap.
Finally, you will need to be able to match your foreground photos up with the background photos to see which came from where. Assume that the number of foreground photos is no greater than the number of background photos, and that at most one foreground photo matches with each background photo. The goal is to find the mapping of foreground-to-background photos which has the best overall score (i.e. mapping produces the largest possible sum of quality scores).
Since only one foreground photo is allowed for every background photo, you will most likey need to backtrack to find the best solution. This sort of problem lends itself to a recursive solution (try to match a pair, then reduce it to the subproblem of matching the remaining photos). In practice, this would be a very slow process if a many photos are involved, but as long as there are just a few, it should be doable. Also, consider saving or precomputing your match scores to save some time.
- public double getBestMatch(HashMap
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
