Standard conversion formula:
gray = r * 0.299 + g * 0.587 + b * 0.114
The RGB three channel values are multiplied by one coefficient (weight) respectively, and then the sum of the three coefficients is 1, which is the weighted average.
Code:
public BufferedImage transferGrayImage(BufferedImage image) { BufferedImage grayImage = new BufferedImage(image.getWidth(), image.getHeight(), image.getType()); for (int i = 0; i < image.getWidth(); i++) { for (int j = 0; j < image.getHeight(); j++) { int rgb = image.getRGB(i, j); int r = (rgb >> 16) & 0xff; int g = (rgb >> 8) & 0xff; int b = rgb & 0xff; int gray = (r * 299 + g * 587 + b * 114 + 500) / 1000; rgb = (255 & 0xff) << 24 | (gray & 0xff) << 16 | (gray & 0xff) << 8 | gray & 0xff; grayImage.setRGB(i, j, rgb); } } return grayImage; }
2. Photoshop grayscale conversion formula:
Gray = (0.2973*R^2.2 + 0.6274 * G^2.2 + 0.0753 * B^2.2 )^(1/2.2)
Photoshop converts RGB channels by exponential 2.2 power operations, weighted averaging, and then 2.2 power operations. Because the calculation process includes power operation and multiplication operation, the calculation amount of the processing process is relatively large, which can be optimized by looking up tables.
Code:
public BufferedImage transferGrayImageByAdobe(BufferedImage image) { BufferedImage grayImage = new BufferedImage(image.getWidth(), image.getHeight(), image.getType()); for (int i = 0; i < image.getWidth(); i++) { for (int j = 0; j < image.getHeight(); j++) { int rgb = image.getRGB(i, j); double r = (rgb >> 16) & 0xff; double g = (rgb >> 8) & 0xff; double b = rgb & 0xff; double gray = Math.pow( 0.2973 * Math.pow(r, 2.2) + 0.6274 * Math.pow(g, 2.2) + 0.0753 * Math.pow(b, 2.2), 1.0 / 2.2); rgb = (255 & 0xff) << 24 | ((int) gray & 0xff) << 16 | ((int) gray & 0xff) << 8 | (int) gray & 0xff; grayImage.setRGB(i, j, rgb); } } return grayImage; }
3. RGB Mean to Gray Level
The average values of three RGB channels are calculated as the gray value of the pixel.
Code:
public BufferedImage transferGrayImageByAverage(BufferedImage image) { BufferedImage grayImage = new BufferedImage(image.getWidth(), image.getHeight(), image.getType()); for (int i = 0; i < image.getWidth(); i++) { for (int j = 0; j < image.getHeight(); j++) { int rgb = image.getRGB(i, j); int r = (rgb >> 16) & 0xff; int g = (rgb >> 8) & 0xff; int b = rgb & 0xff; int gray = (r + g + b) / 3; rgb = (255 & 0xff) << 24 | (gray & 0xff) << 16 | (gray & 0xff) << 8 | gray & 0xff; grayImage.setRGB(i, j, rgb); } } return grayImage; }
Fourth, the lowest RGB channel to gray level
The smallest value of RGB channel is the most gray value.
public BufferedImage transferGrayImageByLowest(BufferedImage image) { BufferedImage grayImage = new BufferedImage(image.getWidth(), image.getHeight(), image.getType()); for (int i = 0; i < image.getWidth(); i++) { for (int j = 0; j < image.getHeight(); j++) { int rgb = image.getRGB(i, j); int r = (rgb >> 16) & 0xff; int g = (rgb >> 8) & 0xff; int b = rgb & 0xff; int[] arr = new int[] { r, g, b }; Arrays.sort(arr); int gray = arr[0]; rgb = (255 & 0xff) << 24 | (gray & 0xff) << 16 | (gray & 0xff) << 8 | gray & 0xff; grayImage.setRGB(i, j, rgb); } } return grayImage; }
5. Conversion of the highest RGB channel to gray level
public BufferedImage transferGrayImageByHighest(BufferedImage image) { BufferedImage grayImage = new BufferedImage(image.getWidth(), image.getHeight(), image.getType()); for (int i = 0; i < image.getWidth(); i++) { for (int j = 0; j < image.getHeight(); j++) { int rgb = image.getRGB(i, j); int r = (rgb >> 16) & 0xff; int g = (rgb >> 8) & 0xff; int b = rgb & 0xff; int[] arr = new int[] { r, g, b }; Arrays.sort(arr); int gray = arr[2]; rgb = (255 & 0xff) << 24 | (gray & 0xff) << 16 | (gray & 0xff) << 8 | gray & 0xff; grayImage.setRGB(i, j, rgb); } } return grayImage; }