Matlab 版 (精华区)
发信人: Christy (绿叶~~捣鼓六仙捣毁仙), 信区: Matlab
标 题: 我做了一个做图像放大缩小的程序
发信站: 哈工大紫丁香 (2002年04月19日21:53:53 星期五), 站内信件
实验证明和MATLAB图形窗口的放大缩小功能几乎一模一样.
% This function is used to amplify an image with any ratio.
function Free_amplification(filename, Am, An)
% Getting an image.
[original_image, x, y, z] = Fetch_image(filename);
% Initialising the resulting image.
for i = 1:floor(x * Am)
for j = 1:floor(y * An)
for q = 1:z
amplified_image(i, j, q) = 0;
end
end
end
% Amplifying the image.
% The amplifying coefficients (the gain).
% Am is the vertical amplification coefficient.
% An is the horizontal amplification coefficient.
% Both of them are adjustable.
% Please note that function floor() means rounding a number
% towards minus infinity, whilst ceil() towards plus infinity.
% Also note that in MATLAB, the strating index of a matrix is
% A(1, 1,...), whilst in C-style languages, it is A[0][0][...].
% Case 1: Stretching the image along both the vertical and horizontal axes.
if Am >= 1 & An >= 1
for i = 1:x
for j = 1:y
for k = 0: (Am -1)
for w = 0: (An -1)
for q = 1:z
amplified_image(floor((Am * (i - 1) + 1 + k)), ...
floor((An * (j - 1) + 1 + w)), q) = original_image...
(i, j, q);
amplified_image(floor((Am * (i - 1) + 1 + k)),...
ceil((An * (j - 1) + 1 + w)), q) = original_image...
(i, j, q);
amplified_image(ceil((Am * (i - 1) + 1 + k)),...
floor((An * (j - 1) + 1 + w)), q) = original_image...
(i, j, q);
amplified_image(ceil((Am * (i - 1) + 1 + k)),...
ceil((An * (j - 1) + 1 + w)), q) = original_image...
(i, j, q);
end
end
end
end
end
% Case 2: Compressing the image along both the vertical and horizontal axes.
elseif Am <= 1 & An <= 1
Am = 1/Am;
An = 1/An;
for i = 1:floor(x/Am)
for j = 1:floor(y/An)
for k = 0:(Am - 1)
for w = 0:(An - 1)
for q = 1:z
amplified_image(i, j, q) = amplified_image(i, j, q) ...
+ double(original_image(floor((Am * (i - 1) + 1 + k)),...
floor((An * (j - 1) + 1 + w)), q))/(Am * An);
end
end
end
end
end
% Case 3: Stretching the image along the vertical axis, then compressing it
% along the horizontal one.
elseif Am > 1 & An < 1
for i = 1:x
for j = 1:y
for k = 0: (Am -1)
for q = 1:z
intermediate_image(floor((Am * (i - 1) + 1 + k)), j, q)...
= original_image(i, j, q);
intermediate_image(ceil((Am * (i - 1) + 1 + k)), j, q) =...
original_image(i, j, q);
end
end
end
end
An = 1/An;
for i = 1:floor(x * Am)
for j = 1:floor(y/An)
for w = 0:(An - 1)
for q = 1:z
amplified_image(i, j, q) = amplified_image(i, j, q) + ..
double(intermediate_image(i, floor((An * (j - 1) + 1 + w...
)), q))/(An);
end
end
end
end
% Case 4: Stretching the image along the horizontal axis, then compressing i
% t along the vertical one.
elseif Am < 1 & An > 1
for i = 1:x
for j = 1:y
for w = 0: (An -1)
for q = 1:z
intermediate_image(i, floor((An * (j - 1) + 1 + w)), q)...
= original_image(i, j, q);
intermediate_image(i, ceil((An * (j - 1) + 1 + w)), q) =...
original_image(i, j, q);
end
end
end
end
Am = 1/Am;
for i = 1:floor(x/Am)
for j = 1:floor(An * y)
for k = 0:(Am - 1)
for q = 1:z
amplified_image(i, j, q) = amplified_image(i, j, q) + ...
double(intermediate_image((Am * (i - 1) + 1 + k), j, q))
/(Am);
end
end
end
end
% Case 5: Retaining the image unchanged.
else
amplified_image = original_image;
end
% Displaying the result.
Show_image(amplified_image, original_image);
end
% Subfunctions are not provided,sicne they are very easily made.
--
Welcome to LilacPark in Literature Division of the BBS
Welcome to Accessing to our Online Literary Periodical
Lilac Rain
http://gaea.hit.edu.cn/lilacrain/index.asp (on campus)
http://www.lilacrain.net/ (outside)
※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 61.165.193.6]
※ 修改:·Christy 於 04月19日22:02:46 修改本文·[FROM: 61.165.193.6]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:3.191毫秒