Matlab Syntax Summary before UTS Kernel prewitt:
-1 0 1
-1 0 1 %for left
Reading an image (jpg, bmp, png, tif): -1 0 1
image = imread (‘image.ext’);
1 0 -1
1 0 -1 %for right
Showing an image (jpg, bmp, png, tif): 1 0 -1
imshow (‘image.ext’);
Kernel sobel:
Showing some of image in one figure/screen: -1 0 1
subplot (m,n,p); imread(‘img.ext’); -2 0 2 %for left
-1 0 1
%annotation: 1 0 -1
2 0 -2 %for right
%m = total row
1 0 -1
%n = total column
%p = image position
Kernel laplace:
Making a new screen/figure: -1 -1 -1
-1 8 -1 %trial 1
figure;
-1 -1 -1
1 -2 1
Conversion from RGB to grayscale: -2 4 -2 %trial 2
gray = rgb2gray(image); 1 -2 1
Conversion from RGB to HSV: Rotation:
hsv = rgb2hsv(image); function rotasi(img)
[row,col] = size(img);
Conversion from RGB to black and white (BW): for i = 1:row
bw = im2bw(image); for j = 1:col
out(j,row+1-i) = img(i,j);
end
Looking for an image histogram: end
histogram = imhist(image); %it can’t be implemented in RGB subplot(1,2,1),imshow(img);
image subplot(1,2,2),imshow(out);
plot(histogram); end
Accessing pixel in certain position: %annotaion:
pixel = image (m,n); %90 degrees = (j,row+1-i)
%180 degrees = (row+1-i,column+1-j)
%270 degrees = (column +1-j,i)
Translation: Reflection:
function translasi(img,m,n)
row = size(img,1);
To binary (for grayscale only):
column = size(img,2);
function result = toBiner(img)
for i = 1:row result = img>=128;
for j = 1:column end
if i+m>0 && i+m<row+1 && j-n>0 && j-n<column+1
result(i,j) = img(i+m, j-n);
else To negative (for grayscale only):
result(i,j) = img(1,1)*0; function result = toNegatif(img)
end result = 255-img;
end imshow(result);
end end
imshow(result);
end Brightening and darkening (for grayscale only):
function result = brightening(img, c)
Dilatation: result = img + c;
function dilatasi(img,sx,sy) end
row = size(img,1)*sx;
column = size(img,2)*sy; function result = darkening(img, c)
for i = 1:row result = img - c;
for j = 1:column end
if sx*i>0 && sx*i<row+1 && sy*j>0 && sy*j<column+1
out(sx*i, sy*j) = img(i,j); Clipping (for grayscale only):
end function result = clipping(img)
end out = img;
imshow(out);
for i = size(img,1)
end
for j = size(img,2);
end
if I(i,j)<0
out(i,j) = 0;
%annotation: elseif I(i,j)>=0 && I(i,j)<=255
%sx = sy = scale out(i,j) = I(i,j);
else
out(i,j) = 255;
end
end
end
result = out;
end
Erotion (for binary only): %annotation:
function result = erotion(img, a) %a = matrix 3 x 3 (kernel that be used)
[maxRow, maxCol] = size(img);
out = img;
for i = 1:(maxRow-2)
for j = 1:(maxCol-2)
Making mean filter for image:
if I(i+1,j+1)==0 function result = meanFilter(img)
for k = 1:3 a = [1/9 1/9 1/9;1/9 1/9 1/9;1/9 1/9 1/9];
for l = 1:3 [maxRow, maxCol] = size(I);
out(i+k-1,j+l-1) = out = img;
img(i+1,j+1).*a(k,l); for i = 1:(maxRow-2)
end for j = 1:(maxCol-2)
end if img(i+1,j+1)==0
end mean = 0;
end for k = 1:3
end for l = 1:3
result = out; mean = mean + img(i+k-1,j+l-
end 1).*a(k,l);
end
end
%annotation: out(i+1,j+1) = mean;
%a = matrix 3 x 3 (kernel that be used) end
end
end
Dilation (for binary only): result = out;
function result = dilate(img,a) end
[maxRow, maxCol] = size(img);
out = img;
for i = 1:(maxRow-2)
for j = 1:(maxCol-2)
if I(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
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