开发者

How to get the RGB values of a 5x5 square?

I am writing a method that is used to get the RGB values of a 5x5 square of pixels and then calculate the average colour of all of them. We were given a package to use, and that's where the .getPixel methods and such are from.

What I am having difficulty with is with getting each pixel in the 5x5 area. What I have tried is running the loop and at the end having x++ so x is increased by one. But I realize that this only gets the pixels in the top row.

I think I must have another loop or a nested loop of somekind to get the 5x5 square but I just can't figure out how to make it work while still adding the values to the vars red,开发者_JAVA技巧green, and blue.

Any hints? Thanks

private Color tileColor ( Picture pic, int x, int y ) {

  Pixel p;
  Color color;
  int r,g,b;
  int red = 0;
  int green = 0;
  int blue = 0;

  for (int i = 0; i <= 5; i++){

    p=  pic.getPixel(x,y);
    color = p.getColor();

    r = color.getRed();
    g = color.getGreen();
    b = color.getBlue();

    red = red + r;
    green = green + g;
    blue = blue + b;

    x++
  }
}


typically, we can write:

for (int x = 0; x < 5; x++) {
    for (int y = 0; y < 5; y++) {
        p =  pic.getPixel(x, y);
        // do something ....
    }
}

Make sure if it is 5 pixels, then 0, 1, 2, 3, 4, is already 5 pixels.

Another common usage is:

for (int dx = 0; dx < 5; dx++) {
    for (int dy = 0; dy < 5; dy++) {
        p =  pic.getPixel(100 + dx, 200 + dy);
        // do something ....
    }
}

which is to get the pixels relative to the pixel (100, 200). dx and dy can be read as delta x and delta y (the small increments).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