Image Processing and Computer vision in java (Intro)

Hi guys,this is my first tutorial on digital Image processing and computer vision in java. I assume that you have a better understanding in java programming language. First of all in these sets of tutorials I will discuss about image processing and cv algorithms and show you how to implement those in java. To make things easy and more understandable I will try to make the codes as simple as possible. For that the code won’t be so efficient but if you get the idea you can implement the code as efficient as you can.

At first we have to understand the structure of an image.  Images are nothing but a two-dimensional array of pixels. Each pixel represents a point of an image. As it is a two-dimensional structure every pixel has a (x,y) position.  In the figure below there are two pixel with a value of 85 appear in (2, 3) and (0, 5) position. The position of the first pixel is (0, 0).


Basically there are three types of images:

  1. Binary image in which each pixel has 2 bit value ‘0’ and ‘1’. 0 represents black and 1 represents white color. Actually 255 represents white color but conventionally we wrote it 1.
  2. Grey Image in which each pixel has 8 bit value from 0 to 255.
  3. Color image in which each pixel has three channels for ‘red’, ‘green’ and ‘blue’. Each channel has 8 bit value from 0 to 255. So a color image has a total of 24 bit value and if we add 8 bits of alpha value then color images actually have 32 bits of value. So there are 224 which is 16.7 million combination of colors we can see with our eyes and that’s why some LED screens label says it has 16 million colors.

In these tutorials we will use grey level images for better understanding but after that I will show you how to perform image processing in colored image.

Loading and displaying an Image:

To read an image in java we need to use ‘ImageIO’ class and save the data into a ‘BufferedImage’.

 BufferedImage image = File("C:\\Users\\nausad\\Pictures\\Test.JPG"));



and to Display an image we can use “JFrame”.

 //add the image in JLable
 JLabel label = new JLabel(new ImageIcon(image));

 JFrame f = new JFrame();
 //Exit whenever colose button pressed
 //add label in JFrame
 //display the image to its original size
 //center the window




The full code of loading and displaying an image in java is given below

 import PointOperation.PointOperation;

 import java.awt.image.BufferedImage;


 import javax.imageio.ImageIO;

 public class Test {

 public static void main(String[] args){
 //Exception must handled to use ImageIO

         BufferedImage image = File("C:\\Users\\nausad\\Pictures\\Test.JPG"));

         JLabel label = new JLabel(new ImageIcon(image));

         JFrame f = new JFrame();






     }catch(Exception e){

     System.err.println("Cannot load image");






Getting the pixel values of an Image:

To access each pixel value of an Image we need two for loop  to iterate over the two-dimensional array. We can create a method which takes a BufferdImage   as a parameter  and iterate each pixel of the image and print their value.

 public void rgbToGrey(BufferedImage img){

//pixel array to hold the r,g,b value of the pixel
         int pixel[];
         for(int i=0; i<img.getWidth(); i++){
             for(int j=0; j<img.getHeight(); j++){

//get the r,g,b value of the pixel at ( i , j ) location
                 pixel = img.getRaster().getPixel(i, j, new int[3]);

//at 0 index the array contains the red pixel value and 1, 2 index have the value of green and blue respectively
                 System.out.println("R: "+pixel[0]+" G: "+pixel[1]+" B: "+pixel[2]);


On our next tutorial I will show you pixel manipulation and point operations




One thought on “Image Processing and Computer vision in java (Intro)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s