Tuesday, April 24, 2012

JPEG VS JPEG 2000





What is JPEG?
  • Joint Photographic Experts Group (pronounced JAY-pehg).
  • An ISO/ITU standard for compressing still images
  • File extensions are .JPEG, .JFIF, .JPG, OR .JPE
  • Uses Discrete Cosine Transform (DCT) algorithm.
  • Lossy coding of continuous tone still images.
  • Very popular due to its variable compression range.
  • Best for compressing photographic images (compared to GIFs).
  • Mostly used for storing and transmitting photographs on the World Wide Web

Advantages of JPEG:
    • low complexity
    • memory efficient
    • reasonable coding efficiency
    • currently applicable for most applications
    • can be saved on a sliding resolution scale based on the quality desired

Disadvantages of JPEG:
    • not great for text - the text appears fuzzy, especially at low resolutions
    • lossy - some data or quality is lost when the image is compressed



What is JPEG 2000?
  • New format of JPEG formalised in December 2000.
  • File extensions : .jp2, .jpx, .jpf, .mj2
  • A revolutionary image compression standard based on wavelet technology and is replacing the old JPEG format.
  • Uses Discrete Wavelet Transform (DWT)
  • Scalable coding of continuous tone still images (from lossy to lossless).
  • Provides both lossless and lossy compression in a single compression architecture

Advantages of JPEG 2000:
    • Better image quality at the same file size
    • 25%–35% smaller file sizes at comparable image quality.
    • Good image quality even at very high compression ratios, over 80:1.
    • Lossless compression mode (identical to original image).

Disadvantages of JPEG 2000:
    • appropriate program or plugin is needed to view
    • more demanding in memory
    • consume higher computation time


JPEG VS JPEG 2000


JPEG
JPEG 2000
  • Created for natural imagery
  • Created for computer generated imagery
  • Discrete Cosine Transform (DCT) algorithm
  • Discrete Wavelet Transform (DWT) algorithm
  • File Extensions:
    • .jpeg
    • .jfif
    • .jpg
    • .jpe
  • File extention :
    • .jp2
    • .jpx
    • .jpf
    • .mj2
  • Currently applicable to most of  applications
  • Currently NOT widely used
Main Differences between JPEG and JPEG 2000




Tuesday, April 3, 2012

[Lab03] Intensity Transformation and Spatial Filter Part 2



Spatial Filtering

In the previous post, we have learned about intensity transformation. This post will be focus on another principal category for image processing – Spatial Filtering. Filter (or known as Mask) refers to “accepting” or “rejecting” a certain frequency components. These accepting or rejecting is known as smoothing or sharping.

Spatial Filtering is also known as neighbourhood processing. This is because we define a center point and apply a filter to only the neighbours of the center point. The result of the operation is one value which will become the new value of the center point in the modified image. In short, the process of spatial filtering simply consists of moving the mask from point to point in an image to derive the new value of the pixel.

Basic idea :
  • Size of neighbourhood = size of mask.
  • Mask slides from left to right, top to bottom.
  • Same operation is performed on every pixel. 
  • Neighbourhood exceeds image boundary = zero padding or replication of border pixel.  


Using myfilter.m

Let us try out spatial filtering using MATLAB. First, we need to create a MATLAB file (M-file) and save it as myfilter.m. Copy and paste the code below in the file. The MATLAB code below was written by Nova Scheidt:

% MYFILTER performs spatial correlation
% I = MYFILTER(f,w) produces an image that has undergone correlation
% f is the original image
% w is the filter (assumed to be 3x3)
% the original image is padded with 0's

function img = myfilter(f,w)
% check that the filter,w is 3x3
[m,n] = size(w);

if m~=3 || n~=3
   error ('Filter must be 3x3') 
end

% get size of the original image, f
[x,y] = size(f);

% create padded f (namely g)  
% filled with zeros
g = zeros (x+2, y+2);

% store f withing g
for i = 1:x
    for j = 1:y
        g(i+1, j+1) = f(i,j);  
    end 
end

% traverse the array and apply the filter
for i = 1:x
    for j = 1:y
        img(i,j) = g(i,j)*w(1,1) + g(i+1,j)*w(2,1) + g(i+2,j)*w(3,1)...
           + g(i,j+1)*w(1,2) + g(i+1,j+1)*w(2,2) + g(i+2,j+1)*w(3,2)...
           + g(i,j+2)*w(1,3) + g(i+1,j+2)*w(2,3) + g(i+2,j+2)*w(3,3);  
    end 
