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

Tidak ada komentar: