Understand Image Pixel Tutorial

Advertisements


An image contains a two dimensional array of pixels. It is actually the value of those pixels that make up an image. Usually an image could be colour or grayscale.

In Java , BufferedImage class is used to handle images. You need to call getRGB() method of the BufferedImage class to get the value of the pixel.

Getting Pixel Value

Color c = new Color(image.getRGB(j, i));

Getting RGB Values

The method getRGB takes the row and column index as a parameter and returns the appropriate pixel. In case of colour image , it returns three values which are (R,G,B). They can be get as follows:

c.getRed();
c.getGreen();
c.getBlue();

Getting Width and height of Image

The height and width of the image can be get by calling the getWidth and getHeight method of the BufferedImage class. Its syntax is given below:

int width = image.getWidth();
int height = image.getHeight();

Apart from these methods , there are other methods supported in the BufferedImage class. They are listed below:

Methods and Description

Sr.NoMethod & Function
1copyData(WritableRaster outRaster)
Computes an arbitrary rectangular region of the BufferedImage and copies it into a specified WritableRaster.
2getColorModel()
Return ColorModel of an image.
3getData()
Returns the image as one large tile.
4getData(Rectangle rect)
Computes and returns an arbitrary region of the BufferedImage.
5getGraphics()
This method returns a Graphics2D, but is here for backwards compatibility.
6getHeight()
Returns the height of the BufferedImage.
7getMinX()
Returns the minimum x coordinate of this BufferedImage.
8getMinY()
Returns the minimum y coordinate of this BufferedImage.
9getRGB(int x, int y)
Returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) and default sRGB colorspace.
10getType()
Returns the image type.

Example

The following example demonstrates the use of java BufferedImage class that display pixels of an image of size (10 x 10).

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
class Pixel {
   BufferedImage  image;
   int width;
   int height;
   public Pixel() {
      try {
         File input = new File("blackandwhite.jpg");
         image = ImageIO.read(input);
         width = image.getWidth();
         height = image.getHeight();
         int count = 0;
         for(int i=0; i<height; i++){
            for(int j=0; j<width; j++){
               count++;
               Color c = new Color(image.getRGB(j, i));
               System.out.println("S.No: " + count + " Red: " + c.getRed() +
               " Green: " + c.getGreen() + " Blue: " + c.getBlue());
               }
            }
      } catch (Exception e) {}
   }
   static public void main(String args[]) throws Exception 
   {
      Pixel obj = new Pixel();
   }
}

Output

When you run the above example, It would print the pixels of the following below figure:

Original Image.

Understand Image Pixel Tutorial

Pixels Output

Understand Image Pixel Tutorial

Scroll down the ouput, and you will see the following thing in the end.

Understand Image Pixel Tutorial

Advertisements
Advertisements