CS116      Lab 4 - Conditionally Modifying Pixels

Objectives:

  1. Before starting this lab you should have read chapter 6 in your text.
     
  2. Open Eclipse and import the cs116 project. 
     
  3. Suppose we wish to change colors that are pretty close to a given color to another color in a picture.  This is useful in reducing the "red-eye" effect in photos.  The following method of the Picture class performs this by conditionally changing colors that are near red within a specified area.

    /* Method to remove red-eye from a picture object in the rectangle defined
    * by startX, startY, endX, endY.
    * The red will be replaced with the passed newColor
    * @params startX, startY is the upper left hand corner of the sub-image
    * @params endX, endY is the lower right hand corner of the sub-image
    * @param newColor is the new color to replace the red
    */
    public void removeRedEye(int startX, int startY, int endX, int endY, Color newColor){
        Pixel pixel = null;
        /* loop through the pixels in the rectangle defined by startX, startY, endX, endY*/
        for (int x= startX; x <endX; x++){
            for (int y = startY; y<endY; y++){
                // get the current pixel
                pixel = this.getPixel(x,y);
                // if the color is near red then change it
                if (pixel.colorDistance(Color.red) < 167)
                pixel.setColor(newColor);
            }
        }
    }


    Add this method to the picture class and try it out on the picture jenny-red.jpg in the mediasources.  Change the color to black within the rectangle defined by (109,91) and (202,107).
     

  4.  

    Assignment:
    Write a method
    colorReplace(int startX, int startY, int endX, int endY, Color startColor, Color newColor, int threshold)
    which replaces all values near to the startColor within the specified rectangle to the newColor.  The threshold defines how "near" we have to be to the startColor to change it.

    Try this out by writing a program that starts with a picture of a person and changes their skin to green and hair to orange. (or whatever colors you would prefer).


     

  5. We want a method which detects edges in the picture image by looking for areas of high contrast.  We check for high contrast if the average of the colors values of a pixel differ by some amount from the average of the color values of a neighboring pixel.

    Consider this simple edge detection code:

    /* Method to do simple edge detection by comparing the absolute value
    * of the difference between the color intensities (average color values)
    * between a pixel and the pixel below it.
    * Edges will be drawn in black, everything else in white
    * @param amount is the threshold for which a pixel marked as an edge
    */
    public void edgeDetection(double amount){
        Pixel topPixel = null;
        Pixel bottomPixel = null;
        double topAverage = 0.0;
        double bottomAverage = 0.0;
        /* loop through the y values from 0 to height - 1
        * (since compare to below pixel)
        */
        for (int y= 0; y < this.getHeight()-1; y++){
            // loop through the x values from 0 to width
            for (int x = 0; x<this.getWidth(); x++){
                // get the top and bottom pixels
                topPixel = this.getPixel(x,y);
                bottomPixel = this.getPixel(x,y+1);
                // get the color averages for the two pixels
                topAverage = topPixel.getAverage();
                bottomAverage = bottomPixel.getAverage();
                /* check if the absolute value of the difference
                * is less than the amount
                */
                if (Math.abs(topAverage - bottomAverage) < amount)
                    topPixel.setColor(Color.white);
                else
                    topPixel.setColor(Color.black);

            }
        }
    }

    Add this method to the Picture class and test if out.
     


  6.  

    Assignment:
    Since the simple edge detection is just checking a pixel with the one below, it is detecting horizontal edges but not detecting vertical edges.  Change the edgeDetection method so it writes a black pixel if there is high contrast with either the pixel below or with the pixel to the right.


     

  7. Email your modified code Picture.java and your resulting pictures to me at jill.zimmerman@goucher.edu