需求说明:FPGA图像处理前期算法验证
当前状态:已通过matlab验证
%当前算法只能等比例放大或者缩小
function scaler_bilinear_matlab()
%-----------------------------0:配置输入输出--------------------------------
I=imread('F:\book\Digital image processing and machine vision\640480\plane.jpg');%读入原图像
[width_sr,height_sr,l]=size(I); %读取图像矩阵大小,方便后面操作
K = str2double(inputdlg('please input scale factor (must between 0.2 - 5.0)', 'INPUT scale factor', 1, {'0.5'}));
width = K * width_sr;
height = K * height_sr;
O = uint8(zeros(width,height,l));
widthScale = width_sr/width; %横向放大系数
heightScale = height_sr/height; %纵向放大系数
%-----------------------------1:开始输出计算--------------------------------
for x = 5:width - 5 % 5是为了防止矩阵超出边界溢出
for y = 5:height - 5
xx = x * widthScale; % xx, yy为原坐标,x,y为新坐标
yy = y * heightScale;
%-----------------------------2:边界处理-----------------------------------
if (xx/double(uint16(xx)) == 1.0) & (yy/double(uint16(yy)) == 1.0)
O(x,y,:) = I(int16(xx),int16(yy),:); % 若xx,yy为整数,直接赋值
else
%-----------------------------3:原坐标四个相邻点----------------------------
a = double(uint16(xx)); % xx, yy为原坐标,x,y为新坐标
b = double(uint16(yy)); % xx, yy为原坐标,x,y为新坐标
x11 = double(I(a, b, :)); % x11 <- I(a,b)
x12 = double(I(a, b+1,:)); % x12 <- I(a,b+1)
x21 = double(I(a+1,b, :)); % x21 <- I(a+1,b)
x22 = double(I(a+1,b+1,:)); % x22 <- I(a+1,b+1)
%-----------------------------4:计算输出图像数值----------------------------
O(x,y,:) = uint8( (b+1-yy) * ( (xx-a)*x21 + (a+1-xx)*x11 ) + (yy-b) * ( (xx-a)*x22 + (a+1-xx) * x12) );% 用双线性插值计算公式计算
end
end
end
%-----------------------------6:输出显示------------------------------------
figure,imshow(I); title('输入图像640*480');
figure,imshow(uint8(O));title('输出图像1920*1080');
设计来自:时间的诗