Intensity transformations

Try imadjdemo. It demostrates essential intensity transformations

An intensity transformation just replaces original intensities by some others according to some transformation table, I_new=f(I). It can be formalized as:
for i=1 to image_width
  for j=1 to image_height 
    I_new(i,j) = f(I(i,j))
  end
end

Negative

Let us make the negative of an image. The transformation table becomes
original value 0123...254255
new value 255254253252...10
We create the transformation function f as look-up table

f=255:-1:0;
A naive Matlab implementation may look as
NEG=zeros(size(IM)); % memory allocation
tic
for i=1:size(IM,2)
   for j=1:size(IM,1)
      NEG(j,i)=f(IM(j,i)+1);
   end
end
toc
You may wonder why to index IM(j,i)+1. This is because Matlab cannot index from "0" but "1".

The pair tic a toc measures the execution time. However, the implementation above is far from Matlab-optimal. The task is very suitable for code vectorization. We may rewrite the two nested loops as:

NEG=f(IM+1);
Elegant, isn't it? And faster, much faster! You may check it by the tic-toc pair.

More intensity transforms

Try few more intensity transforms.

Thresholding

Thresholding, sometimes called binarization, replace intensities which are lower than a predefined threshold T by 0 and assign 1 to others. Prepare transformation tables for T=20, T=128, T=250.

Piecewise linear transformation

Create a function lut=piecewise(a,b,c,d) which creates a transformation table according to the following graph:

Output of the function is the lut vector for desired transformation.