matlab中图像锐化程序怎么写,简单点,最好有注释。

 我来答
百度网友8d94651
2014-12-31 · TA获得超过168个赞
知道小有建树答主
回答量:123
采纳率:0%
帮助的人:102万
展开全部

%之前写的一个程序,这里面用Sobel算子和Prewitt算子的部分就是对图像锐化得到边缘的了。也可以直接用matlab自带的函数S = edge(I, 'sobel');进行锐化。

clc;
close all;
clear all;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%读入图像
f=imread('Pictures/4_m03.tif');
%f=imread('Pictures/5_m01.tif');
%f=imread('Pictures/10_m02.tif');
%f=imread('Pictures/22_m03.tif');
f=imresize(f,0.25);
f=im2double(f);
[m,n]=size(f);
subplot(2,3,1),imshow(f);
title('原始图像');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%生成高斯平滑滤波模板
hg=zeros(3,3);  %设定模板大小3*3
delta=0.5;
for x=1:1:3
    for y=1:1:3
        u=x-2;
        v=y-2;
        hg(x,y)=exp(-(u^2+v^2)/(2*pi*delta^2));
    end
end
h=hg/sum(hg(:));

%高斯滤波
ftemp=zeros(m,n);
rowhigh=m-1;
colhigh=n-1;
for x=2:1:rowhigh-1
    for y=2:1:colhigh-1
        mod=[f(x-1,y-1) f(x-1,y) f(x-1,y+1); f(x,y-1) f(x,y) f(x,y+1);f(x+1,y-1) f(x+1,y) f(x+1,y+1)];
        A=h.*mod;
        ftemp(x,y)=sum(A(:));
    end
end
f=ftemp;
subplot(2,3,4),imshow(f);
title('高斯滤波后的图像');
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%利用Sobel算子进行边缘检测
sx=[-1 0 1;-2 0 2;-1 0 1];
sy=[-1 -2 -1;0 0 0;1 2 1];
for x=2:1:rowhigh-1
    for y=2:1:colhigh-1
        mod=[f(x-1,y-1) f(x-1,y) f(x-1,y+1); f(x,y-1) f(x,y) f(x,y+1);f(x+1,y-1) f(x+1,y) f(x+1,y+1)];
        fsx=sx.*mod;
        fsy=sy.*mod;
        ftemp(x,y)=max((abs(sum(fsx(:)))),(abs(sum(fsy(:)))));
    end
end
fs=im2uint8(ftemp);
subplot(2,3,2),imshow(fs);
title('Sobel算子进行边缘检测的原始图像');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%阈值分割
TH1=140;  %设定阈值
for x=2:1:rowhigh-1
    for y=2:1:colhigh-1
        if (fs(x,y)>=TH1)&((fs(x,y-1) <= fs(x,y)) & (fs(x,y) > fs(x,y+1)) )
          fs(x,y)=200;
      elseif(fs(x,y)>=TH1)&( (fs(x-1,y) <=fs(x,y)) & (fs(x,y) >fs(x+1,y)))
          fs(x,y)=200;
      else fs(x,y)=50;
        end
    end
end
subplot(2,3,5),imshow(fs);
title('Sobel算子边缘检测并细化后的图像');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%利用Prewitt算子进行边缘检测
sx=[1 0 -1;1 0 -1;1 0 -1];
sy=[-1 -1 -1;0 0 0;1 1 1];
for x=2:1:rowhigh-1
    for y=2:1:colhigh-1
        mod=[f(x-1,y-1) f(x-1,y) f(x-1,y+1); f(x,y-1) f(x,y) f(x,y+1);f(x+1,y-1) f(x+1,y) f(x+1,y+1)];
        fsx=sx.*mod;
        fsy=sy.*mod;
        ftemp(x,y)=max((abs(sum(fsx(:)))),(abs(sum(fsy(:)))));
    end
end
fs=im2uint8(ftemp);
subplot(2,3,3),imshow(fs);
title('Prewitt算子进行边缘检测的原始图像');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%阈值分割
TH1=100;  %设定阈值
for x=2:1:rowhigh-1
    for y=2:1:colhigh-1
        if (fs(x,y)>=TH1)&((fs(x,y-1) <= fs(x,y)) & (fs(x,y) > fs(x,y+1)) )
          fs(x,y)=200;
      elseif(fs(x,y)>=TH1)&( (fs(x-1,y) <=fs(x,y)) & (fs(x,y) >fs(x+1,y)))
          fs(x,y)=200;
      else fs(x,y)=50;
        end
    end
end
subplot(2,3,6),imshow(fs);
title('Prewitt算子边缘检测并细化后的图像');


东莞大凡
2024-08-07 广告
OpenCV标定板是东莞市大凡光学科技有限公司在相机标定中常用的工具。它通常由黑白格点按一定规则排列在平面上组成,如棋盘格或圆形格等。在相机标定时,将标定板置于不同位置和姿态下拍摄图像,利用OpenCV库中的函数检测标定板上的角点或圆心,进... 点击进入详情页
本回答由东莞大凡提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式