图像放大有很多算法,我不知道你需要哪种。可以用matlab自己带的函数实现。
比如下面的函数是对输入图像x做放大,放大倍数是factor,放大方法(就是内插方法)是mode、可以是'cubic', 'linear'等
调用方法是
F = MyInterp2(x, 2, 'cubic');
function result = MyInterp2(x, factor, mode)
%x input data
%factor size of zoom
%mode the mode of interpolation
[nRowLR, nColumnLR] = size(x);
nRowHR = nRowLR factor;
nColumnHR = nColumnLR factor;
[X_LR, Y_LR] = meshgrid(1:factor:nColumnHR, 1:factor:nRowHR);
[X_HR, Y_HR] = meshgrid(1:nColumnHR, 1:nRowHR);
result = interp2(X_LR, Y_LR, x, X_HR, Y_HR, mode);
result(isnan(result))=0;
I = imread('demopng');
J = imresize(I, 50); %放大50倍
figure, imshow(I), figure, imshow(J);
imwrite(J,'c:\demobmp');
%% 4图像的缩放
% 思路:双线性插值
kV = 05; % 垂直缩放系数
kH = 05; % 水平缩放系数
% 读入图像
I = imread('D:\lenabmp'); % 注意修改文件路径
II = double(I);
[m, n, r]=size(II);
OI=zeros(mkV,nkH, r);
rot=[kV 0 0;0 kH 0;0 0 1]; % 变换矩阵
for k=1:r
for i=1:mkV
for j=1:nkH
pix=[i j 1]/rot;
Y =pix(1)-floor(pix(1));
X =pix(2)-floor(pix(2));
% 边界处理
if pix(1) < 1
pix(1) = 1;
end
if pix(1) > m
pix(1) = m;
end
if pix(2) < 1
pix(2) = 1;
end
if pix(2) > n
pix(2) =n;
end
% 四个邻点
pix1 = [floor(pix(1))floor(pix(2))];
pix2 = [floor(pix(1))ceil(pix(2))];
pix3 = [ceil(pix(1))floor(pix(2))];
pix4 = [ceil(pix(1))ceil(pix(2))];
% 计算临近四个点的权重
w1 = (1-X)(1-Y);
w2 = X(1-Y);
w3 = (1-X)Y;
w4 = XY;
% 按权重进行双线性插值
OI(i,j,k)=
w1II(pix1(1),pix1(2),k)+
w2II(pix2(1),pix2(2),k)+
w3II(pix3(1),pix3(2),k)+
w4II(pix4(1),pix4(2),k);
end
end
end
figure, imshow(uint8(II)), title('原图')
figure, imshow(uint8(OI)), title('缩放后')
根据矩阵计算:
平移旋转以后,还需要插值,才能显示完整的图像
clear all; close all; clc;img=imread('lenabmp');
[h w]=size(img);
%平移旋转参数
a = 10; b = 20;
theta = 30 /180pi;
% 旋转矩阵
rot=[cos(theta) -sin(theta) 0;
sin(theta) cos(theta) 0;
0 0 1];
% 平移矩阵
move = [ 1 0 a;
0 1 b;
0 0 1];
%平移加旋转
rot = rot move;
pix1=[a b 1]rot; % 左上点的坐标
pix2=[a w+b 1]rot; % 右上点的坐标
pix3=[h+a b 1]rot; % 左下点的坐标
pix4=[h+a w+b 1]rot; % 右下点的坐标
height = round(max([abs(pix1(1)-pix4(1)) abs(pix2(1)-pix3(1))])); %变换后图像的高度
width = round( max([abs(pix1(2)-pix4(2)) abs(pix2(2)-pix3(2))])); %变换后图像的宽度
imgn=zeros(height,width);
delta_y = abs(min([pix1(1) pix2(1) pix3(1) pix4(1)]));
delta_x = abs(min([pix1(2) pix2(2) pix3(2) pix4(2)]));
for i=1-delta_y:height
for j=1-delta_x:width
pix=[i j 1]/rot; %用变换后图像的点的坐标去寻找原图像点的坐标,
float_Y=pix(1)-floor(pix(1));
float_X=pix(2)-floor(pix(2));
if pix(1)>=1 && pix(2)>=1 && pix(1) <= h && pix(2) <= w
pix_up_left=[floor(pix(1)) floor(pix(2))]; %四个相邻的点
pix_up_right=[floor(pix(1)) ceil(pix(2))];
pix_down_left=[ceil(pix(1)) floor(pix(2))];
pix_down_right=[ceil(pix(1)) ceil(pix(2))];
value_up_left=(1-float_X)(1-float_Y);
value_up_right=float_X(1-float_Y);
value_down_left=(1-float_X)float_Y;
value_down_right=float_Xfloat_Y;
imgn(i+delta_y,j+delta_x) = value_up_leftimg(pix_up_left(1),pix_up_left(2))+
value_up_rightimg(pix_up_right(1),pix_up_right(2))+
value_down_leftimg(pix_down_left(1),pix_down_left(2))+
value_down_rightimg(pix_down_right(1),pix_down_right(2));
end
end
end
imshow(uint8(imgn));title('平移旋转并插值');
以上就是关于matlab编图像放大源程序全部的内容,包括:matlab编图像放大源程序、matlab 如何写程序将已经打开的figure图片放大、如何用MATLAB编写一个双线性插值的算法,用来放大缩小图象等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)