Dip Tutorial
Dip Tutorial
Audience
This reference has been prepared for the beginners to help them understand and
implement the basic to advance algorithms of digital image processing in java.
Prerequisites
Before proceeding with this tutorial, you need to have a basic knowledge of digital image
processing and Java programming language.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher. We strive to update the contents of our website and tutorials as timely
and as precisely as possible, however, the contents may contain inaccuracies or errors.
Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or
completeness of our website or its contents including this tutorial. If you discover any
errors on our website or in this tutorial, please notify us at contact@tutorialspoint.com.
i
Java Digital Image Processing
Contents
About the Tutorial .................................................................................................................................... i
Audience .................................................................................................................................................. i
Prerequisites ............................................................................................................................................ i
Contents.................................................................................................................................................. ii
Constructors............................................................................................................................................ 2
Methods.................................................................................................................................................. 2
Example .................................................................................................................................................. 3
Output .................................................................................................................................................... 4
Downloading an Image............................................................................................................................ 6
Example .................................................................................................................................................. 7
Output .................................................................................................................................................... 8
Example ................................................................................................................................................ 10
Output .................................................................................................................................................. 13
Example ................................................................................................................................................ 16
Output .................................................................................................................................................. 18
ii
Java Digital Image Processing
Example ................................................................................................................................................ 20
Output .................................................................................................................................................. 21
Example ................................................................................................................................................ 24
Output .................................................................................................................................................. 25
Example ................................................................................................................................................ 27
Output .................................................................................................................................................. 28
Example ................................................................................................................................................ 31
Output .................................................................................................................................................. 32
Example ................................................................................................................................................ 35
Output .................................................................................................................................................. 36
Example ................................................................................................................................................ 40
Output .................................................................................................................................................. 41
Example ................................................................................................................................................ 45
Output .................................................................................................................................................. 46
Example ................................................................................................................................................ 50
Output .................................................................................................................................................. 51
Example ................................................................................................................................................ 55
iii
Java Digital Image Processing
Output .................................................................................................................................................. 57
Example ................................................................................................................................................ 59
Output .................................................................................................................................................. 60
Example ................................................................................................................................................ 63
Output .................................................................................................................................................. 64
Example ................................................................................................................................................ 68
Output .................................................................................................................................................. 69
Example ................................................................................................................................................ 73
Output .................................................................................................................................................. 74
Example ................................................................................................................................................ 76
Output .................................................................................................................................................. 77
Example ................................................................................................................................................ 81
Output .................................................................................................................................................. 82
Example ................................................................................................................................................ 85
Output .................................................................................................................................................. 87
Example ................................................................................................................................................ 91
iv
Java Digital Image Processing
Output .................................................................................................................................................. 93
Example ................................................................................................................................................ 97
Output .................................................................................................................................................. 99
Endrov................................................................................................................................................. 127
v
Java Digital Image Processing
vi
1. JAVA DIP — INTRODUCTION
Java Digital Image Processing
Digital Image Processing (DIP) deals with manipulation of digital images using a computer. It
is a subfield of signals and systems but focuses particularly on images. DIP focuses on
developing a computer system that is able to perform processing on an image. The input of
such system is a digital image. The system processes the image using efficient algorithms,
and gives an image as an output.
Java is a high level programming language that is widely used in the modern world. It can
support and handle digital image processing efficiently using various functions.
1
2. JAVA DIP — JAVA BUFFEREDIMAGE CLASS
Java Digital Image Processing
Java BufferedImage class is a subclass of Image class. It is used to handle and manipulate
the image data. A BufferedImage is made of ColorModel of image data. All BufferedImage
objects have an upper left corner coordinate of (0, 0).
Constructors
This class supports three types of constructors. The first constructor constructs a new
BufferedImage with a specified ColorModel and Raster.
The second constructor constructs a BufferedImage of one of the predefined image types.
The third constructor constructs a BufferedImage of one of the predefined image types:
TYPE_BYTE_BINARY or TYPE_BYTE_INDEXED.
Methods
Sr. No. Methods
1 copyData(WritableRaster outRaster)
2 getColorModel()
3 getData()
4 getData(Rectangle rect)
2
Java Digital Image Processing
5 getGraphics()
6 getHeight()
7 getMinX()
8 getMinY()
9 getRGB(int x, int y)
It returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) and
default sRGB colorspace.
10 getType()
Example
The following example demonstrates the use of java BufferedImage class that draws some
text on the screen using Graphics Object:
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
3
Java Digital Image Processing
g.drawImage(img, 20,20,this);
BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);
Graphics g = bufferedImage.getGraphics();
g.drawString("www.tutorialspoint.com", 20,20);
g.drawString("www.tutorialspoint.com", 20,40);
g.drawString("www.tutorialspoint.com", 20,60);
g.drawString("www.tutorialspoint.com", 20,80);
g.drawString("www.tutorialspoint.com", 20,100);
return bufferedImage;
frame.getContentPane().add(new Test());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
Output
When you execute the given code, the following output is seen:
4
Java Digital Image Processing
5
3. JAVA DIP — DOWNLOADING / UPLOADING IMAGES
Java Digital Image Processing
In this chapter we are going to see how you can download an image from internet, perform
some image processing techniques on the image, and then again upload the processed image
to a server.
Downloading an Image
In order to download an image from a website, we use Java class named URL, which can be
found under java.net package. Its syntax is given below:
Apart from the above method, there are other methods available in class URL as described
briefly:
6
Java Digital Image Processing
Example
The following example demonstrates the use of java URL class to download an image from
the internet:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
try{
String website =
"http://tutorialspoint.com/java_dip/images/"+fileName;
int length = 0;
outputStream.write(buffer, 0, length);
7
Java Digital Image Processing
inputStream.close();
outputStream.close();
}catch(Exception e){
Output
When you execute the given code, the following output is seen:
8
Java Digital Image Processing
Uploading an Image
Let us see how to upload an image to a webserver. We convert a BufferedImage to byte array
in order to send it to server.
We use Java class ByteArrayOutputStream, which can be found under java.io package. Its
syntax is given below:
Apart from the above method, there are other methods available in the
ByteArrayOutputStream class as described briefly:
This method resets the number of valid bytes of the byte array output stream to
zero, so that all the accumulated output in the stream is discarded.
9
Java Digital Image Processing
This method creates a newly allocated Byte array. Its size would be the current
size of the output stream and the contents of the buffer will be copied into it. It
returns the current contents of the output stream as a byte array.
Converts the buffer content into a string. Translation will be done according to the
default character encoding. It returns the String translated from the buffer's
content.
It writes len number of bytes starting from offset off to the stream.
It writes the entire content of this Stream to the specified stream argument.
Example
The following example demonstrates ByteArrayOutputStream to upload an image to the
server:
Client Code
import javax.swing.*;
import java.net.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
10
Java Digital Image Processing
Socket soc;
soc=new Socket("localhost",4000);
try {
baos.flush();
baos.close();
dos.writeInt(bytes.length);
dos.write(bytes, 0, bytes.length);
dos.close();
out.close();
}catch (Exception e) {
soc.close();
11
Java Digital Image Processing
soc.close();
Server Code
import java.net.*;
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
class Server {
ServerSocket server=null;
Socket socket;
server=new ServerSocket(4000);
socket=server.accept();
System.out.println("Client connected.");
InputStream in = socket.getInputStream();
dis.readFully(data);
dis.close();
12
Java Digital Image Processing
in.close();
l.setIcon(icon);
f.add(l);
f.pack();
f.setVisible(true);
Output
Client Side Output
When you execute the client code, the following output appears on client side:
After receiving the image, the server displays the image as shown below:
13
Java Digital Image Processing
14
4. JAVA DIP — IMAGE PIXELS
Java Digital Image Processing
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 color or grayscale.
In Java, the BufferedImage class is used to handle images. You need to call getRGB() method
of the BufferedImage class to get the value of the pixel.
c.getRed();
c.getGreen();
c.getBlue();
Apart from these methods, there are other methods supported in the BufferedImage class.
They are described briefly:
1 copyData(WritableRaster outRaster)
15
Java Digital Image Processing
2 getColorModel()
3 getData()
4 getData(Rectangle rect)
5 getGraphics()
6 getHeight()
7 getMinX()
8 getMinY()
9 getRGB(int x, int y)
It returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) and
default sRGB colorspace.
10 getType()
Example
The following example demonstrates the use of java BufferedImage class that displays pixels
of an image of size (10 x 10):
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
16
Java Digital Image Processing
import javax.swing.JFrame;
class Pixel {
BufferedImage image;
int width;
int height;
public Pixel() {
try {
image = ImageIO.read(input);
width = image.getWidth();
height = image.getHeight();
int count = 0;
count++;
} catch (Exception e) {}
17
Java Digital Image Processing
Output
When you execute the above example, it would print the pixels of the following image:
Original Image
Pixels Output
18
Java Digital Image Processing
19