Saving JP2 images

August 11, 2006

After four days of dealing with continous errors in save routine for jp2 files, I finally got something fruitful. Initially I was getting errors from glibc which was giving me trouble using malloc(). Turns out I had not freed some memory and doing that fixed that error. Then came a run-time seg fault which was annoying. It took me a while to notice that in a for loop I had for(k =0;i< comps;i++) whereas it should have been for(k=0;k<comps;k++). i was uninitialized so the program basically skipped the step and a opj_create_image routine did not get the proper values.

After finding these stupid errors came the errors from the openjpeg library. Finding this error made me understand how the library is written, so it may have been a blessing in disguise! The problem was in writing header data into a stream. After a lot of debugging, I noticed the actual problem was initializing the data buffer in the stream. This led me to realize that I was actually not setting the width and height in the openjpeg image data structure. I assumed this done during initializing because I provided this data for each individual component. After doing this the program ran smoothly and saved the image. But now the problem was that the saved image was all black. The reason was some uinitialized values in the compression parameters. After figuring out the right values the image was finally saved correctly.

There are still some improvement that are needed though. First I need an interactive way of changing paramters for compression. Secondly, I need to test it with some standard images provided on the openjpeg discussion forum. That should complete this plugin. I would need to work a little harder to complete all this within two days as I have until August 21 to do this work.

I also need to make changes in the inverse halftoning plugin to put a graphical interface so that halftoned images of various frequencies can be reversed. The denoising plugin I believe is complete and works well.

After all this I need to make sure I have proper copyrights in the files and also write a final report.  Hopefully things will be done in time.


Loading JP2 and J2K images works!

July 31, 2006

I was finally able to find the error in my jpeg2000 plugin code. The reason for the error was that I was not reading the filename properly (basically reading NULL) and because of this I was getting segmentation faults when it tried to load images. I always assumed that the run function would automatically have the filename and never bothered to check this!  I solved the problem by providing the necessary arguments in the query function.

The good thing is that the load plugin is working smoothly on the select images that I tested it on. It may be a little slow but I can definitely speed it up using row processing. I used the openjpeg library although I could have used the jasper library as well because the problem I was getting was common. But the openjpeg library is specifically geared towards jpeg2000 development and the library functions are a lot more easier to use that the jasper library. The final code is also very clean.

A screenshot of how the image was loaded is shown below.


I will be out attending a family function for the next two days and will start working on refining the current load code and then work on saving as jp2 or j2k images.

Inverse Halftoning – contd

July 28, 2006

This update comes a little late as I was busy with working on the jpeg2000 plug-in and forgot to update on my final status on inverse halftoning. I decided on the algorithm to use, implemented it in C, and finally wrote the plug-in which seems to be working well with exception on images of the order 1024 and higher. My guess is that I am not doing memory management well and hopefully I can resolve this issue soon. I started working on jpeg2000 plugin in the meanwhile as I was getting bored of working on halftoning for a while!

Here are some scanned newspaper images on which I tried the algorithm using the plug-in.

news_1 halftoned newspaper image

news_1_ihalf inverse halftoned image

news_2   halftoned newspaper image

news_2_ihalf  inverse halftoned image

The basic algorithm goes as follows:

If we take a one step wavelet transform of a halftoned image, the low pass frequency part will be less noisy whereas the high frequency part will be extremely noisy as it detects many edges in the halftoned images.  The basic idea is to remove the noise in these images and preserve the edges.  In order to preserve the edges, I get the high frequency content from blurred versions of the image.  This way the noise gets reduced and I get  the useful edge information.  I get the blurred image by using 3×3 kernel.  If we increase the radius of the kernel the final image become very blurry.  It can be argued that is process is almost similar to simply blurring the image, but in actuallity there is a slight difference.  I am using the high frequency content from blurred images and low frequency from the original halftoned image.  Further research can be done to improve the sharpness of the images, but for now this method works and I will concentrate on making it memory efficient.  Once I get that working, I will work on further increasing the quality of inverse halftoned images.  Another update on my jpeg2000 work will soon folllow…

Inverse Halftoning – problems

July 16, 2006

Inverse Halftoning is tough!!! Yeah, thats my final conclusion. No I have not given up..I am just frustrated that things are not working properly. I pretty much explained the steps involved in a halftoning process using wavelets in my last post. I realized later that I should not pass the ‘S’ component through a gaussian filter as this makes the final image more blurrier. After all the whole point of the process is to recover edges from the halftoned image.

