i.e. some stuff and junk about Python, Perl, Matlab, Ruby, Mac X, Linux, Solaris, ...
Monday, August 27, 2007
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
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
Sunday, June 17, 2007
Ruby: Tk, select 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
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 $FDThe 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
Saturday, June 09, 2007
Ruby: reading Microsoft/OpenOffice spreadsheets

def readExcelToHash f
oo = Excel.new(f)
oo.default_sheet = 1
xcl=Hash.new
0.upto(oo.last_row) { |line|
r=oo.row(line)
next if r[0].to_s.length==0
rs= String.new r[0].to_s
a=String.new
rs.each_byte{|x| a+= x.chr if x!=0} #(1)
next if rs =~ /^#/
xcl[a.gsub(/\.tif+$/,'')]=r
}
xcl
endAs far as creating spreadsheets is concerned, roo does not provide this functionality.
Monday, June 04, 2007
Noise and blur effect on the image

P.S
Above Fourier transforms were calculated using free ImageJ software.
Saturday, June 02, 2007
Ruby: Plotting with gnuplot in Mac X
sudo gem install ./gnuplot-2.2.gemIf you have gnuplot already installed it is good, if not install it by
sudo port install gnuplot.Assuming that everything went well, one can now plot and save figures using Ruby.
Simple example of Ruby code that plots simple data and saves (do not display) it to png file.
#!/usr/bin/env ruby
#testGnuplot.rb
require 'rubygems'
require 'gnuplot'
outFname='graph.png'
xData=[1, 2, 3, 4,5]
yData=[2, 3 ,4, 2,3]
#xData=(0..10).collect { |v| v.to_f }
#yData= xData.collect { |v| v ** 2 }
Gnuplot.open do |gp|
Gnuplot::Plot.new( gp ) do |plot|
plot.output outFname
plot.terminal 'png'
plot.title "Array Plot Example"
plot.ylabel "y"
plot.xlabel "x"
x= xData
y= yData
plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds|
ds.with = "linespoints"
ds.notitle
end
end
end
This is especially useful when one want to plot a lot of figures contained in a data file. Of corese, this is the simples example, and more complicated things require to do some experimenting before desired plots are obtained. Nonetheless, this is good start.
To plot two data on one graph and to save it as png one can do as below:
#!/usr/bin/env ruby
#test.rb
require 'rubygems'
require 'gnuplot'
outFname='graph.png'
xData=[1, 2, 3, 4,5]
yData=[2, 3 ,4, 2,3]
yData2=[1, 2 ,3, 3,4]
Gnuplot.open do |gp|
Gnuplot::Plot.new( gp ) do |plot|
plot.output outFname
plot.terminal 'png'
plot.title "Array Plot Example"
plot.ylabel "y"
plot.xlabel "x"
x= xData
y= yData
plot.data =[ Gnuplot::DataSet.new( [x, y] ) do |ds|
ds.with = "linespoints"
ds.notitle
end,
Gnuplot::DataSet.new( [xData, yData2] ) do |ds|
ds.with = "linespoints"
ds.notitle
end
]
end
end
Results is (graph.png):
