JSPM KK

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/312294616

A Comparative study of Converting Coloured Image to Gray-scale Image using


Different Technologies

Conference Paper · March 2012

CITATIONS READS

3 3,354

1 author:

Kavita Khobragade
Fergusson College Pune
14 PUBLICATIONS 19 CITATIONS

SEE PROFILE

All content following this page was uploaded by Kavita Khobragade on 13 January 2017.

The user has requested enhancement of the downloaded file.


A Comparative study of Converting Coloured Image to Gray-scale Image using Different
Technologies

Ms. Kavita Khobragade


Asst. Professor, Department of Computer Science,
Fergusson College,
Pune, India
[email protected]

Abstract— Image processing is the new era of recent of the primary colors. A 16-bit digital grayscale image
research. Image Processing is a technology where consumes far more memory or storage than the same image
programs are written to handle and manipulate the image with the same physical dimensions in 8-bit digital
in terms of pixels. Latest technologies provide the easiest grayscale.[1] The coloured image is shown in figure 1.
way of writing the image processing programs. Some of
them are C, C++, Java, Matlab, OpenCV, Python etc. Java
is a programming language with image processing API
support. OpenCV is the library of programming functions
which works on classes and functions. Matlab is the
Matrix laboratory which works on matrices and vectors.
The purpose of this paper is to provide an introduction to
different technologies such as OpenCV, Java and Matlab
and explain pros and cons of each. These technologies are
used to convert coloured image to a gray-scale image. This
paper shows the comparative study of these technologies.
Fig.1 : Coloured Image
Keywords- Image Processing, Coloured image, Gray Scale
image, OpenCV, Java, Matlab. 1.2. Grayscale Image
.
Grayscale is a range of shades of gray without apparent
1. Introduction color. The darkest possible shade is black and the lightest
Image processing is the rapidly growing technology possible shade is white. Black means the total absence of
today. It is applied in various aspects and various areas of a transmitted or reflected light and the white means total
business. It forms core research area within engineering and transmission or reflection of light at all visible wavelength.
computer science disciplines. Image processing is a method Intermediate shades of gray are represented by equal
which converts an image into digital form and performs some brightness levels of the three primary colors [red, green and
operations on it. This process is done to get an enhanced blue]. [2] The grayscale image is shown in figure 2.
image or to extract useful information from it. In this case the
input and output is also an image. Usually Image Processing
system includes treating images as two dimensional signals
while applying already set signal processing methods to
them.[6]

1.1. Coloured Image

In real life we generally get coloured images. Some


systems use the RGB color model. There are 2 16 or 65,636
possible levels for each primary color. When the image is
converted as R = G = B then the image is known as 16-bit
grayscale. This is because the decimal number 65,536 is
equivalent to the 16-digit binary number 1111111111111111.
In 8-bit grayscale image the lightness of the gray is directly
proportional to the number representing the brightness levels Fig. 2: Grayscale Image
1.3. Introduction to OpenCV with 8-bit and a single plane. The memory is automatically
released by cvReleaseImage.
OpenCV stands for Open Source Computer Vision. It is a
#include “highgui.h”
library of programming functions used for real time computer int main()
vision. It was developed by Intel and used into robotics {
research. As name suggests it is open source and free for use. IplImage* img = cvLoadImage(“water.jpg”);
It has collection of large number of C functions and C++ IplImage* gray = cvCreateImage(cvSize(img-> width, img -
> height),8,1);
classes which supports Image Processing. [3] cvNamedWindow(“Example1”,cv_WINDOW_AUTOSIZE);
cvShowImage(“Example1”,img);
It is designed for the computational efficiency with strong cvWaitKey(0);
focus on real time applications. It provides active development cvCvtColor(img, gray, CV_RGB2GRAY);
cvShowImage(“Example2”,gray);
on interface for Python, Ruby, Matlab and other languages. It cvWaitKey(0);
contains over five hundred functions which includes medical cvRealeseImage(&img);
imaging, security, camera calibration, stereo vision and cvDestroyWindow(“Example1”);
robotics. Its main goal is to provide simple-to-use computer }
vision infrastructure. It is free and open source and works on Fig. 4. OpenCV program
Linux, Windows and MacOSX. The Eclipse IDE can be used
to write the programs. The basics structure of OpenCV is
shown in figure 3. 1.4. Introduction to Java

Java is a object oriented programming language