end

% convert to uint -- expected range is [0,1] when the image is displayed
img = uint8(img);
end

To apply this filter, we need to create an average filter (for this lab, we are using 3x3 matrix). The following code are used to do the filtering. By using the imtool, we can zoom out the picture for clearer result. The picture below have been zoomed 3000%.

% create average filter
>> w = [1/9, 1/9, 1/9; 1/9, 1/9, 1/9,; 1/9, 1/9, 1/9];

% applying the filter to an image
>> original_img = imread('stock_cut.jpg');
>> results = myfilter(original_img, w);
>> imtool(results)  




Using imfilter


Instead of using the M-file, we can use a function that comes as part of the Image Processing Toolkit. To use it, we need to call imfilter as below:

% create average filter
>> w = [1/9, 1/9, 1/9; 1/9, 1/9, 1/9,; 1/9, 1/9, 1/9];

% applying the filter to an image
>> original_img = imread('stock_cut.jpg');
>> results2 = imfilter(original_img, w);
>> imtool(results2)  



Using imfilter – with boundary options

The imfilter comes with a various boundary options. The following table shows the summary of additional options available (sources from Digital Image Processing, Using MATLAB, by Rafael C. Gonzalez, Richard E.Woods,and Steven L.Eddins).  

  

Since we are considering neighbourhood pixel, there are some cases where we there are no upper or lower neighbours or neighbours to the left or right. These are usually occurs when the mask covers the neighbourhood that exceeds the image boundary. For example, based on the image above, there are no upper neighbours or neighbours to the left. In order to solve this, we have two solutions- Zero padding and replicating.

Let us try the imfilter with different boundary options. The code below includes zero padding, replication, symmetric and circular. We are using 5 x 5 filter for clearer result.

% create a 5x5 average filter
>> h = fspecial('average', 5);

% zero-padding
>> results1 = imfilter(cat,h);

% replication
>> results2 = imfilter(cat,h,'replicate');

% symmetric
>> results3 = imfilter(cat,h,'symmetric');

% circular
>> results4 = imfilter(cat,h,'circular');


>> figure, imshow(cat)
>> title('Original');
>> figure, imshow(results1)
>> title('Zero-Padded');
>> figure, imshow(results2)
>> title('Replicate');
>> figure, imshow(results3) 
>> title('Symmetric');
>> figure, imshow(results4)
>> title('Circular');  


Using imfilter – with filtering mode

With imfilter, we can choose one of the two filtering modes: correlation or convolution. Correlation is the default. Convolution rotates the filter by 180 degree before performing multiplication. The diagram below demonstrates correlation versus convolution operation.  


Let us try out the filtering mode and see what happen to the images after we applied the filtering mode. Below is the sample of MATLAB code that we can try:

>> h = [1 2 3; 4 5 6; 7 8 9];
>> h = h/45; 

% correlation mode
% or we can use imfilter(cat,h,'conv');
>> result_corr = imfilter(cat,h);

% convolution mode
>> result_conv = imfilter(cat,h,'conv');
>> figure, imshow(result_conv)
>> title('Convolution')
>> figure, imshow(result_corr)
>> title('Correlation')  





Using imfilter – size option

There are two size options – full or same. ‘full’ option will be as large as the padded image, while ‘same’, which is the default, will be the same size as the input image. Below are the codes that demonstrate the application of size option for imfilter. Notice that ths ‘same’ is 16x16 whereas ‘full’ 18x18.

% create a 3x3 average filter
>> h = [1/9, 1/9, 1/9; 1/9, 1/9, 1/9,; 1/9, 1/9, 1/9];

% same option
>> stock_cut_same = imfilter(original_img,h);

% full option
>> stock_cut_full = imfilter(original_img,h, 'full');


>> figure, imtool(stock_cut_same) >> figure, imtool(stock_cut_full)  


[Lab03] Intensity Transformation and Spatial Filter Part 1

Untitled Document


Intensity Transformation Functions


Intensity transformation involves contrast manipulation and thresholding. An application of intensity transformation is to increase the contrast between certain intensity values so that we can the information that we seek is more visible and clear.

Making changes in the intensity is done through Intensity Transformation Functions. The four main intensity transformation functions are:
  • Photographic Negative
  • Gamma Transformation
  • Logarithmic Transformation
  • Contrast-stretching Transformation


Photographic negative


To use Photographic Negative, use MATLAB function called imcomplement . With this transformation, the true black become true white and vice versa. It is suitable when the black areas are dominant in size. Below are the codes that implements photographic negative and example of photographic negative images.

