General discussion

Locked

image processing

By sunrise ·
hi

how can i convert an image to the gray scale using code?

thanks a lot

This conversation is currently closed to new comments.

1 total post (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

by zilaq In reply to image processing

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).

Back to Web Development Forum
1 total post (Page 1 of 1)  

Related Discussions

Related Forums