Sunday, December 02, 2007

Python Imaging Library - not working with 16 bit images

Python Imaging Library works well with 8-bit images, however it has problem with 16-bit images. As an example I wrote a script that flops tiff images horizontally. One image is 8-bit and the second image is 16-bit:



#!/usr/bin/env python
'''
Convert tif images from Ludvig 2008.

This scripts take all tifs in input dir, and changes
tif files into tiff. Additionali it takes
right knee x-rays and flips them horizontally,
to have all x-ray in the same format.

'''
#from myUtil import *
import re
import os
import Image
import Tkinter, tkFileDialog, os.path
from scipy import *

def flip_horizontally(inDir,inFile,outFile=None):
imgpath=inDir+inFile
im = Image.open(imgpath)
out = im.transpose(Image.FLIP_LEFT_RIGHT)
if outFile is None: outFile=inFile
base=os.path.splitext(outFile)[0]
out.save(inDir+base+'_flopped.tiff')

def main():
inDir=myGetDir('./')
print inDir
files=os.listdir(inDir)
#change_file_ext(inDir,r'\.tif+$','.tiff')
for f in files:
if f.find('tiff')==-1: continue
if f.find('test')==-1: continue
print f
flip_horizontally(inDir,f,f)

def myGetDir(indir='./',putTitle='Select dir'):
"""Get one dir name"""
root = Tkinter.Tk()
root.withdraw()
dirr=tkFileDialog.askdirectory(initialdir=indir,
title=putTitle)
if len(dirr)==0: exit(1)
return dirr+'/'

if __name__ == '__main__':
main()

The result of the above code is:




It is clearly seen that flopping 16-bit image gives wrong results.

Friday, November 16, 2007

Matlab: Easter egg - spy

In Matlab, spy(S) function plots the sparsity pattern of the matrix S. But if you run spy without any arguments you get:
Interesting, isn't it:-)

Monday, August 27, 2007

Programming golden formula

"Make It Work, Make It Right, Make It Fast"
More is here.

Monday, July 23, 2007

PSNR (Peak Signal-to-Noise Ratio)

Compressing an image is significantly different than compressing raw binary data. Of course, general purpose compression programs can be used to compress images, but the result is less than optimal. This is because images have certain statistical properties which can be exploited by encoders specifically designed for them. Also, some of the finer details in the image can be sacrificed for the sake of saving a little more bandwidth or storage space. This also means that lossy compression techniques can be used in this area.

Lossless compression involves with compressing data which, when decompressed, will be an exact replica of the original data. This is the case when binary data such as executables, documents etc. are compressed. They need to be exactly reproduced when decompressed. On the other hand, images (and music too) need not be reproduced 'exactly'. An approximation of the original image is enough for most purposes, as long as the error between the original and the compressed image is tolerable.

Error Metrics

Two of the error metrics used to compare the various image compression techniques are the Mean Square Error (MSE) and the Peak Signal to Noise Ratio (PSNR). The MSE is the cumulative squared error between the compressed and the original image, whereas PSNR is a measure of the peak error. The mathematical formulas for the two are

MSE =

PSNR = 20 * log10 (255 / sqrt(MSE))

where I(x,y) is the original image, I'(x,y) is the approximated version (which is actually the decompressed image) and M,N are the dimensions of the images. A lower value for MSE means lesser error, and as seen from the inverse relation between the MSE and PSNR, this translates to a high value of PSNR. Logically, a higher value of PSNR is good because it means that the ratio of Signal to Noise is higher. Here, the 'signal' is the original image, and the 'noise' is the error in reconstruction. So, if you find a compression scheme having a lower MSE (and a high PSNR), you can recognize that it is a better one.

Monday, June 25, 2007

Matlab: Mandelbrot set

Recently I read article on Free Software Magazine titled Generating cool fractals. I was very pleased to find source code for generation of Mandelbrot set.
function mandelFrac
% MATLAB and Octave code to generate
%a Mandelbrot fractal