After recovering them, the more difficult part is to blend it with the image. One paper suggested passing horizontal and vertical edges (from the DWT) through a low pass filter. This will reduce the noise and highlight the main edges. Well it does highlight the main edges but also highlights a lot of extra stuff. Supposedly (as suggested in another paper), this does not happen in error-diffused halftoned images. Moreover, the method of low pass filtering only works well when the halftoned images is error-diffused. A real bummer becuase the authors of the paper claimed the algorithm worked for any kind of halftoned image and that is what got me to implement the algorithm. Anyways, I have got a pretty good grasp of what is going on and should be able to come up with a general algorithm.

Its getting late now.  My plan for tomorrow is to test the algorithm on a newspaper image that I have scanned.  That should be interested because its a more practical situation.

Inverse Halftoning

July 14, 2006

For the past week I had been working on implementing a Rice University based inverse halftoning algorithm which they had published in Matlab. The results were good and I was motivated to implement the same in C. This apparently was a bad decision because of the various complicated steps involved. My aim was to start with the denoising implementation and use the dwt code provided in the same toolkit. But this did not work and I got some random images which was definitly not what I had in mind for the final image. I realized that I should have carefully tested and understood the halftoning process.

The work done at Rice University was based on the original work done by Xiong, Orchard, and Ramchandran in a paper titled “Inverse Halftoning Using Wavelets”. This was the first paper of doing inverse halftoning using wavelets and most of the subsiquent research papers used some form of the the algorithm proposed by them. Unfortunately there is no implementation of the algorithm available online. Fortunately the method is easy enough to be implemented in Matlab. My goal was to first test how the algorithm works and then implement in C. So far I have results of how the code works on a Matlab implementation.

Here are the steps involved in the method:

Step 1: Take DWT of the halftoned image. This will produce four set of images say S, W_H, W_V, W_D. S will contain low frequency information of the image, W_H will be the horizontal high pass image, W_V will be the vertical high pass image, and W_D will be the diagonal high pass image.

Step 2: Pass W_H and W_V through a low pass gaussian filter and ignore W_D.

Step 3: Pass S through an edge preserving filter so as to not get blurry images. Instead of doing this, I also passed S through a gaussian filter. The result as expected was blurry but the amount was not that much. I will implement the particular noise removal filter soon.

Step 4: Calculate inverse DWT using changed wavelets coefficients and ignoring W_D.

The sequence of images starting from the orignial are shown below:

halftoned image1 original image

pepper_s S wavelet image

pepper_h W_H wavelet image

pepper_v W_V wavelet image

Passing each W_H, W_V, S through gaussian filtering and taking inverse we get:


final inverse halftoned image

If you compare this with the image I got in my previous post, you will notice a huge difference in the quality. I will improve upon this by changing the filter used for S wavelet coefficients. Once I have the Matlab code ready and I will implement this in C and then make it work for GIMP. This should take about 2-3 days because most of the code for doing Wavelet transforms is already available and I just have to get code for doing filtering (which I have actually written but realized there is a better and faster version online!) and specify the necessary kernel for doing filtering. I should have the final plugin done within 3 days.  I will do testing of the Matlab code on some pictures that I have scanned from newspapers.  That should give a good idea of how this method can actually be used becuase newspaper images are halftoned images.

Starting Halftoning

July 7, 2006

Having done image denoising I have now started implementing inverse image halftoning. This might get a little tricky because I do not have a C implementation and just some Matlab. But I think I can use most of the image denoising code becuase the concepts used are almost the same. While I was reading this paper on image halftoning using wavelets they mentioned that the approach was similar to that of image denoising. Since halftoned data is basically a very noisy but elegant looking image, I decided to try the current denoising plug-in on a halftoned image. The results, as expected, were not great but showed that the concepts could be used with some slight modification. I will be working on that over the next couple of days. Here are the results that I got on using the denoising plug-in:

halftoned image1

halftoned image


image obtained after denoising

Image Denoising – contd

July 4, 2006

Today I have finally completed the image denoising plug-in which I started writing last week. Previously the code could only handle grayscale images and that too of specific resolution (power of 2). Now the implementation is generalized and gave pretty good results on testing under various levels of noise. The main problem I faced during this time was in understanding how the gimp processed colored images using a single array. I had the concept right, but was reading images by the Matlab format which reverses the x and y coordinates. I didn’t realize what I was doing wrong until I asked Simon (my mentor) about it.

