Lecture 38 (Sherriff) - Images 2

Lecture Date: Friday, April 17

More image manipulation algorithms today!

Our main algorithms for the day will be:

  • Grayscale
  • Resizing
  • Green Screen

Grayscale

The basic idea here to get the overall intensity of the color by adding up the red, green, and blue values and then taking the average. Remember that for a pixel to be gray (or black or white), all three RGB values have to be the same.

Resizing

With resizing, we will either be taking a fraction of the pixels (to make the image smaller) or duplicating pixels (to make the image bigger). We begin by creating a new image at the new size. Then, as we adjust the looping through our original image based on the scaling factor - we either skip pixels to make a smaller image or grab the same pixel multiple times to enlarge it.

Green Screen

Take two pictures, one in which the background is completely one solid color that you wouldn’t normally find in a “typical” picture. (This is one reason TV weathermen don’t wear green…) Then, as you loop through both pictures, pick the pixels from the background image if the matching pixel from the foreground image is green. Otherwise, take the pixel from the foreground image. Here are two images we can work with:

Today's Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public static Picture greenscreen(Picture fore, Picture back) {
  Picture greenscreen = new Picture(fore.getWidth(), fore.getHeight());
  for (int row = 0; row < fore.getWidth(); row++) {
      for (int col = 0; col < fore.getHeight(); col++) {
          Pixel fore_p = fore.getPixel(row, col);
          Pixel back_p = back.getPixel(row, col);
          Pixel green_p = greenscreen.getPixel(row, col);
          
          if(fore_p.getGreen() > fore_p.getBlue() + fore_p.getRed() + 10) {
              green_p.setColor(back_p.getColor());
          } else {
              
              green_p.setColor(fore_p.getColor());
          }
      }
  }
  
  return greenscreen;
}

public static Picture grayscale(Picture pict) {
  Picture gray = new Picture(pict.getWidth(), pict.getHeight());

  for (int row = 0; row < pict.getWidth(); row++) {
      for (int col = 0; col < pict.getHeight(); col++) {
          Pixel p = pict.getPixel(row, col);
          Pixel dest = gray.getPixel(row, col);
          int intensity = p.getRed() + p.getGreen() + p.getBlue();
          dest.setRed(intensity / 3);
          dest.setGreen(intensity / 3);
          dest.setBlue(intensity / 3);
      }
  }

  return gray;

}

public static Picture scale(Picture pic, double scale) {
  int oldWidth = pic.getWidth();
  int oldHeight = pic.getHeight();
  
  Picture newImg = new Picture((int)(oldWidth * scale), (int)(oldHeight * scale));
  
  for(int row = 0; row < oldHeight*scale; row++) {
      for(int col = 0; col < oldWidth*scale; col++) {
          Pixel origPix = pic.getPixel((int)(col/scale), (int)(row/scale));
          newImg.getPixel(col, row).setColor(origPix.getColor());
      }
  }
  
  return newImg;
}