Oxaric’s Blog

A compendium of amazing things…

Posts Tagged ‘imaging’

Deinterlacing Images

Posted by oxaric on November 29, 2008




Today I worked with Matlab to find the best way to deinterlace a grayscale image. I used the famous Lena picture, figure 1, and set all of the odd-numbered lines to black, figure 2. My goal was to find the best way to replace the black lines so that the final image resembled the original as closely as possible.


You can view the algorithms I used and their resulting peak signal-to-noise ratio, PSNR, in table 1. I have not written down specific instructions for each algorithm but if you have questions about the algorithms feel free to contact me. The equation for finding the PSNR can be found on Wikipedia here.


Before testing any algorithms I wanted to see what I could expect for the PSNR boundaries. If you calculate the PSNR of an image to itself you will get infinity because there is only signal and no noise. However, once you change an image even slightly the PSNR is almost guaranteed to drop below 100. I found that if I changed one pixel of the original image by adding 1 to it’s color value the PSNR dropped to 96.293. And after replacing the odd-numbered lines in the original image with black lines I found the PSNR was 10.187. This let me know I could not expect a PSNR above 100 or below 10. Always a good thing to start out knowing your practical boundaries.


After trying out the 11 algorithms I found the max PSNR I could get was 34.031. This was accomplished using what I call the ‘direct averaging algorithm’. The results of this algorithm can be seen in figure 3.


Direct Averaging Algorithm

  • Start at the beginning of an odd-numbered line.
  • Choose a black pixel.
  • Take the color values of the pixels directly above and below this black pixel.
  • Average the two color values.
  • Replace the black pixel’s color value with this averaged value.
  • Repeat for every black pixel on an odd-numbered line.
Average Algorithm Example



For comparison, the results of another algorithm I call the ‘direct biggest algorithm’ can be seen in figure 4. The direct biggest algorithm gives a PSNR of 29.368 and is similar to the direct averaging algorithm but instead of using the averaged color value the biggest of the two color values is used to replace the black pixel.


The direct averaging algorithm was one of the first I tried and I was happy at the time. But I am slightly disappointed I did not achieve a higher PSNR. I feel there must be a way to to get a higher PSNR and I’m frustrated I haven’t thought of how. But the only reason I think there must be a better algorithm is because the algorithm I described seems too simple. I searched a litle on google to see if I could find a way to calculate a realistic practical optimum but I didn’t find anything. So for now I just have to live with a feeling that I can do better. That’s not such a bad thing though. :)


If you have any ideas how to achieve a higher PSNR I’d like to hear them!






Figure 1 – Original Image
Lena - Black & White


Figure 2 – Original Image Missing Odd-Numbered Lines
Lena - Missing Odd Lines


Figure 3 – Resulting Image of the Direct Average Algorithm
Direct Average Algorithm


Figure 4 – Resulting Image of the Direct Highest Algorithm
Direct Highest Algorithm






Table 1 – Algorithms and Resulting PSNRs (higher is better)


Original Image With Blacked Out Lines
PSNR = 10.1865


Direct Averaging Algorithm
PSNR = 34.0313


Hybrid1 – Threshold = 10
If the difference between the top and bottom pixels is less than the threshold choose the highest color, otherwise blend colors.
PSNR = 33.7630


Take Median of 3 Pixels Above and 3 Below
PSNR = 33.3099


Average 3 Pixels Above and 3 Below
PSNR = 31.7530


Take All Even Lines As One Image, Resize To Original Size
PSNR = 31.0262


Hybrid2 – Threshold = 10
If the difference between the top and bottom pixels is less than the threshold blend colors, otherwise choose highest color.
PSNR = 29.4632


Take Highest From Above and Below
PSNR = 29.3676


Take Lowest From Above and Below
PSNR = 29.3251


Replace Odd Lines With Even Lines
PSNR = 29.2886


Look At Bottom 3 Pixels, Find Closest to Top Pixel, Average Pixels
PSNR = 26.5942


Take Highest From 3 Above and 3 Below
PSNR = 23.5104

Posted in Imaging, Programming | Tagged: , , , , , , , , , , , | Leave a Comment »

Bitmap Imaging

Posted by oxaric on November 20, 2008

I’m currently working on some code to detect text in images and it reminded me of an old project I created.


The program loads a 24-bit .bmp file and has code for a lot of different filters and transformations. Things like sun flares, swapping color levels, blurring, increasing color saturation, determining the number of colors in a picture and limiting the picture to a certain number of colors, and other things such as creating circles, lines, and squares.


At the time I had seen many different filters but I never knew about the algorithms to create these filters. So through trial and error I discovered ways to create the effects I wanted. It was a fun project and I’m glad I spent the time learning about imaging.


It’s not a finished project by any means and it doesn’t have very good commenting but it does has some neat features someone might want to look into. It has some tools that are useful such as bit manipulation and the bitmap loader.


I’m putting it up here more for posterity than anything else. I make no guarantees about how good it is. :)


Click to directly download ‘Imaging Program.zip’


Posted in C/C++, Programming | Tagged: , , , , , , , , | Leave a Comment »