Sabtu, 17 Mei 2008

Portofolio 2



myPhoto.jpg




bw = myfunc('myPhoto.jpg', 0.4);


strel = circularstruct(2);
imclose(bw,strel);


imopen(bw, strel);


tugas3_1= openclose (bw, 2)

tugas3_2 = closeopen (bw, 2)


listing of myfunc.m:

function bw = myfunc(im, thresh)
image = imread(im);
g = rgb2gray(image);
bw = im2bw(image,thresh);
imshow(bw)
imwrite(bw,'image_blacknwhite.png');



listing of tugas3_1.m:

function tugas3_1 = openclose (image, radius)
strel = circularstruct(radius);
openclose = imclose(imopen(image, strel),strel);
imshow(openclose);


listing of tugas 3-2.m:

function tugas3_2 = closeopen (image, radius)
strel = circularstruct(radius);
closeopen = imopen(imclose(image, strel),strel);
imshow(closeopen);


listing
of ciscularstruct.m:

% CIRCULARSTRUCT
%
% Function to construct a circular structuring element
% for morphological operations.
%
% function strel = circularstruct(radius)
%
% Note radius can be a floating point value though the resulting
% circle will be a discrete approximation
%
% Peter Kovesi March 2000

function strel = circularstruct(radius)

if radius <>
error('radius must be >= 1');
end

dia = ceil(2*radius); % Diameter of structuring element