Once I fixed that, I was getting results but they did not seem satisfactory for images of dimension say 240×260. I stored this image in a 512×512 array and set the necessary values to the image pixels and set the rest of the values to 0. As you can see, a large part of the image will be 0 and thus the overal image noise becomes small. For this reason it was not denoising the image properly. The solution was to extend the image in each direction to create 2*(240*260) image and read the values of 512×512 array accordingly. This way the noise level remains the same and the denoising is done successfully. Some examples of how the denoising is performing are shown here.

Noisy Image 2 noisy image
Noisy Image 2 denoised image

Blurred 2 blurred image
Noisy Image noisy image 2
Denoised Image denoised image 2

There is a lot of noise in the first image and the corresponding denoised is not that good but this is understandable considering the amount of information lost due to noise.  Notice what happens the the first noisy image is blurred.  The noise is gone but the image is severally blurred which is not good.  Clearly this denoising method is superior and restores the basic image even if the noise is a lot.  The noise in the second image is not that much and hence the denoised image looks much better.

Image Denoising

June 29, 2006

I have been rather lazy about blogging about the work I am doing, but today I was pretty excited about the work done so thought of putting them online as well :). I started out writing a jpeg2000 load plug-in but was experiencing some problems using an external library. It was getting frustrating so I thought I would suspend that work and concentrate on other goals that I had in mind.

I have got a working denoising plug-in which is currently a little selective in its functionality, but works well. I will be working on making it more flexiable during the next couple of days. The results are shown below. Denoising basically attempts to remove noise from an image (here the noisy cats image). One way to remove noise is to blur the image, but this is not very effective as it removes high frequency components in an image.

Cats with noise

noisy image
Blurred cats image

blurred image

Denoising cats image

denoised image
original cats image

original image

The current implementation tries to restore high frequency and the results look better than the blurred images. More testing will be done to further confirm this. In the next couple of days I will be working on generalizing the implementation. It currently only works on sqaured images with dimension of the form 2^m by 2^m. There are memory allocation issues the size is not of this form. Also, the implementation currently uses Daubechies wavelet of length 8. This can be changed for better results. Additionally, I will also work on improving the speed of the code. All this should not take more than 2 days.

Compiling GIMP from source

May 29, 2006

I was confused when I did not notice any options (quality, blocksize etc) for converting to jpeg2000 but on digging out the manual I found a whole bunch of options to specify.  These were also defined in the jp2.c file provided by jasper, but I guess I overlooked it.  So I know what controlling parameters to put in the final interface.

I also got myself the source code of GIMP 2.2 and compiled it.  It took a while to get all the necessary libraries, but it worked out well in the end.  I looked at the jpeg.c plug-in and will use this as a starting point for the jp2 plug-in.  I have got everything I needed and should be able to get something useful working by tomorrow.

Schedule for the next couple of days:

  1. Write code using jasper libraries to read jp2 files.
  2. Learn how to write plug-ins and also how to add the jp2 extensions to the gimp interface.
  3. Write code for saving images as jp2 files

Hopefully I should be abe to these by Wednesday.  My parents have planned a 2 week vacation to visit several places followed by a week of stay at my grandparents place starting this Thursday.  I may not get much coding done during this time, but will catch up on reading for other wavelet applications.  The good thing is that I don't have any more plans to go out after that so I can spend all my time ont his project

The start of summer

May 26, 2006

Well my summer as officially started and will mainly go by in implementing various wavelet based applications for GIMP. This what this blog is about and I will mainly discuss various things I am implementing. A rough proposal for the same can be found here.

I have already started working on making a jpeg2000 plug-in using libraries provided by the Jasper project. So far I have installed vmware on my windows and got an ubuntu system working for the necessary development. I have installed the jasper libraries and have gone over the jpeg2000 paper that they have provided. I tried using the code on some test images and it seems to be working fine. One thing which bugs me is that on convering an image to jpeg2000, there are no parameters that I can input. I was guessing there would such an option depending on how much wavelet coefficient to choose. I may have to go over the manual to see what is going on there. Other than that the libraries seem to be well written and documented so I should not have trouble using them. After doing some more reading today, tomorrow I will look more closely at the given code and hopefully get something useful out of it so that. Then I will start learning how to write a GIMP plug-in and then things should move along well. I am really excited on learning on how to write plug-in as its been a while since I have done some hardcore C programming. The past year has been all Matlab realted stuff. Well I am officially an engineer now, so theoretically I should not have problems..or will I!!