Inf1 OP : Lab Sheet Week 6 Q6 - Image Editor
Overview

In this exercise, we will explore image manipulation. We will load an image, and then write methods for converting the image to greyscale and for thresholding the image. Although these methods will be static, in order to implement them you will need to call instance methods of objects of type Picture (API documentation) and Color (API documentation). You will need the StdLib library introduced in week 5.

You can download the following files to use as sample images and add them to your project:

Warning

Depending on how you have setup your IntelliJ project and where source files are located, you might need to reference resource files in different ways. The same is true if you are running your code from the command line.

You need to access the picture resource files at runtime based on your current working directory. Have a look at this previous exercise to see how you can find out what your current working directory is and how you can create a relative path to corresponding resource files.

We assume that the resource files are directly located in your working directory and therefore access them directly for the remainder of this text. Make sure you adapt this accordingly!

Greyscaling

Start off by implementing the methods required for converting a colour picture to greyscale. Create a class ImageEditor1 that contains the following three static methods:

public static double luminance(Color color)

Return a double that represents the luminance of color. This is given by the following function of color‘s red (R), green (G) and blue (B) values: \( luminance = 0.299 \times R + 0.587 \times G + 0.114 \times B \).

To obtain the RGB values of color, you will need to call its instance methods getRed(), getGreen() and getBlue().

public static Color toGrey(Color color)
Return a new Color object whose RGB values are all equal to the luminance of the input color. To get appropriate values, round the luminance value and cast it to an int.
public static Picture makeGreyscale(Picture pic)
Return a new Picture object which results from converting every pixel in pic to its greyscale equivalent. Remember that a Picture is just a 2D array of Color objects, and the pixel with coordinates (i, j) is accessed via the call pic.get(i, j).

Once you have written these three methods, test them by defining a main() method which contains the following lines:

Picture p = new Picture("lion2.jpg"); // or use any other colour image
Picture greyscale = makeGreyscale(p);
greyscale.show();

An automated test has been created for this exercise: ImageEditor1Test.java.

Thresholding

The next task is to write a method for thresholding an image. In thresholding, we look at the luminance of each pixel in the image. If the luminance is below a certain value thresh, we set the pixel in the new image to black, otherwise we use the greyscale equivalent of the original pixel.

Create a second class ImageEditor2, that contains this method:

public static Picture threshold(Picture p, int thresh)

To implement this, you again need to inspect every pixel in the picture, using a similar approach to before.

In order to test your method, add the following lines to your main method:

Picture t = threshold(p, 120);
t.show();

Experiment with different values of threshold.

It can also be fun to play around with variants, for example where you set pixels that are below the threshold to something like Color.RED, and keep the pixels that are above the threshold at their initial colour values.

An automated test has been created for this exercise: ImageEditor2Test.java.