图5.15 拉普拉斯锐化
实现锐化功能的相关代码如下:
function Gradient_Callback(hObject, eventdata, handles)
% hObject handle to Gradient (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
tic;
f=handles.imdata;
[row,col]=size(f);
prompt={'Threshold(0~255):'};
name='Input for Gradient Sharping';
numlines=1;
defaultanswer={'10'};
anss=inputdlg(prompt,name,numlines,defaultanswer);
th=str2num(anss{1});
f1=double(f);
f2=zeros(row,col);
for x=2:(row-1)
for y=2:(col-1)
g=abs(f1(x,y)-f1(x+1,y))+abs(f1(x,y)-f1(x,y+1));
if g>=th
f2(x,y)=g;
else
f2(x,y)=f1(x,y);
end
end
end
Time=toc;
set(handles.edit1,'string',Time);
extendx=double(zeros(256));
if (row<=256)&(row<=256)
W=256;
else
W=max(row,col);
end
for m=1:W
for n=1:W
if (m<=row)&(n<=col)
extendx(m,n)=f2(m,n);
else
extendx(m,n)=realmax;
end
end
end
axes(handles.axes2);
imshow(extendx,[min(min(f2)),max(max(f2))]);
handles.imdata=f2;
guidata(hObject, handles);
5.6 图像分割
5.6.1 阈值分割
所谓阈值的方法实质是利用图像的灰度直方图信息得到用于分割的阈值。基于阈值的分割方法可以分为全阈值的方法和局部阈值的方法。所谓全阈值的方法是利用整幅图像的灰度信息,从整幅图像中得到用于分割的阈值,并且根据该阈值对图像进行分割;而局部阈值的方法是根据图像中不同区域获得对应不同区域的几个阈值,利用这些得的阈值对图像进行分割。本系统中实现阈值分割的主要代码如下所示:
function GrayThreshold_Callback(hObject, eventdata, handles)
tic;
x=handles.imdata;
N=256;
[row,col]=size(x);
th=0;
p=zeros(N);
for i=1:row*col
p(fix(x(i)+1))=p(fix(x(i)+1))+1;
end
p1=zeros(N);
p1(1)=p(1);
for i=2:N
p1(i)=p(i)-p(i-1);
end
p2=zeros(N);
p2(1)=p1(1);
for i=2:N
p2(i)=p1(i)-p1(i-1);
end
for i=1:N
if p2(i)>=0
th=p2(i);
end
end
for i=1:row*col;
if x(i)>th
x(i)=256;
else
x(i)=0;
end
end
Time=toc;
set(handles.edit1,'string',Time);
if (row<=256)&(row<=256)
W=256;
else
W=max(row,col);
end
extendx=double(zeros(W));
for m=1:W 基于MATLAB的图像编辑软件开发(17):http://www.751com.cn/jisuanji/lunwen_541.html