Image Processing in Java | Set 3 (Colored image to greyscale image conversion)
We strongly recommend to refer below post as a prerequisite of this.
- Image Processing in Java | Set 1 (Read and Write)
- Image Processing In Java | Set 2 (Get and set Pixels)
In this set we will be converting a coloured image to greyscale image.
Note(Think intuitively): In a greyscale image the Alpha component of the image will be same as the original image, but the RGB wil be changed i.e, all three RGB components will be having a same value for each pixels.
- Get the RGB value of the pixel.
- Find the average of RGB i.e., Avg = (R+G+B)/3
- Replace the R, G and B value of the pixel with average (Avg) calculated in step 2.
- Repeat Step 1 to Step 3 for each pixels of the image.
Implementation of the above algorithm:
Note : This code will not run on online IDE as it needs an image on disk.
In the next set we will be learning how to convert a colored image to negative image in JAVA.
This article is contributed by Pratik Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.