How to Set/modify the pixels(RGB values) of an image using Java OpenCV Library?


A pixel is the smallest element of a digital image, each pixel contains the values of alpha, red, green, blue values. The pixel value(s) is stored in a 32-bit memory space holding ARGB values (8 bits each) in the same order. Therefore, to modify the color of an image −

  • You need to loop through each pixel in the image.

  • Get the pixel value using the getRGB() method.

  • Retrieve the ARGB values from the pixel value.

  • Perform the required modifications to the RGB values.

  • Create a new pixel value with the modified RGB values.

  • Set the new pixel value(s) using the setRGB() method.

Setting the ARGB values −

Instantiate the Color object by passing the pixel value as a parameter. Get the Red, Green, Blue values using the getRed(), getGreen() and getBlue() methods respectively. After the modifications to set the values back −

  • Create a Color object bypassing the new RGB values as parameters.

  • Get the pixel value from the color object using the getRGB() method of the Color class.

  • Set the new pixel value to the image by passing the x and y positions along with the new pixel value to the setRGB() method.

Example

import java.io.File;
import java.io.IOException;
import java.awt.Color;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class SetPixels {
   public static void main(String args[])throws IOException {
      //Reading the image
      File file= new File("D:\Images\cat.jpg");
      BufferedImage img = ImageIO.read(file);
      for (int y = 0; y < img.getHeight(); y++) {
         for (int x = 0; x < img.getWidth(); x++) {
            //Retrieving contents of a pixel
            int pixel = img.getRGB(x,y);
            //Creating a Color object from pixel value
            Color color = new Color(pixel, true);
            //Retrieving the R G B values
            int red = color.getRed();
            int green = color.getGreen();
            int blue = color.getBlue();
            //Modifying the RGB values
            green = 150;
            blue = 150;
            //Creating new Color object
            color = new Color(red, green, blue);
            //Setting new Color object to the image
            img.setRGB(x, y, color.getRGB());
         }
      }
      //Saving the modified image
      file = new File("D:\Images\setting_pixels.jpg");
      ImageIO.write(img, "jpg", file);
      System.out.println("Done...");
   }
}

Input

Output

Setting the ARGB values using shift operators −

To retrieve each value from a pixel you need to right shift to the beginning position of each color i.e. 24 for alpha 16 for red etc. and perform bitwise and operation with 0Xff. This masks the variable leaving the last 8 bits and ignoring all the rest of the bits.

you can reconstruct a pixel by shifting the ARGB to left in their respective positions and join them using bitwise OR.

//Set new RGB value
p = (a<<24) | (r<<16) | (g<<8) | b;
img.setRGB(x, y, p);

Updated on: 08-Apr-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements