3 Mart 2012 Cumartesi

MatLab - Flip & Rotate & Resize image

% Main function
function main()
global height;
global width;
global x;

x = imread('sample_img.jpg');

% display the original image
figure
image(x);
title('original');

height = size(x, 1);
width = size(x, 2);

flipVertical();
flipHorizantal();
rotateLeft();
rotateRight();
resizeImage();

return

% flips input image vertically
function flipVertical()
global height;
global width;
global x;
y = zeros(height, width, 3, 'uint8');

for i=1:height
for j=1:width
for k=1:3
y(height - i + 1, j, k) = x(i, j, k);
end
end
end
imwrite(y, 'flipVertical.jpg', 'jpg');
figure
image(y);
title('vertical flip');
return

% flips input image horizontally
function flipHorizantal()
global height;
global width;
global x;
y = zeros(height, width, 3, 'uint8');

for i=1:height
for j=1:width
for k=1:3
y(i, width - j + 1, k) = x(i, j, k);
end
end
end
imwrite(y, 'flipHorizontal.jpg', 'jpg');
figure
image(y);
title('horizontal flip');
return

% rotates input image to left
function rotateLeft()
global height;
global width;
global x;
y = zeros(width, height, 3, 'uint8');

for i=1:width
for j=1:height
for k=1:3
y(i, j, k) = x(j, width-i+1, k);

end
end
end
imwrite(y, 'rotateLeft.jpg', 'jpg');
figure
image(y);
title('rotate left');
return

% rotates input image to right
function rotateRight()
global height;
global width;
global x;
y = zeros(width, height, 3, 'uint8');

for i=1:width
for j=1:height
for k=1:3
y(i, j, k) = x(height - j + 1, i, k);
end
end
end
imwrite(y, 'rotateRight.jpg', 'jpg');
figure
image(y);
title('rotate right');
return

% resizes input image to half by keeping aspect ratio
function resizeImage()
global x;
y = zeros(201, 250, 3, 'uint8');
for i=1:201
for j=1:249
for k=1:3
y(i, j, k) = x(i * 2, j * 2, k);
end
end
end

imwrite(y, 'resizeImage.jpg', 'jpg');
figure
image(y);
title('resized');

return

Download Code

Hiç yorum yok:

Yorum Gönder