Pages

Wednesday, April 27, 2011

Matlab Syntax Summary before UTS

Reading an image (jpg, bmp, png, tif):
image = imread (‘image.ext’);

Showing an image (jpg, bmp, png, tif):
imshow (‘image.ext’);

Showing some of image in one figure/screen:
subplot (m,n,p); imread(‘img.ext’);

%annotation:
%m = total row
%n = total column
%p = image position

Making a new screen/figure:
figure;

Conversion from RGB to grayscale:
gray = rgb2gray(image);

Conversion from RGB to HSV:
hsv = rgb2hsv(image);

Conversion from RGB to black and white (BW):
bw = im2bw(image);

Looking for an image histogram:
histogram = imhist(image); %it can’t be implemented in RGB image
plot(histogram);

Accessing pixel in certain position:
pixel = image (m,n);

Kernel prewitt:
-1 0 1
-1 0 1          %for left
-1 0 1
1 0 -1
1 0 -1          %for right
1 0 -1

Kernel sobel:
-1 0 1
-2 0 2          %for left
-1 0 1
1 0 -1
2 0 -2          %for right
1 0 -1

Kernel laplace:
-1 -1 -1
-1  8 -1         %trial 1
-1 -1 -1
 1 -2  1
-2  4 -2         %trial 2
 1 -2  1

Rotation:
function rotasi(img)
    [row,col] = size(img);
    for i = 1:row
        for j = 1:col
            out(j,row+1-i) = img(i,j);
        end
    end
    subplot(1,2,1),imshow(img);
    subplot(1,2,2),imshow(out);
end

%annotaion:
%90 degrees = (j,row+1-i)
%180 degrees = (row+1-i,column+1-j)
%270 degrees = (column +1-j,i)


Translation:
function translasi(img,m,n)
    row = size(img,1);
    column = size(img,2);
    for i = 1:row
        for j = 1:column
            if i+m>0 && i+m<row+1 && j-n>0 && j-n<column+1
                result(i,j) = img(i+m, j-n);
            else
                result(i,j) = img(1,1)*0;
            end
        end
    end
    imshow(result);
end

Dilatation:
function dilatasi(img,sx,sy)
    row = size(img,1)*sx;
    column = size(img,2)*sy;
    for i = 1:row
        for j = 1:column
            if sx*i>0 && sx*i<row+1 && sy*j>0 && sy*j<column+1
                out(sx*i, sy*j) = img(i,j);
            end
        end
        imshow(out);
    end
end

%annotation:
%sx = sy = scale

Reflection:


To binary (for grayscale only):
function result = toBiner(img)
    result = img>=128;
end

To negative (for grayscale only):
function result = toNegatif(img)
    result = 255-img;
    imshow(result);
end

Brightening and darkening (for grayscale only):
function result = brightening(img, c)
    result = img + c;
end

function result = darkening(img, c)
    result = img - c;
end

Clipping (for grayscale only):
function result = clipping(img)
    out = img;   
    for i = size(img,1)
        for j = size(img,2);
            if img(i,j)<0
                out(i,j) = 0;
            elseif img(i,j)>=0 && img(i,j)<=255
                out(i,j) = img(i,j);
            else
                out(i,j) = 255;
            end     
        end
    end
    result = out;
end

Erotion (for binary only):
function result = erotion(img, a)
    [maxRow, maxCol] = size(img);
    out = img;
    for i = 1:(maxRow-2)
        for j = 1:(maxCol-2)
            if img(i+1,j+1)==0
                for k = 1:3
                    for l = 1:3
                        out(i+k-1,j+l-1) = img(i+1,j+1).*a(k,l);
                    end
                end
            end
        end
    end
    result = out;
end

%annotation:
%a = matrix 3 x 3 (kernel that be used)


Dilation (for binary only):
function result = dilate(img,a)
    [maxRow, maxCol] = size(img);
    out = img;
    for i = 1:(maxRow-2)
        for j = 1:(maxCol-2)
            if img(i+1,j+1)==1
                for k = 1:3
                    for l = 1:3
                        if a(k,l)==1
                            out(i+k-1,j+l-1) = img(i+1,j+1).*a(k,l);
                        end
                    end
                end
            end
        end
    end
    result = out;
end

%annotation:
%a = matrix 3 x 3 (kernel that be used)


Making mean filter for image:
function result = meanFilter(img)
    a = [1/9 1/9 1/9;1/9 1/9 1/9;1/9 1/9 1/9];
    [maxRow, maxCol] = size(img);
    out = img;
    for i = 1:(maxRow-2)
        for j = 1:(maxCol-2)
            if img(i+1,j+1)==0
                mean = 0;
                for k = 1:3
                    for l = 1:3
                        mean = mean + img(i+k-1,j+l-1).*a(k,l);
                    end
                end
                out(i+1,j+1) = mean;
            end
        end
    end
    result = out;
end

Making median filter for image:
function result = medianFilter(img)
    [maxRow, maxCol] = size(img);
    out = img;
    for i = 1:(maxRow-2)
        for j = 1:(maxCol-2)
            if img(i+1,j+1)==0
                temp = zeros(1,9);
                for k = 1:3
                    for l = 1:3
                        temp(1,l+3*(k-1)) = img(i+k-1,j+l-1);
                    end
                end
                out(i+1,j+1) = median(temp,2);
            end
        end
    end
    result = out;
end

Making a chess board:
function chessBoard
    [x,y] = meshgrid(0:1:255);
    z = mod(floor(x/32)+floor(y/32),2);
    imshow(z)
end

Making a white hole:
function whiteHole(radius)
    [x,y] = meshgrid(0:255,0:255);
    img = (sqrt((x-127).^2 + (y-127).^2)<= radius);
    imshow(img);
end

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. yeuh urang geus bisa,,,

    wkwkwkwk

    jangan lupa berkunjung ke

    itsalltoofanz.blogspot.com

    ReplyDelete