CV MLL HighGui developed by Sun Microsystems of USA in 1991. It is most
Image Statistical GUI, reliable and portable language. It is simple, powerful and
processing classifier image & platform neutral language. It can work on Linux and
& vision & video I/O Windows. It supports dynamic and automatic compilation,
algorithms clustering automatic garbage collection, event-driven programming,
tool
GUI, Networking and security. It does not allow using
pointers and multiple inheritances. It contains standard
libraries. Java provide support for multithreading and
exception handling. Java gives high performance due to usage
CXCore of intermediate byte-code by reducing the overheads at
Basic structures & algorithms, XML runtime.
support, drawing functions
Java provides input/output API for image. The name of
this API is javax.imageio. This is the main package. Many of
the common image I/O operations are performed by using
Fig. 3: Basic Structure of OpenCV static methods of the ImageIO class. This package contains the
basic classes and interfaces for describing the contents of
CV basically contains image processing, image structure image files. This includes metadata and thumbnails
analysis, motion and tracking, pattern recognition, camera (IIOImage). For controlling the image reading process
calibration etc. HighGui provides the graphical user interface ImageReader, ImageReadParam, and ImageTypeSpecifier are
and takes care of image storage. CXCore contains data available and for image writing process ImageWriter and
structure, matrix algebra, object persistence, error handling, ImageWriteParam are present. The process ImageTranscoder
drawing and basic math’s etc. It also works for dynamic is used to perform transcoding between formats and for
loading of code. MLL consists of many clustering, reporting errors IIOException is present.
classification and data analysis functions. [4]
Images are described by a width and heights measured in
The program to convert coloured image into gray scale using pixels and have a coordinate system that is independent of the
OpenCV is shown in figure 4. drawing surface. There are two main classes which support
java image processing viz. java.awt.image and
The cvLoadImage is a high level routine which determines the java.awt.image.BufferedImage. The Toolkit is a class in the
file format to be loaded based on the file name. It java.awt package that provides various resources and tools for
automatically allocates the memory needed for the image to the display system. One of the Toolkit methods is getImage().
store, and allocates a pointer. The cvNameWindow opens a It is overloaded to take either a String filename parameter
window on the screen and cvShowImage displays the image. specifying the location of the image file or a URL parameter
The window is visible on the screen till a key-press. The identifying the image file.
cvCvtColor converts coloured image into gray scale image
import javax.imageio.*;
import java.io.*;
import java.awt.image.*;
The BufferedImage subclass describes an Image with an import java.awt.*;
accessible buffer of image data. A BufferedImage is import javax.swing.*;
comprised of a ColorModel and a Raster of image data. All
public class imagegreyscale extends JPanel
BufferedImage objects have an upper left corner coordinate of {
(0, 0). Any Raster used to construct a BufferedImage must Image image,image1;
therefore have minX=0 and minY=0. The ColorModel abstract public imagegreyscale(String str, String str1) {
class encapsulates the methods for translating a pixel value to super();
image = Toolkit.getDefaultToolkit().getImage(str);
color components (for example, red, green, and blue) and an image1 = Toolkit.getDefaultToolkit().getImage(str1);
alpha component. To render an image to the screen, a printer, }
or another image, pixel values must be converted to color and public void paintComponent(Graphics g)
alpha components. {
g.drawImage(image,50,10,200,200, this);
g.drawImage(image1,270,10,200,200,this);
The number, order, and interpretation of color }
components for a ColorModel are specified by its ColorSpace. public static void main(String [] pie)
A ColorModel used with pixel data that does not include alpha {
information treats all pixels as opaque, which is an alpha value if(!((pie.length == 3) && (new File(pie[0]).exists()) &&
(pie[2].equals("jpg")||(pie[2].equals("bmp")||(pie[2].equals
of 1.0. This ColorModel class supports two representations of ("pgm")||(pie[2].equals("tif")||(pie[2].equals("gif")||(pie[2]
pixel values. A pixel value can be a single 32-bit integers or an .equals("jpeg") || pie[2].equals("png"))))))))) {
array of primitive types. A class represents a rectangular array System.out.println("\nOutput Format can be jpeg or
of pixels. png");
System.exit(0);
}
A Raster defines values for pixels occupying a particular BufferedImage image = null;
rectangular area of the plane, not necessarily including (0, 0). try { image = ImageIO.read(new File(pie[0])); }
The rectangle, known as the Raster's bounding rectangle and catch(IOException e) {
System.err.println("Error reading in file,
available by means of the getBounds method, is defined by check file name and format");
minX, minY, width, and height values. The minX and minY System.exit(0);
values define the coordinate of the upper left corner of the }
Raster. References to pixels outside of the bounding rectangle // loop through every pixel in the image
for(int x = 0; x <image.getWidth(); x ++) {
may result in an exception being thrown, or may result in for(int y = 0; y < image.getHeight(); y++)
references to unintended elements of the Raster's associated {
DataBuffer. int RGB = image.getRGB(x,y);
int blue = (RGB & 0x000000FF);
int green = (RGB & 0x0000FF00) >> 8;
ImageIO is a class which contains static convenience int red = (RGB & 0x00FF0000) >> 16;
method for locating ImageReaders and ImageWriters. int average = (blue + green + red)/3;
ImageReader is able to read the resulting stream and int newRGB = 0xFF000000 + (average <<
ImageWriter is able to write image. The program to convert 16) + (average << 8) + average;
image.setRGB(x,y,newRGB);
coloured image into gray scale using Java is shown in figure 5. }
}
try { ImageIO.write(image, pie[2], new File(pie[1])); }
catch(IOException e)
{ System.err.println("Unable to output results"); }
JFrame frame = new JFrame("ShowImage");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLO
SE);
frame.setSize(600,300);
imagegreyscale panel = new
imagegreyscale(pie[0],pie[1]);
frame.setContentPane(panel);
frame.setVisible(true);
}
}

