Friday, October 26, 2012

Threshold

Threshold Image

Threshold: 

Threshold follows the same concept as in basic electronics, here it is used to convert the grayscale image to black and white(binary image, consisting of 0's and 1's as pixel values.)
Threshold for an image can be set in the following ways:
  • Brute force : .5 times the maximum pixel intensity, which will be 127 in most of the images.
  • Otsu's method : Based on a paper published in 1979 (IEEE Link), this method makes use of the histogram shape to find the threshold for the image. This gives a good enough performance but there are a few drawbacks when we run on entire image. There are a few regions that miss out as it will not consider the local neighborhood of the image. So, I prefer to use block based processing as this splits the image into smaller blocks and this can help us in getting the good idea in neighborhood for calculating threshold.

The Matlab code is given below:

clc;clear all;close all;

%Local Function Definitions:
fun = @(block_struct) thresher(block_struct.data);

I1= imread('rice.png');

f1 = figure(1);
imshow(I1);
title('Actual image');

brute = im2bw(I1,0.5);
brute = bwareaopen(brute,50);
imshow(brute);
title('Brute force Threshold image');
imwrite(brute,'brute.jpg','jpeg');
pause();


Brute Force
















thresh = graythresh(I1);
otsu_full_image = im2bw(I1,thresh);
otsu_full_image = bwareaopen(otsu_full_image,30);
imshow(otsu_full_image);
title('Otsu`s Threshold on Full Image');
imwrite(otsu_full_image,'otsu_full_image.jpg','jpeg');
pause();


Otsu's Threshold (Full Image)















 

 block_otsu = blockproc(I1,[32,32],fun);
block_otsu = bwareaopen(block_otsu,30);
imshow(block_otsu);
title('Otsu`s Threshold on block Image');
imwrite(block_otsu,'block_otsu.jpg','jpeg');
pause();

Otsu's Threshold (Block Processing)















thresher.m (Function used during block processing)

function [ out ] = thresher(in)

thresh = graythresh(in);
out = im2bw(in,thresh);
imshow(out);

end


Note that the edges near lower corner have been jagged in the first and second method. This is not seen in block processed image. In the brute force threshold image we can observe the grain missed out in the top right part of the image.

Now let's see what is block processing using the 2 lines that define it.

fun = @(block_struct) thresher(block_struct.data);
block_otsu = blockproc(I1,[32,32],fun);

 

Here we define that we should split the image I1 into smaller blocks of 32X32 image and then run the function 'fun' on it. This 'fun' was defined in the second line of the program that it has to send the data under process to the 'thresher' function. The thresher function calculates the Otsu's threshold for that 32X32 image and returns a 32X32 binary image calculated with threshold value calculated for that block. Thus we have used the ability of 32X32 neighborhood processing.Note: If block size is smaller it can take more time to run and too small block size may not always produce good results. So, its a trade-off.

In blockproc 'UseParallel',true can help sometimes if you have Parallel Computing Toolbox.

Thursday, October 11, 2012

Basics of Digital Image

The blog had been quiet for a long time, I am restarting it again with a different perspective. This time around I would be learning from basics and will try to explain the stuff as I understand from the basics. If I have misunderstood any concept, I will be pleased to learn from anyone.

I will be using Gonzalez and Woods 3rd Ed. for this. I will try to give minimal explanation on Matlab code but stress more on the concepts around as Matlab Help is extensively available at www.mathworks.com.

Without wasting much time lets begin....

Few terms to begin with

Bare Basics:

Grayscale Image: A frame of 2 D distribution of intensities.
Coordinates - x,y
Any image is a product component of intensity-i(x,y) and reflectance-r(x,y)
Intensity: 0 to inf (Yes, infinity and that's how you write in Matlab).
Reflectance: 0 to 1
(Think, what would be reflectance of a 1. black screen? 2.white screen? 3. transparent plastic sheet?)

How to see in Matlab:

in = imread('rice.png');
whos in;
size(in)

The second line here whos gives variable name, size of image read into, bytes in storage, datatype per element. If you just give whos, you end up getting the details about all the variables in workspace.


This means in black and white movies you have 3 dimensions (x, y, time).

Color Image: A triplet frame of 2 D distribution of intensities. (I will explore more in color models later.)
Coordinates - x, y, z
How to see in Matlab:


in = imread('color.jpg'); %make sure there is a color image of %same name in the folder
whos in;
size(in)

This means in color movies in a basic sense you have 4 dimensions (x, y, z, time).

Image Sampling and Quantization:

Analog image to Digital image conversion uses this.
Discretization in time domain is image sampling and digitization of amplitudes after discretization is quantization. End result "A digital image".

Sampling intervals in x-axis gives your length resolution and y-axis gives you the height resolution of a picture.
More the resolution, better defined is the image for visual perception, but more is the size for storage..

Vector images that are used in Photoshop are handled differently (as to what I know the images there are defined by mathematical equations, so can be extrapolated/expanded without loss of resolution/chequer-boarding). A good read on this is in Wikipedia here...

Hope I have started off well, please post questions so that I can get different perspectives...