Lab 2: Mean Image Median Displayed
Overview
In this assignment, you will incorporate lab 1 feedback from your instructor (if available), and load and display images in a graphical user interface.
Video introduction for the lab
Pre-Lab (DUE BEFORE LAB - see due date in Canvas)
As part of the pre-lab activity you must ensure that you have JavaFX installed on your laptop.
Assignment
In addition to incorporating feedback from your instructor, you must replace
the code in the mocked
package with appropriate JavaFX code (change the imports to
use the javafx.scene.image
classes). You must:
- Declare
readPPMImage()
andwritePPMImage()
asprivate
methods - Ensure that alpha value is set to 255 in
readPPMImage()
- Implement
readImage()
andwriteImage()
methods with the same parameters asreadPPMImage()
andwritePPMImage()
and return type that will callreadPPMImage()
andwritePPMImage()
when the file extention on thePath
ends with.ppm
. - Ensure
calculateMedianImage()
andcalculateMeanImage()
calculate alpha value for each pixel. - Create a
Lab2
class that extends the JavaFXApplication
class and overrides thestart()
method. (Delete theLab1
class or comment out calls toreadPPMImage()
andwritePPMImage()
since they are now private methods) - Update
readImage()
andwriteImage()
methods to use JavaFX classes to add support for reading and writing JPG and PNG file formats. - If a path with an unsupported file extension is passed to either
readImage()
orwriteImage()
, the method should throw anIllegalArgumentException
. - Ensure that the
Lab2
class catches any exceptions thrown and displays a useful error message before exiting gracefully. - The program should display the calculated image in a window.
javax.imageio.ImageIO.write()
method, and you can use the
javafx.embed.swing.SwingFXUtils.fromFXImage()
method to
get a BufferedImage
(that is a RenderedImage
).
If writing PNG files works but it doesn't work for JPG files, don't worry
about it.
Your program must run as a .jar
file from the command line and accept the
following arguments:
- Operation preference — either
median
ormean
(all lowercase) - Output filename — e.g.,
images/output.png
- At least two input image filenames — e.g.,
images/input1.jpg
andimages/input2.jpg
Potentially useful tutorials:
Once the .jar
file is created, move it from the out/artifacts/...
folder into the project folder (same folder as the README.md file) and rename
it lab2.jar
.
The program will process the input images to produce the desired output and then save and display the resulting image. For example,
java -jar lab2.jar median images/codeMedian.ppm images/code1.png images/code2.png images/code3.png
will produce codeMedian.ppm
in the images
folder that is the median of the first three
codeX.png
images.
Image
in the Scene
by placing the Image
in a ImageView
that is placed in an HBox
that then gets added
to the Scene
.
Exception Handling
If any problems are encountered with reading the input files or writing the output file, the program should display a useful error message to the console and terminate gracefully. The program should not crash or display any exceptions.
Just For Fun
Ambitious students may wish to:
- Display all of the input images as well as the output image.
Acknowledgement
This laboratory assignment, developed by Dr. Chris Taylor.