Archive

Posts Tagged ‘Matlab’

Matlab offset values and histograms

April 26th, 2007

The semester slowly moves to its logical ending - the summer, and the only question I have is, if I’m going to use Matlab after finishing Data lectures or not.

But since, I’m still using , here is going to be one of the last projects for DIP lesson - offset changes of an image, with later on processing of histograms.

Comparing erode and dilate functions - offset procedure doesn’t effect the neighbouring pixels - all the actions are performed on each pixel respectively.

For changing offset value of an image we need to:

  1. Create a matrix of an initial image;
  2. Create a colour array for mapping pixels of the same colours (will be used for histogram);
  3. Accept the offset value from the user;
  4. And, of course, recalculate all!

Since we’re going to work with grey scale image, we have up to 255 colours from black to white. Later on, I’ll use the picture which was taken close to the institute.

%Reading the image
Ia = imread(’a.jpg’);
disp(’..Image “A” is loaded’)

%Initializing the colour array

arrA = zeros([1 255]);

Since the image has 683×1024 size, we’re going to use the nested loop to match all the colours and store its pixels into the array:

for m=1: 683
for n=1: 1024
%Storing repetetive color indexes in color array.
for i=1: 255
%Placing colors into color arrays for 3 images respectively.
if i == Ia(m,n)
arrA(1,i) = arrA(1,i) +1;
end
end
end
end

After eliminating the matrix picture, and entering the offset value, we can create an offset.m function file, where the rest of the calculations will take place. For , we’ll need to initiate a new matrix, of the same size, and another colour array, which will hold transformed pixel colours, and their quantities, to show the difference in the histograms:

for i=1: 683
for j=1: 1024
for k=1: 255
if k == Mtrx(i,j)
newArr(1,k) = newArr(1,k) + 1;
end
end
end
end
%…
disp(’Resulting images…’)
figure,imshow(y); title(’Initial Image’);
figure,imshow(Mtrx); title(’Result of Offset.jpg’);
disp(’Histograms…’)
figure,plot(z);title(’Initial Histogram’);
figure,plot(newArr); title(’Resulting Histogram’);

I’ve mentioned the main states of the calculation, but if you need to get really into the sources, simply download the source files, where three images have been processed, and enhanced with scaling function source files. For more complicated offset functionality, you may be interested in the offset filter made in Boston University.

Design, Gadgets ,

Matlab. Dilate, Erode & Open-Close functions

March 22nd, 2007

matlab_in-actionWhile playing around with basic types of grayscale and binary images in , I’ve found few nice functions to be used to perform simple morphological changes and noise clearance of an image.

If you have to use a basic morphological changes to your grayscale image, I think ’s impossible to avoid imerode() and imdilate() functions, which give the ability of (as you guessed!) erosion and dilation binary, grayscale or packed images.

Of course, to perform these changes you should use strel() function with predefined structural elements, which will be used in dilation and/or erosion functions. The links provided in the post will give a huge amount of description concerning these functions with examples of its usage.

The other function I’d like to mention are fully dependent on dilate/erode pair, since combining these two, we can provide better elimination of noise with keeping initial image morphology.

The functions imopen() and imclose() use same arguments passing through therefore you can achieve better with writing simply another few lines of code.

Links , , ,

Matlab: Threshold

March 1st, 2007

Even though we have threshold built-in function in , ’s not really convenient to be used, because an automated threshold level identification can give an opportunity for the user to control the intensity of the image he is working on.
I think the best explanation of the theoretical part is a simple example. So, here we are, the user is given the choice of inputting his own threshold level, so the program will work on .

Image = imread(’picturename.tif’);
%Input the user desired threshold level
thresh = input(’Enter a threshold level [between 0 & 1]:’);
%Convert an image to grayscale
ImFin = im2bw(Image, thresh);

For those who need more sophisticated ways of processing an image via later pixel calculation, the user will have use this tiny piece of code:


%translate coordinates of ImFin to matrix I
I = size(ImFin);
%Initialize basic variables
sum_white = 0;
sum_black=0;
%Coordinates nested loop for eliminating pixels position
for i=1:I(1)
for j=1:I(2)
%Color condition
if ImFin(i,j) ~=0;
sum_white = sum_white+1;
else
sum_black = sum_black+1;
end
end
end
a = [sum_white, sum_black];

Due to the fact that we’ve transformed the image to binary type, can contain only two types of color, 0(black) or 1(white), thus using

plot()

function we can compare the results of images, and if a new matrix “a” have the same results as

imhist()

function.

Threshold function is quite useful in case of silhouette recognition, when we have a picture with monochrome background, so ’s easy to establish the difference between foreground and background objects of the image.

Design, Gadgets , , , ,

Image Processing: Outside and Inside views

February 27th, 2007

matlab_logo
Working with different graphical editors, like Photoshop, Illustrator, Corel Draw, I’ve never gone really deep into details of actual algorithms. Simply, because I didn’t have to. Since every tool does what I want - ’s enough!

But, taking Data course, I’ve witnessed bunches of maths inside. And after calculating an average number of filters used in any graphic editor, I just want to take off my hat in front of these people who created these applications, and thank them. Guys, you’re a crazy maths geeks!

Another regards should be sent to Matlab Community for releasing such a powerful tool, and those people who support the community and post really helpful material, because concerning so many features of I got lost in this pile of commands, functions and processing features.

Design, Gadgets , , , ,