一、简介
基于matlab拉氏滤波的图像质量提升
二、源代码
unction HighBoostFiltering
handle=figure;
set(handle,'unit','pixels','position',[0 0 1000 600]);
movegui(handle, 'center' );
img=imread('k.bmp');
[h w]=size(img);
filter_mask={... %定义俩个拉氏滤波器
[0 -1 0;-1 4 -1;0 -1 0];... %普通拉氏滤波器
[-1 -1 -1; -1 8 -1 ;-1 -1 -1]}; %扩展拉氏滤波器
A=1.7; %设定提升系数A值的大小
%% matlab 实现
imgsharp2=imfilter(double(img),...
filter_mask{1},...
'corr', ...
'symmetric',...
'same');
% imfilter函数:返回与输入图片的类型一样的处理图片;corr是相关,因为拉氏算子是中心对称的,所以做卷积和做相关是一样的;symmetric是镜像复制;same是相同尺寸;
imgafter2=double(img).*A+imgsharp2;
imgafter2=uint8(imgafter2);
% uint8强制类型转换:如果输入数据为double,则数据四舍五入,小于0的置为0。
subplot(241),imshow(img), title('此行为matla的imfilter实现的');
subplot(242),imshow(uint8(imgsharp2)), title('未标定的拉氏滤波图');
subplot(243),imshow(mat2gray(imgsharp2)), title('标定的拉氏滤波图');
subplot(244),imshow(imgafter2), title('最终图');
%% 自己写的算法
m=1;n=1;
imgMSR=EdgeFilling(img,m,n,1); %图像的边缘填充镜像的像素,因为拉氏算子是3*3,所以水平和垂直个填充一个像素单位
imgMSR=double(imgMSR); %扩展的图像转为double型
imgsharp1=imgMSR;
%拉氏算子是3*3,所以水平和垂直个填充一个像素单位,这里的m和n分别表示垂直和水平
hwait=waitbar(0,'请等待>>>>>>>>');
for i=1+m:h+m
for j=1+n:w+n
[p,q,r,t]=safeborder(h+2*m,w+2*n,i,j,m,n);
tempimg=imgMSR(p:q,r:t);
imgsharp1(i,j)=sum(sum(tempimg.*filter_mask{1}));
end
if i/h>0.8
set(findobj(hwait,'type','patch'),'edgecolor','g','facecolor','g');
end
waitbar(i/h,hwait,'正在处理');
end
imgsharp1=imgsharp1(1+m:h+m,1+n:w+n); % 取中间部分,去掉边沿
imgafter1=double(img).*A+imgsharp1;
imgafter1=uint8(imgafter1);
close(hwait);
subplot(245),imshow(img), title('此行为自己写的代码处理的');
subplot(246),imshow(uint8(imgsharp1)), title('未标定的拉氏滤波图');
subplot(247),imshow(mat2gray(imgsharp1)), title('标定的拉氏滤波图');
subplot(248),imshow(imgafter1), title('最终图');
%% 总结:
% 最后发现,高提升滤波除了细节的提升外,还有亮度的提升,取决于A的值。
% 高提升滤波非常适合低灰度图像处理
function imgMSR=EdgeFilling(img,m,n,which)
% 对图像进行边缘填充,
% 填充方案有:
% 镜像复制which=1、填充黑色which=2、填充灰色which=3、填充白色which=4
[h w]=size(img);
if which==1
fill=img;
elseif which==2
fill=uint8(zeros(h,w));
elseif which==3
fill=uint8(ones(h,w)*127);
elseif which==4
fill=uint8(ones(h,w)*255);
end
三、运行结果
四、备注
版本:2014a