Tuesday, July 8, 2008

Convert a series of Matlab figure files in to jpg

This program converts a set of fig files located in a directory in to jpg files. The program runs through the directory and gets a list of fig file names. These files are then read using the ‘openfig’ command and the output is written using ‘saveas’ command. For example, a file named ‘image.fig’ will be read and saved as ‘image.fig.jpg’. The only variable that needs to be changed is the figdirectory

Bonus: You can use the parsing directory structure to read any kind of files located in a directory.

%%Matlab program - fig2jpg
figdirectory = 'C:\Documents and Settings\username\Desktop'

fullpath = sprintf('%s/*.fig',figdirectory)
d = dir(fullpath);
length_d = length(d)
if(length_d == 0)
disp('couldnt read the directory details\n');
disp('check if your files are in correct directory\n');

startfig = 1
endfig = length_d

for i = startfig:endfig
fname = d(i).name;
fname_input = sprintf('%s/%s',figdirectory,fname)
fname_output = sprintf('%s/%s.jpg',figdirectory,fname)

Tuesday, May 13, 2008

Setting automatic jobs in Linux

I was working on setting up a cron job for monitoring the machines under my control and so the natural thing was to set a cron job in Linux. I was new to crontab command in Linux but as you will soon see, the command is very simple and hence should be easy to master. There are some nuances that I learnt during the course of the work and I will discuss it at the end.

The crontab command begins with setting the time, date at which a particular job needs to be run. These parameters are set using a crontab file, which can be edited by calling the command crontab -l. In the editor, type the following command

5 * * * * command-to-execute > logfile.log

The number 5 represents the minute at which the command has to be executed. The subsequent values represents the hour, day, month and day of the week (Sunday = 0) respectively. The value * represents all the possible combination. The > pipes the output of the command to a logfile. Once the crontab file has been set, it can be verified using crontab -l. The crontab file can also be written to a text file and loaded using crontab cronfile.txt. The content of the cronfile.txt is same as above.

If you wish that you receive a mail every time the cron runs, add the following line first

MAILTO: johndoe@email.com (Of course, replace it with the correct email address)

So far, everything is easy and good but be careful with the following,

1.Ensure that there is an empty line after the last line of crontab file.
2.For some reason, crontab cannot use the PATH stored by Linux. So ensure that all commands in the crontab file and all the commands and scripts called by crontab are referred with full path.

So instead of calling
* 2 * * * python /path/to/file/pythonfile.py


* 2 * * * /usr/bin/python /path/to/file/pythonfile.py

Also make sure that all the path in the python file are also referred using full path.

Wednesday, April 9, 2008

Optical Character Recoginition (OCR) using Tesseract

Recently I had a very interesting project from client who wanted to extract the text (in my case numbers) embedded on thousands of images. My natural choice was to use Tesseract, as it can be scripted and applied to many images in sequenence.

Tesseract is an OCR software, originally developed by Hewlett Packard and currently developed by Google. It is a open source software released under Apache license. Since it is open source, you can get your hands on it and install it on pretty much any operating system. I installed it on a Windows machine under Cygwin and the installation was a breeze.

Tesseract does not have any segmentation methods, no document layout and can only output the recognised text to a file. But its accuracy is good enough for many applications. It was ranked among top 3rd OCR software for the year 1995. Making a call to tesseract is also easy

tesseract data.bmp text.dat [-l langid]

The values within [] are optional. The langid is the the language being recognized. The default language is English. But it also supports French, Italian, German, Spanish.

Since Tesseract does not have any segmentation methods to separate the text from background, the user have to apply these methods using other softwares like ImageMagick, ITK etc. The most common segmentation technique for scanned documents is the Local Adaptive Thresholding. It takes in to account the variation in background intensity across the scanned image and thresholds accordingly. But the right technique has to be chosen depending on the type of image being recognized.

Possible additions to Tesseract
All the text that I needed to decipher in my images were numbers but Tesseract does not have a langid for numbers. Since there are no langid for numbers, Tesseract deciphered some of these numbers as alphabets. If I have time in the future, I will work on creating the langid for numbers as it will be helpful for many people. If you find that it might be helpful for you, I encourage you to create one or contact me and we can work togther.

Thursday, March 27, 2008

Summary of all chapters

Currently I am busy with my other projects and I am not able to continue working on chapters on Draw and Pixel class and also on other useful small projects that you could perform. So meanwhile, I am uploading a file which has all my writing thus far. Enjoy reading.

Magick++ chapter 1 to 6 - Get more free documents

Monday, March 24, 2008

Chapter 6: Image class – Part 2

In chapter 4, we looked at the overview of the Image class and discussed a few functions, for filtering images, in detail using some example program. In this chapter, we will discuss a higher order image processing function like segmentation. We will first demonstrate a program for performing adaptive thresholding and then a program for performing fuzzy c mean clustering. Each of these algorithms will be explained and then the program will be illustrated to ensure complete understanding.

Continue reading the pdf file ...

Chapter 6: Image class – Part 2 - Get more free documents

