📜  Java中的图像处理——彩色到红绿蓝图像的转换

📅  最后修改于: 2022-05-13 01:54:39.280000             🧑  作者: Mango

Java中的图像处理——彩色到红绿蓝图像的转换

先决条件:

  • Java中的图像处理——读写
  • Java中的图像处理——获取和设置像素
  • Java中的图像处理——彩色图像到灰度图像的转换
  • Java中的图像处理——彩色图像到负图像的转换

在这组中,我们将彩色图像转换为具有红色效果、绿色效果或蓝色效果的图像。

彩色图像 -彩色图像或 RGB 颜色模型是一种加法混合模型,其中红色、绿色和蓝色光以各种方式添加在一起,以再现广泛的颜色阵列。

基本思想是获取每个坐标的像素值,然后保持所需的结果颜色像素值相同,并将其他两个设置为零。

转换为红色图像

算法:

  1. 获取像素的 RGB 值。
  2. 设置 RGB 值如下:
    • R:没有变化
    • G:设置为 0
    • B:设置为 0
  3. 将像素的 R、G 和 B 值替换为步骤 2 中计算的值。
  4. 对图像的每个像素重复步骤 1 到步骤 3。

执行:

Java
// Java program to demonstrate colored
// to red colored image conversion
  
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
  
public class RedImage {
    public static void main(String args[])
        throws IOException
    {
        BufferedImage img = null;
        File f = null;
  
        // read image
        try {
            f = new File(
                "C:/Users/hp/Desktop/Image Processing in Java/gfg-logo.png");
            img = ImageIO.read(f);
        }
        catch (IOException e) {
            System.out.println(e);
        }
  
        // get width and height
        int width = img.getWidth();
        int height = img.getHeight();
  
        // convert to red image
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int p = img.getRGB(x, y);
  
                int a = (p >> 24) & 0xff;
                int r = (p >> 16) & 0xff;
  
                // set new RGB keeping the r
                // value same as in original image
                // and setting g and b as 0.
                p = (a << 24) | (r << 16) | (0 << 8) | 0;
  
                img.setRGB(x, y, p);
            }
        }
  
        // write image
        try {
            f = new File(
                "C:/Users/hp/Desktop/Image Processing in Java/GFG.png");
            ImageIO.write(img, "png", f);
        }
        catch (IOException e) {
            System.out.println(e);
        }
    }
}


Java
// Java program to demonstrate colored
// to green coloured image conversion
  
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
  
public class GreenImage {
    public static void main(String args[])
        throws IOException
    {
        BufferedImage img = null;
        File f = null;
  
        // read image
        try {
            f = new File(
                "C:/Users/hp/Desktop/Image Processing in Java/gfg-logo.png");
            img = ImageIO.read(f);
        }
        catch (IOException e) {
            System.out.println(e);
        }
  
        // get width and height
        int width = img.getWidth();
        int height = img.getHeight();
  
        // convert to green image
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int p = img.getRGB(x, y);
  
                int a = (p >> 24) & 0xff;
                int g = (p >> 8) & 0xff;
  
                // set new RGB
                // keeping the g value same as in original
                // image and setting r and b as 0.
                p = (a << 24) | (0 << 16) | (g << 8) | 0;
  
                img.setRGB(x, y, p);
            }
        }
  
        // write image
        try {
            f = new File(
                "C:/Users/hp/Desktop/Image Processing in Java/GFG.png");
            ImageIO.write(img, "png", f);
        }
        catch (IOException e) {
            System.out.println(e);
        }
    }
}


Java
// Java program to demonstrate colored
// to blue coloured image conversion
  
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
  
public class BlueImage {
    public static void main(String args[])
        throws IOException
    {
        BufferedImage img = null;
        File f = null;
  
        // read image
        try {
            f = new File(
                "C:/Users/hp/Desktop/Image Processing in Java/gfg-logo.png");
            img = ImageIO.read(f);
        }
        catch (IOException e) {
            System.out.println(e);
        }
  
        // get width and height
        int width = img.getWidth();
        int height = img.getHeight();
  
        // convert to blue image
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int p = img.getRGB(x, y);
  
                int a = (p >> 24) & 0xff;
                int b = p & 0xff;
  
                // set new RGB
                // keeping the b value same as in original
                // image and setting r and g as 0.
                p = (a << 24) | (0 << 16) | (0 << 8) | b;
  
                img.setRGB(x, y, p);
            }
        }
  
        // write image
        try {
            f = new File(
                "C:/Users/hp/Desktop/Image Processing in Java/GFG.png");
            ImageIO.write(img, "png", f);
        }
        catch (IOException e) {
            System.out.println(e);
        }
    }
}


输出 -

转换为绿色图像

算法

  1. 获取像素的 RGB 值。
  2. 设置 RGB 值如下:
    • R:设置为 0
    • G:没有变化
    • B:设置为 0
  3. 将像素的 R、G 和 B 值替换为步骤 2 中计算的值。
  4. 对图像的每个像素重复步骤 1 到步骤 3。

执行:

Java

// Java program to demonstrate colored
// to green coloured image conversion
  
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
  
public class GreenImage {
    public static void main(String args[])
        throws IOException
    {
        BufferedImage img = null;
        File f = null;
  
        // read image
        try {
            f = new File(
                "C:/Users/hp/Desktop/Image Processing in Java/gfg-logo.png");
            img = ImageIO.read(f);
        }
        catch (IOException e) {
            System.out.println(e);
        }
  
        // get width and height
        int width = img.getWidth();
        int height = img.getHeight();
  
        // convert to green image
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int p = img.getRGB(x, y);
  
                int a = (p >> 24) & 0xff;
                int g = (p >> 8) & 0xff;
  
                // set new RGB
                // keeping the g value same as in original
                // image and setting r and b as 0.
                p = (a << 24) | (0 << 16) | (g << 8) | 0;
  
                img.setRGB(x, y, p);
            }
        }
  
        // write image
        try {
            f = new File(
                "C:/Users/hp/Desktop/Image Processing in Java/GFG.png");
            ImageIO.write(img, "png", f);
        }
        catch (IOException e) {
            System.out.println(e);
        }
    }
}

输出 -

转换为蓝色图像

算法

  1. 获取像素的 RGB 值。
  2. 设置 RGB 值如下:
    • R:设置为 0
    • G:设置为 0
    • B:没有变化
  3. 将像素的 R、G 和 B 值替换为步骤 2 中计算的值。
  4. 对图像的每个像素重复步骤 1 到步骤 3。

执行

Java

// Java program to demonstrate colored
// to blue coloured image conversion
  
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
  
public class BlueImage {
    public static void main(String args[])
        throws IOException
    {
        BufferedImage img = null;
        File f = null;
  
        // read image
        try {
            f = new File(
                "C:/Users/hp/Desktop/Image Processing in Java/gfg-logo.png");
            img = ImageIO.read(f);
        }
        catch (IOException e) {
            System.out.println(e);
        }
  
        // get width and height
        int width = img.getWidth();
        int height = img.getHeight();
  
        // convert to blue image
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int p = img.getRGB(x, y);
  
                int a = (p >> 24) & 0xff;
                int b = p & 0xff;
  
                // set new RGB
                // keeping the b value same as in original
                // image and setting r and g as 0.
                p = (a << 24) | (0 << 16) | (0 << 8) | b;
  
                img.setRGB(x, y, p);
            }
        }
  
        // write image
        try {
            f = new File(
                "C:/Users/hp/Desktop/Image Processing in Java/GFG.png");
            ImageIO.write(img, "png", f);
        }
        catch (IOException e) {
            System.out.println(e);
        }
    }
}

输出 -