需求说明:FPGA图像处理前期算法验证
当前状态:已通过matlab验证
针对FPGA优化后,图像出现锯齿,先实现功能再继续优化
matlab源码 参考文章《matlab图像处理基础知识0(双线性插值matlab实现--调整水平和垂直放大倍数)》
http://blog.csdn.net/times_poem/article/details/51499793
正文:
function scaler_bilinear_for_fpga()
%-----------------------------0:配置输入输出--------------------------------
m=2.25; %放大或缩小的高度
n=3; %放大或缩小的宽度
I=imread('F:\book\Digital image processing and machine vision\640480\plane.jpg');
[height,width,l]=size(I); %输入分辨率 480*640
O=zeros(height*m,width*n,l); %输出分辨率 1080*1920
%-----------------------------1:开始输出计算--------------------------------
ratio=[m,0,0;
0,n,0;
0,0,1]; %变换矩阵
ratio_rotate=[1,0,0;0,1,0;0,0,1]/ratio; %逆矩阵计算
ratio_rotate=[256,0,0;0,256,0;0,0,256]*ratio_rotate; %0:适配fpga
%采用定点小数方式,后8位为小数内容
ratio_rotate=uint16(ratio_rotate); %强制转换 四舍五入处理
ratio_rotate=double(ratio_rotate); %强制转换 方便矩阵浮点型相乘
for i=1:height*m
for j=1:width*n
pix=[i,j,1]*ratio_rotate; %前向放大 求原图坐标值xx
pix_xx(1)=bitshift(pix(1), -8); %1:适配fpga 得到原图坐标
pix_xx(2)=bitshift(pix(2), -8); %除以256(右移8位)再取整
float_Y =bitand( pix(1),255 ); %1:适配fpga 得到位置关系 计算权重
float_X =bitand( pix(2),255 ); %取低八位小数部分
%-----------------------------2:边界处理--------------------------------
if pix_xx(1)<=1 %边界处理
pix_xx(1)=1;
end
if pix_xx(1)>=height-1 %边界处理 避免坐标点超出范围
pix_xx(1)=height-1;
end
if pix_xx(2)<=1 %边界处理
pix_xx(2)=1;
end
if pix_xx(2)>=width-1 %边界处理 避免坐标点超出范围
pix_xx(2)=width-1;
end
%-----------------------------3:原坐标四个相邻点----------------------------
%四个相邻的点
pix_up_left = [pix_xx(1) , pix_xx(2)];
pix_up_right = [pix_xx(1)+ 1 , pix_xx(2)];
pix_down_left = [pix_xx(1) , pix_xx(2)+1];
pix_down_right= [pix_xx(1)+1 , pix_xx(2)+1];
%-----------------------------4:权重计算-----------------------------------
%计算临近四个点的权重 累加和为65536 本该是1 2^16 = 65535
value_up_left = (256-float_X) * (256-float_Y);
value_up_right = float_X * (256-float_Y);
value_down_left = (256-float_X) * float_Y;
value_down_right= float_X * float_Y;
c = value_up_left + value_up_right + value_down_left + value_down_right;
%按权重进行双线性插值
%-----------------------------5:计算输出图像数值----------------------------
O(i,j,1) = value_up_left * uint32(I(pix_up_left(1), pix_up_left(2) ,1)) +...
value_up_right * uint32(I(pix_up_right(1), pix_up_right(2) ,1)) +...
value_down_left * uint32(I(pix_down_left(1), pix_down_left(2) ,1)) +...
value_down_right* uint32(I(pix_down_right(1),pix_down_right(2),1));
O(i,j,1)=bitshift(O(i,j,1), -16); %右移16位
O(i,j,2) = value_up_left * uint32(I(pix_up_left(1), pix_up_left(2) ,2)) +...
value_up_right * uint32(I(pix_up_right(1), pix_up_right(2) ,2)) +...
value_down_left * uint32(I(pix_down_left(1), pix_down_left(2) ,2)) +...
value_down_right* uint32(I(pix_down_right(1),pix_down_right(2),2));
O(i,j,2)=bitshift(O(i,j,2), -16); %右移16位
O(i,j,3) = value_up_left * uint32(I(pix_up_left(1), pix_up_left(2) ,3)) +...
value_up_right * uint32(I(pix_up_right(1), pix_up_right(2) ,3)) +...
value_down_left * uint32(I(pix_down_left(1), pix_down_left(2) ,3)) +...
value_down_right* uint32(I(pix_down_right(1),pix_down_right(2),3));
O(i,j,3)=bitshift(O(i,j,3), -16); %右移16位
end
end
%-----------------------------6:输出显示------------------------------------
figure,imshow(I); title('输入图像640*480');
imwrite(uint8(O),'F:\book\Digital image processing and machine vision\640480\19201080.jpg');
figure,imshow(uint8(O));title('输出图像1920*1080');
clc
%--------------------------------------------------------------------------
图一:原图640*480
图二:matlab输出1920*1080
图三:针对FPGA优化后输出1920*1080
设计来自:时间的诗