% Number of points in side of image and
% number of iterations in the Mandelbrot
% fractal calculation
npts=1000;
niter=51;
% Generating z = 0 (real and
% imaginary part)
zRe=zeros(npts,npts);
zIm=zeros(npts,npts);
% Generating the constant k (real and
% imaginary part)
kRe=repmat(linspace(-1.5,0.5,npts),npts,1);
kIm=repmat(linspace(-1,1,npts)',1,npts);

% Iterating
for j=1:niter
% Calculating q = z*z + k in complex space
% q is a temporary variable to store the result
qRe=zRe.*zRe-zIm.*zIm+kRe;
qIm=2.*zRe.*zIm+kIm;
% Assigning the q values to z constraining between
% -5 and 5 to avoid numerical divergences
zRe=qRe;
qgtfive= find(qRe > 5.);
zRe(qgtfive)=5.;
qltmfive=find(qRe<-5.);
zRe(qltmfive)=-5.;
zIm=qIm;
hgtfive=find(qIm>5.);
zIm(hgtfive)=5.;
hltmfive=find(qIm<-5.);
zIm(hltmfive)=-5.;
end

% Lines below this one are commented out when making
% the benchmark.

% Generating plot
% Generating the image to plot
ima=log( sqrt(zRe.*zRe+zIm.*zIm) + 1);
% Plotting the image
imagesc(ima);

Sunday, June 24, 2007

Lenne picture

The original photo of famous Lenna picture which is found in many papers and books conserning image processing:-)

Sunday, June 17, 2007

Ruby: Tk, select directory

Just quick code for selecting input/output directory#!/usr/bin/env ruby
require 'tk'
bdir=Tk.chooseDirectory('initialdir'=>'./')
puts bdir

Below an example that takes a directory and lists all txt files that contain string '_data' in their name. Than contents of each such file is copied to one output file ('allData.txt'):#!/usr/bin/env ruby
require 'tk'

bdir=Tk.chooseDirectory('initialdir'=>'./')

fout= open('allData.txt', 'w')

Dir.foreach(bdir) { |x|
next if x !~ /_data/
fpath=bdir+'/'+x
File.open(fpath).each_line {|l| fout << l}
}
fout.close

Thursday, June 14, 2007

Excel: Visual Basic; mean value

Simple code in Microsoft VBA as a function in Excel. This function takes a range (x) and calculates mean value of the number lower than 0.
Function mymeanLr(x)
n = x.Count
ReDim xx(n + 1)
Dim ind As Integer
ind = 0
For i = 1 To n
If x(i) < 0 Then
xx(i - 1) = x(i)
ind = ind + 1
End If
Next i
mymeanLr = WorksheetFunction.Average(xx)
End Function

Monday, June 11, 2007

bash: use sed and awk to calculate an awarage value in the file

#!/bin/bash
FD=`cat $1 | sed -e 's/^[0-9.]*//g' -e 's/^\t//g' -e 's/^M$//g' \
| awk '{for (i=1; i<=NF; i++) s=s+$i}; END{print s/9/12}'`
echo $1 $FD

The script calculates avarage of values in a txt without values from the first column.
Example txt file:0 2.7792 2.7876 2.8023 2.8248 2.8413 2.8551 2.8679 2.8593 2.8537
18.281 2.7619 2.7723 2.7864 2.7888 2.8105 2.8314 2.8697 2.8695 2.8615
26.719 2.749 2.7464 2.7622 2.7811 2.8145 2.8276 2.8359 2.8438 2.8482
45 2.7596 2.7601 2.7614 2.779 2.803 2.803 2.8207 2.8391 2.8374
63.281 2.778 2.7809 2.8013 2.8071 2.8408 2.8423 2.8478 2.8514 2.826
71.719 2.7871 2.7927 2.8171 2.819 2.8366 2.8581 2.876 2.8826 2.8648
90 2.8007 2.8038 2.8219 2.8371 2.851 2.8628 2.8683 2.879 2.8719
108.28 2.781 2.7835 2.7901 2.7982 2.8214 2.8314 2.8488 2.852 2.8416
116.72 2.7731 2.7749 2.7784 2.7852 2.8077 2.8155 2.8226 2.8274 2.832
135 2.7744 2.7826 2.7854 2.7998 2.8153 2.8154 2.8358 2.8627 2.8775
153.28 2.7866 2.8011 2.8079 2.8253 2.8517 2.862 2.8727 2.8728 2.8644
161.72 2.7906 2.8071 2.8278 2.8424 2.8674 2.8679 2.8709 2.8716 2.8635