LAB 19 – Conditionally Modifying Pixels (part 3 – Highlighting Extremes and Blurring)

Topics

n      To replace a range of colors with one color in a picture: posterizing.

n      To average nearby pixels: blur

n      To combine Boolean expressions with and and or.

 

6.1 Conditional Pixel Changes

Type in the following code in the Program window (changing the comments):

 

import java.awt.Color;

/* Program that transforms pictures using loops and conditionals

 *

* Picture and Pixel classes are defined in bookClasses developed

 * at Georgia Tech by Mark Guzdial / Barbara Ericson

 *

 * @author Wayne Summers

 * @date   Sept. 23, 2007

 */

 

public class TransformPictures3

{

  public static void main(String[] args)

  {

    String fileName1, fileName2;

    System.out.println(“Find the source file”);

    fileName1 = FileChooser.pickAFile();  // note that this pops up a window to select a jpg file

    Picture sourcePicture;

    sourcePicture = new Picture(fileName1);

 

// calls to methods that manipulate images go here

 

   sourcePicture.show(); 

  }

}

 

6.4 Highlighting Extremes

i) Type in the following code in the Program window below the main method and before the last }:

 

/**

  * Method to replace the pixel colors of the picture that have a color distance less than

  * the passed amount of white or black with the passed replacement color

  * @param pict - picture Object to be modified

  * @param amount - distance from white or black

  * @param replacementColor - the new color to use

  */

 public static void highlightLightAndDark(Picture sourcePic, double amount, Color replacementColor)

  {

    Pixel pix = null;

    int width = sourcePic.getWidth();

    int height = sourcePic.getHeight();

 

     // loop through the pixels

    for (int x = 0; x < width; x++)

    {

      for (int y = 0; y < height; y++)

      {

        // get the current pixel and color values

        pix = sourcePic.getPixel(x,y);

 

        // if the distance from white or black is less than passed amount

        // use the replacement color

        if (pix.colorDistance(Color.white) < amount || pix.colorDistance(Color.black) < amount)

            pix.setColor(replacementColor);

      }

    }

  }

 

ii) Type in the following code in the Program window in the main method and before the pic.show();

 

highlightLightAndDark(sourcePicture, 50.0, Color.yellow);

 

iii) Test your program with different pictures, values for the distance and colors (e.g. butterfly1.jpg, 50, yellow).

 

6. 5 Combining Pixels: Blurring

iv) Type in the following code in the Program window below the main method and before the last }:

 

  /**

  * Method to blur the pixels of the picture

  * @param pict - picture Object to be modified

  * @param numPixels - number of pixels to average in all directions

  */

  public static void blur(Picture sourcePic, int numPixels)

  {

    Pixel pix = null;

    Pixel samplePix = null;

    int width = sourcePic.getWidth();

    int height = sourcePic.getHeight();

    int redValue = 0;

    int greenValue = 0;

    int blueValue = 0;

    int count = 0;

 

     // loop through the pixels

    for (int x = 0; x < width; x++)

    {

      for (int y = 0; y < height; y++)

      {

        // get the current pixel and color values

        pix = sourcePic.getPixel(x,y);

 

        // reset the count and red, green, and blue values

        count = 0;

        redValue = greenValue = blueValue = 0;

       

        // loop through pixel numPixels before x and after x and sum values of colors

        for (int xSample = x - numPixels; xSample <= x + numPixels; xSample++)

        {

            for (int ySample = y - numPixels; ySample <= y + numPixels; ySample++)

            {

                // check that we are in the range of acceptable pixels

                if (xSample >= 0 && xSample < width && ySample >= 0 && ySample < height)

                {

                    samplePix = sourcePic.getPixel(xSample,ySample);

                    redValue = redValue + samplePix.getRed();

                    greenValue = greenValue + samplePix.getGreen();

                    blueValue = blueValue + samplePix.getBlue();

                    count++;

                } // end if

            } // end inner loop

        } // end outer loop

        

         // use average color of surrounding pixels

         Color newColor = new Color (redValue / count, greenValue / count, blueValue / count);

         pix.setColor(newColor);

      } // end inner loop

    } // end outer loop

  }

 

v) Type in the following code in the Program window in the main method and before the pic.show();

 

    blur(sourcePicture, 2);

 

vi) Test your program with different pictures (e.g. blueMotorcycle.jpg) and other values for numPixels.

 

EXTRA (if there is time)

6. 3 Sepia-tinted and Posterized Pictures: Using Multiple Conditionals to Choose a Color

 

 

QUESTIONS:

Modify the blur method to allow for changing just part of the picture by passing in the startX, startY, endX, and endY values (see the replaceColor method from lab17)

 

Submit the .java file (with edgeDetection where you set the arguments in the main method to values that will allow you to select a region of the picture to change) through the DropBox in WebCT.