>> I = imread('cat.jpg');
>> imshow(I)
>> J=imcomplement(I);
>> figure,imshow(J)


The following plots the photographic negative.



Gamma Transformation 


To use Gamma Transformation, use MATLAB function called imadjust. The syntax of this function is:

J = imadjust(f,[low_in high_in],[low_out high_out], gamma)
where :
f = input image
[low_in high_in],[low_out high_out] = for clipping
gamma = controls the curve


Values for low_in, high_in, low_out, and high_out must be between 0 and 1. Values below low_in are clipped to low_out and values above high_in are clipped to high_out. For the example below, we will use empty matrix ([]) to specify the default of [0 1].

gamma specifies the shape of the curve describing the relationship between the values in J and f. If gamma is less than 1, the mapping is weighted toward higher (brighter) output values. If gamma is greater than 1, the mapping is weighted toward lower (darker) output values. By default, gamma is set to 1 (linear mapping).

Below are the codes that implements gamma transformation and example of gamma transformation images.

>> I = imread('cat.jpg');
>> J1 = imadjust(I,[],[],3);
>> J2 = imadjust(I,[],[],1);
>> J3 = imadjust(I,[],[],0.4);
>> imshow(J1);
>> figure, imshow(J2);
>> figure, imshow(J3);




The following plots the gamma transformations with varying gamma.




Logarithmic Transformation


To use Logarithmic Transformation, use the function c*log(1+f).This transformation enhances the details (or contrast) in the darker region of an image (with lower intensity values) by expensing detail in brighter regions. In other words, it expands the values of dark pixel in an image while compressing the higher level values. The syntax of this function is :

g = c*log(1+double(f))

The higher the c, the brighter the image will appear. Any values (produced from logarithmic transformation) greater than one, are displayed as full intensity, equivalent to the value of 1. Below are the codes that implements logarithmic transformation and example of logarithmic transformation images.

>> imshow(I)
>> I2 = im2double(I);
>> J4 = 1 * log(1 + I2);
>> J5 = 2 * log(1 + I2);
>> J6 = 5 * log(1 + I2);
>> figure, imshow(J4)
>> figure, imshow(J5)
>> figure, imshow(J6)




The following plots the logarithmic transformations with varying C.


Contrast-Stretching Transformation


Contrast-Stretching Transformation (also called Piecewise-Linear Transformation) stretches gray-level ranges where we desire more information. This transformation increases the contrast between the darks and the lights. In simple words, the dark becomes darker and the light becomes brighter. To use this transformation, we use the function as below:

g = (1./(1+(m./(double(f)+eps)).^ E)


where:
E = slope function
m = mid-line of switching from dark value to light value
eps = MATLAB constant; distance between 1.0 and the next largest number in double-precision floating point [2^(-52)]


            Below are the codes that use contrast-stretching transformation. The mean of the image intensities as m value and varies the value of E. The higher the value of E, the function becomes more like a thresholding function, which results the image is more black and white than grayscale.

>> I=imread('cat.jpg');
>> I2=im2double(I);
>> m=mean2(I2)
>> contrast1 = 1./(1 + (m ./(I2 + eps )).^4);
>> contrast2 = 1./(1 + (m ./(I2 + eps )).^5);
>> contrast3 = 1./(1 + (m ./(I2 + eps )).^10);
>> contrast4 = 1./(1 + (m ./(I2 + eps )).^-5);
>> contrast5 = 1./(1 + (m ./(I2 + eps )).^-1);
>> figure, imshow(I2)
>> figure, imshow(contrast1)
>> figure, imshow(contrast2)
>> figure, imshow(contrast3)
>> figure, imshow(contrast4)
>> figure, imshow(contrast5)





The following plots the gamma transformations with varying E.



Another contrast-stretching transformation being applied by setting the value of E and to 4 and varies the value of m. The higher the value of m, the darker the image and fewer details are showed.

>> I=imread('cat.jpg');
>> I2=im2double(I);
>> contrast6 = 1./(1 + (0.2 ./(I2 + eps )).^4);
>> contrast7 = 1./(1 + (0.5 ./(I2 + eps )).^4);
>> contrast8 = 1./(1 + (0.7 ./(I2 + eps )).^4);
>> figure, imshow(I2)
>> figure, imshow(contrast6)
>> figure, imshow(contrast7)
>> figure, imshow(contrast8)


The following plots the contrast-stretching transformations with varying m.