Fig. 5. Java Program


1.5. Introduction to Matlab Few observations from this table are given below:
1. Writing code in Matlab is easier than writing code in
Matlab is Matrix Laboratory. Matlab is high-performance Java and OpenCV.
language for technical computing, user Friendly, easy to work 2. The header files need to be included or imported in Java
with and has the powerful mathematical commands. It works and OpenCV.
on data acquisition, data analysis, data exploration, integration 3. No need to remember the directory settings which are
and differentiation, matrices, linear equations etc. The done in OpenCV.
compilation and linking is not required in Matlab. It supports 4. IP Toolbox in Matlab provides complete help with
powerful mathematics. It is easy to create our own functions. examples.
Plotting different graphs is also easy. The memory allocation 5. Matlab directly works on the built-in functions.
and deletion is performed automatically. The program is 6. Directory setting is needed in OpenCV but Matlab
shown in figure 6. automatically selects a working directory.

i=imread('water.jpg'); Conclusion
%[a b c]=size(i);
figure(1), imshow(i);
r=i(:,:,1); This paper presents the comparison of OpenCV, Java and
g=i(:,:,2); Matlab, as image processing can be done using these
b=i(:,:,3); technologies. This paper shows the program code for
%for k=1:p
x= 0.2989 * r + 0.5870 * g+ 0.1140 * b;
converting coloured image to grayscale image and comparison
%end among them. From the above information it is observed that it
figure(2),imshow(x); is not required to remember the header file name needed for
the execution of a program. Secondly, the result / output vary
as far as intensities and contrast are considered. Thirdly, the
Fig.6: Matlab Program
size of the code is very small in Matlab than OpenCV and
Java. But Matlab gives very good intensities and contrast in
In this case the in-built rgb2gray() can be used. But the grayscale images. As future enhancement the
the conversion is done with the help of a expression. An preprocessing and segmentation algorithms are used for
imshow() is a function which displays the image to the screen. comparison.
Figure shows different image windows for colour and gray
image to display.[7]
REFERENCES
2. Observation and Discussion
For experimentation a simple program is considered i.e. the [1] http://whatis.techtarget.com/definition/0,,sid9_gci1169848,00.html
program is of converting a coloured image into gray-scale [2] http://whatis.techtarget.com/definition/0,,sid9_gci1169848,00.html
image. The comparisons of these four technologies are shown [3] www.oreilly.com/openbook/javawt/book/ch12.pdf
in table 1. [4] Gary Bradski, Adrian Kaehler, Learning OpenCV – Computer Vision
with OpenCV Library, O’reilly publication, June-2011.
Table. 1: Comparison of Different Technologies [5] Wilhem Burger, Mark Burge, Digital Image Processing: An algorithmic
introduction using Java, Springer, 2008.
OpenCV Java Matlab [6] http://www.engineersgarage.com/articles/image-processing-tutorial-
applications
Features
Open source or not Yes No No [7] Rafael Gonzalez, Richard Woods, Steven Eddins, Digital Image
Processing using Matlab, 2nd edition, Tata McGraw Hill publication,
2010.
Coding Slower Slower Faster

Length of code More More Less

Debug More More Medium/Less

Execution Faster Slower Slower

Maintenance More More Medium/ Less

Image Processing Library API for IP IP Toolbox


done through
Suitable for Real-time Real time Experimental
applications and offline environment
applications

View publication stats

You might also like