if mod(dia,2) == 0 % If diameter is a odd value
dia = dia + 1; % add 1 to generate a `centre pixel'
end

r = fix(dia/2);
x = ones(dia,1)*[-r:r]; % Matrix with each pixel set to its x coordinate relative to the centre
y = [-r:r]'*ones(1,dia); % " " " " " " " y " " " " "
rad = sqrt(x.^2 + y.^2); % " " " " " " " radius from the middle
strel = rad<=radius



listing of locatelandmarks.m:


% LOCATELANDMARKS - locates landmarks on SURF form
%
% Usage: [tl, tr, bl, br] = locatelandmarks(im)
%
% Argument: im - Image to be processed, assumed binary.
%
% Returns: tl, tr, bl, br
% - Coordinates of the centroids of the top-left, top-right,
% bottom-left and bottom-right landmarks respectively.
% These coordinates are returned as column vectors in the
% form [row; col] for each landmark.
%
% The function should also display the image with the centroids of the
% landmarks overlayed.

function [tl, tr, bl, br] = locatelandmarks(im)

image = imread(im)
[rows,cols] = size(image);

% menginisialisasi koordinat landmark
tl=[rows;cols];
tr=[rows;0 ];
bl=[0 ;cols];
br=[0 ;0 ];

bw = ~image;

a = 8/784; %faktor skala

SE = ones(a*rows, a*cols);
%lakukan close-open
closeopen = imopen(imclose(bw,SE),SE);

% matrix yang setiap pixelnya di set untuk setiap x koordinat
x = ones(rows,1)*[1:cols];

% matrix yang setiap pixelnya di set untuk setiap x koordinat
y = [1:rows]'*ones(1,cols);

% melabeli semua blob
[L, num] = bwlabel(closeopen);

% mencari blob untuk menentukan titik tengah
for i = 1:num
img = L==i;
area(i) = sum(sum(img));
meanx = sum(sum(double(img).*x))/area(i);
meany = sum(sum(double(img).*y))/area(i);

% tentukan kiri atas
if tl(1)+tl(2) > meanx + meany
tl = [meanx; meany];
tlindex = i;
end
% tentukan kanan atas
if tr(1)-tr(2) > meanx - meany
tr = [meanx; meany];
trindex = i;
end
% tentukan kanan bawah
if br(1)+br(2) <>
br = [meanx; meany];
brindex = i;
end
% tentukan kiri bawah
if bl(1)-bl(2) <>
bl = [meanx; meany];
blindex = i;
end
end

imshow(image)
hold;
landmarks = [tl, tr, br, bl];
plot(landmarks(1,:), landmarks(2,:),'xr','MarkerSize',25,'LineWidth',2);
plot(landmarks(1,:), landmarks(2,:),'or','MarkerSize',15,'LineWidth',1);
hold;

end

Jumat, 16 Mei 2008

Portofolio 1



1. mengubah image jadi hitam putih
bw = myfunc('lego1.png', 0.6)





2. BWL = bwlabel(~bw)
ob1 = BWL==1
ob2 = BWL==2
ob3 = BWL==3
[area, centroid, theta, roundness] = moments(ob1)
[area, centroid, theta, roundness] = moments(ob2)
[area, centroid, theta, roundness] = moments(ob3)







Listing of myfunc.m :
function bw = myfunc(im, thresh)
image = imread(im);
g = rgb2gray(image);
bw = im2bw(image,thresh);
imshow(bw)
imwrite(bw,'image_blacknwhite.png');
Listing of moments.m :

% MOMENTS
%
% Fungsi menghitung momen sebuah citra biner dan mengembalikan
% area, centroid, sudut dari sumbu inertia minimum, dan sebuah
% ukuran ‘kebulatan'. Fungsi ini mengasumsikan bahwa hanya ada
% satu obyek pada citra biner.
%
% function [area, centroid, theta, roundness] = moments(im)
%
% Argument: im - citra biner berisi nilai 0 atau 1
%
% Returns: area - luasan citra biner
% centroid - vektor dengan 2 elemen
% theta - sudut sumbu kelembaman minimum (radian)
% roundness - ratio minimum inertia/maximum inertia.
%
% Fungsi ini juga menampilkan citra dan letak dari
% centroid dan sumbu inertia minimum. Area yang dihitung
% harus ditampilkan sebagai title di citra.

function [area, centroid, theta, roundness] = moments(im)
%mendapatkan ukuran matrix image
[rows,cols] = size(im);
x = ones(rows,1)*[1:cols]; % Matrix pada setiap pixel set pada koordinat x
y = [1:rows]'*ones(1,cols); % Matrix pada setiap pixel set pada koordinat y

area = sum(sum(im));
meanx = sum(sum(double(im).*x))/area;
meany = sum(sum(double(im).*y))/area;
centroid = [meanx, meany];

% mengubah koordinat ke pusat masa
x = x - meanx;
y = y - meany;

a = sum(sum(double(im).*x.^2));
b = sum(sum(double(im).*x.*y))*2;
c = sum(sum(double(im).*y.^2));

denom = b^2 + (a-c)^2;

if denom == 0
% tetha mengubah sudut
thetamin = 2*pi*rand;
thetamax = 2*pi*rand;
roundness = 1;
else
sin2thetamin = b/sqrt(denom); %solusi positif
sin2thetamax = -sin2thetamin; %solusi negatif
cos2thetamin = (a-c)/sqrt(denom); %solusi positif
cos2thetamax = -cos2thetamin; %solusi negatif

thetamin = atan2(sin2thetamin, cos2thetamin)/2;
thetamax = atan2(sin2thetamax, cos2thetamax)/2;
Imin = 0.5*(c+a) - 0.5*(a-c)*cos2thetamin - 0.5*b*sin2thetamin;
Imax = 0.5*(c+a) - 0.5*(a-c)*cos2thetamax - 0.5*b*sin2thetamax;
roundness = Imin/Imax;
end

% menggambar sumbu sesuai dengan objek
rho = sqrt(area)/(roundness + 0.5) + 5 ;
[X1,Y1] = pol2cart(thetamin, rho);
[X2,Y2] = pol2cart(thetamin + pi, rho);

imshow(im);
hold;
line([X1 + meanx, X2 + meanx],[Y1 + meany, Y2 + meany])
plot(meanx, meany,'r+')
hold;