Friday, March 14, 2008

Chapter 5: Exception class

In this chapter, we will focus on the Exception class, to identify errors in the Magick++ function calls at run time, ensuring easier debugging of code. We will compliment it with example program that will aid in the understanding of its use.

Continue reading the pdf below...

Chapter 5: Exception class - Get more free documents

Sunday, March 9, 2008

Chapter 4: Image class – Part 1

In this previous chapter, we looked at the overview of the various classes available in Magick++. In this chapter, we will focus on the Image class, the base class and learn its features and some of it functionalities. We will compliment these with example programs that will aid in the understanding of its use.

Image class

As indicated earlier, Image class is the main class in Magick++. It can
1.Read and Write almost all common formats including some field domain specific formats like dicom
2.Create new images
3.Perform image filtering1
4.Perform geometric image transformation
5.Improve quality of photographs
6.Perform image modification like slicing, bordering etc
8.Set image attributes
9.and more such features which could not arranged in to this list

Continue reading the pdf below...

Chapter 4: Image class – Part 1 - Get more free documents

Sunday, March 2, 2008

Chapter 3: Introduction to Magick++ classes

In this chapter, we will look at an overview of the various classes available in Magick++. We will list the various classes and a brief description of their function and then a detailed look at how these classes work together in-order to create a good programming practise.

Magick++ classes
There are a total of 11 classes in Magick++. The major class is Image as it can read, write and create images. Other classes like Color, Geometry etc are used to support the functionalities in Image class and other classes. In the following table, the various classes are arranged roughly in the order of importance...

Continue reading the pdf file below...

Chapter 3: Introduction to Magick++ classes - Get more free documents

Sunday, February 24, 2008

Chapter 2: Compiling Magick++ program

In this chapter, we will look at a demonstration program to understand the method for compiling and linking a Magick++ program. Read the pdf file located below ...

Chapter 2: Compiling Magick++ program - Get more free documents

Wednesday, February 20, 2008

Chapter 1: Introduction to Magick++

I have been preparing programs that demonstrate the various capabilities of Magick++. I am planning to prepare them as a set of articles (around 10) that would demonstrate how Magick++ could be integrated in to your C++ program, thus providing an immense power in manipulating images.

In this tutorial, I would introduce some of the basics of Magick++ and in the next tutorial, we would work on the various classes that are defined in Magick++. Subsequently, we would prepare simple programs and with advanced features appearing as we progress.

Continue reading the pdf file below...

Introduction to Magick++ - Get more free documents

Monday, February 4, 2008

Reviewing ImageMagick

ImageMagick is a software suite for reading, writing and processing images. Over the years various functionalities have been added to the suite and the current version can read and write almost all common formats and also many not so common formats. It can also process these images using its various transformation, filtering etc. It is a free software and is available for all common OS.

It is so powerful that many other desktop applications and some online applications like flickr.com have been developed around it. So whether you are looking for a simple image conversion or you want to program your website to serve users image processing needs, ImageMagick can handle it for you.

Why ImageMagick?
1. If the various operations in image processing can be classified in to input, processing and output, ImageMagick provides functionalities that perform all these three operations.
2. ImageMagick provides a set of libraries that allow reading and writing about 100 different formats. So that the user can concentrate more on the image processing than on the file input and output (I/O).
3. ImageMagick can be used in command line in almost all the common operating systems like Windows, Linux, UNIX and Mac etc by combining the read, write and image processing operations.

Other features:
1. Read and write files from over 100 different file formats. These include the commonly used formats like GIF, JPEG, PNG and TIFF etc. Refer http://www.imagemagick.com/www/formats.html for complete list.
2. Modify shape, size and orientation of the images
3. Add different effects like blur, emboss, swirl etc
4. Create GIF and movie sequences
5. Draw vector graphics on the images
6. Perform image composition
7. Decorate an image with a border or frame
8. Combine all these operations with different programming languages using APIs to create powerful image processing functionality

Why ImageMagick APIs?
1. Command line tools do not lend themselves to large programs.
2. The number of file formats is finite and definitely only a few of them are used universally. But the number of image processing operations is infinite as the individual operations can be mixed and matched. Hence, the programmer needs flexibility in creating their own image processing routines. ImageMagick fills this gap by providing APIs.
3. APIs also allow access to the individual pixels in an image and hence can perform low level image processing operations as well.
4. Two people are not alike and definitely two programmers are not alike. So, ImageMagick provides a multiple choice for programming environment. A Java developer need not be constrained to use C++ for his image processing operations.
5. This also allows ImageMagick to be used in different environment,
a. Desktop applications using C, C++, LabView, Pascal and Tcl/Tk
b. Internet based applications using COM+, Java, .NET, PHP, Python and Ruby

This listing demonstrates the range of applications that can be developed using ImageMagick and does not in any way limit the imagination of the programmer. PHP can also be used to write desktop application while C++ can be used to write command line operations which in turn can be used in an internet based application.

Please check this and this website for some examples on ImageMagick. Also check this website for introductory tutorials on various ImageMagick API.