IN JAVA: Write code to convert a color image to a greyscale image (with only sha
ID: 3753253 • Letter: I
Question
IN JAVA:
Write code to convert a color image to a greyscale image (with only shades of grey). To do this, recall that every pixel is four values (alpha, red, green, blue). If you make the values of red, green and blue the same, it becomes grey. So, what you need to do is:
Download this template and this test image. Implement your code in the method toGreyScale(). The pixels for the original image and the creation of the pixel array for the grey-image have been done for you. Remember to copy the alpha value without changing it. To make the RGB values the same in the grey-image, simply take the average of the R, G and B values in the original.
Explanation / Answer
Hey to convert a coloured image in to grey image, we have to make R,B,G as same value by taking the avg of R,B and G as you have already explained. So for each and every row and each and every column, I am taking the average of R,B and G colours from pixels and putting them in greyPixels so that you will get grey image.
Code:
Note:
1)Please make sure that you are giving the crt path for the image file. If classes and image file are in the same package then you can use like that directly. But image is located at the different location than the code, then give the path accordingly. For example your image is at the location C->Users->src->Image then use the path C:/Users/src/statue.jpg. Hope you understood.
2)I am assuming that you have ImageTool class that you have posted in the question. If you don't have it. If you dont have please find the code for the ImageTool class at the bottom of this answer.
3)If you are getting any kind of errors, please let me know. I will be happy resolving the errors.
4)Please give your valuable feedback abd rating.. Thank you.. Hope this helps you.
ImageTool: