image processing
hi
how can i convert an image to the gray scale using code?
thanks a lot
Web Development
hi
how can i convert an image to the gray scale using code?
thanks a lot
by zilaq
This is a code snippet from an application I wrote a few years ago:
Picture pic = new Picture(args[0]);
int width = pic.width();
int height = pic.height();
// convert to grayscale
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
Color c = pic.getColor(i, j);
int r = c.getRed();
int g = c.getGreen();
int b = c.getBlue();
int gray = (int) (0.299*r + 0.587*g + 0.114*b); // NTSC formula
c = new Color(gray, gray, gray);
pic.setColor(i, j, c);
}
}
It's not optimised, but it will do the work (if you want more speed, you should try accessing the pixel data directly, without the getColor/setColor functions).
This discussion is closed to new replies.
Sort by Trending