求程序:灰度图像 椒盐噪声 matlab

求程序:灰度图像 椒盐噪声 matlab,第1张

均值滤波器程序送上。这个程序是我做边缘检测是写的,刚开始用高斯滤波器做均值滤波,LZ可以借鉴一下。程序送上~~~~

clc

close all

clear all

%%%生成高斯平滑滤波模板%%%

%%%%%%%%%%%%%%%%%%%%%%%%%

hg=zeros(3,3); %设定高斯平滑滤波模板的大小为33

delta=05;

for x=1:1:3

for y=1:1:3

u=x-2;

v=y-2;

hg(x,y)=exp(-(u^2+v^2)/(2pidelta^2));

end

end

h=hg/sum(hg(:));

%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%读入图像%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%

f = imread('1111tif'); % 读入图像文件

f=rgb2gray(im2double(f));

imshow(f)

title('原始图像');

[m,n]=size(f);

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=hmod;

ftemp(x,y)=sum(A(:));

end

end

f=ftemp

figure,imshow(f)

title('通过高斯滤波器后的图像');

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% %%%利用roberts算子进行边缘检测%%%

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

sx=[-1 -2 -1;0 0 0;1 2 1];

sy=[-1 0 1;-2 0 2;-1 0 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=sxmod;

fsy=symod;

ftemp(x,y)=sqrt((sum(fsx(:)))^2+(sum(fsy(:)))^2);

end

end

fr=im2uint8(ftemp);

figure,imshow(fr)

title('用roberts算子边缘检测的原始图像');

%%%域值分割%%%

TH1=60; %设定阈值

for x=2:1:rowhigh-1

for y=2:1:colhigh-1

if (fr(x,y)>=TH1)&((fr(x,y-1) <= fr(x,y)) & (fr(x,y) > fr(x,y+1)) )

fr(x,y)=200;

elseif(fr(x,y)>=TH1)&( (fr(x-1,y) <=fr(x,y)) & (fr(x,y) >fr(x+1,y)))

fr(x,y)=200;

else fr(x,y)=50;

end

end

end

figure,imshow(fr)

title('用roberts算子边缘检测并细化后的图像');

你的第一行命令写错了,应该写成如下形式:

I2=edge(img,'roberts',015,'both');

edge命令的参数中,指定罗伯特算子的参数是roberts,你少了一个s!

clc;

clear;

clf;

[FileName,PathName] = uigetfile('');%d出对话框得到打开的路径

rgb=imread(strcat(PathName,FileName));%打开得到的路径下的文件

cr=double(rgb(:,:,1));%将图像中的8位数据转换成浮点数

cg=double(rgb(:,:,2));%将图像中的8位数据转换成浮点数

cb=double(rgb(:,:,3));%将图像中的8位数据转换成浮点数

figure(1);mesh(cr);

figure(2);mesh(cg);

figure(3);mesh(cb);

gry=(cr-cg)+(cr-cb);

figure(4);mesh(gry);

[gv,t]=edge(gry,'sobel','vertical');%这里改变水平垂直,还可以改变其他方式提取

figure(20);

imshow(gv);

[m,n]=size(gv);

th=100;

lct=double(zeros(m));

for i=1:m

sumj=0;

for j=1:n

if cb(i,j)>th

gray=gry(i,j);

else

gray=00;

end

lct(i)=lct(i)+grayj;

sumj=sumj+j;

end

lct(i)=lct(i)/sumj;

end

figure(30);

plot(lct);

function ck

close all

clear all

I=imread('ckbmp'); %读取图像

subplot(3,3,1)

imshow(I) %显示原始图像

title('原始图像')

P1=imnoise(I,'gaussian',002) %加入高斯躁声

subplot(3,3,2)

imshow(P1) %加入高斯躁声后显示图像

title('加入高斯噪声后的图像');

I1=im2double(P1); %将彩图序列变成双精度

I2=rgb2gray(I1); %将彩色图变成灰色图

[thr, sorh, keepapp]=ddencmp('den','wv',I2);

I3=medfilt2(I2,[9 9]); %中值滤波

I4=wdencmp('gbl',I2,'sym4',2,thr,sorh,keepapp); %小波除噪

I5=imresize(I4,15,'bicubic'); %图像大小

BW1=edge(I5,'sobel'); %sobel图像边缘提取

BW2=edge(I5,'roberts'); %roberts图像边缘提取

BW3=edge(I5,'prewitt'); %prewitt图像边缘提取

BW4=edge(I5,'log'); %log图像边缘提取

BW5=edge(I5,'canny'); %canny图像边缘提取

h=fspecial('gaussian',5); %高斯滤波

BW6=edge(I5,'zerocross',[ ],h); %zerocross图像边缘提取

figure;

subplot(1,3,1); %图划分为一行三幅图,第一幅图

imshow(I2)%绘图

title('灰度图');

subplot(1,3,2);

imshow(I3)

title('中值滤波后图');

subplot(1,3,3);

imshow(I4)

title('小波除噪后图');

figure;

subplot(1,3,1);

imshow(BW1);

title('Sobel算子');

subplot(1,3,2);

imshow(BW2);

title('Roberts算子');

subplot(1,3,3);

imshow(BW3);

title('Prewitt算子');

save datI4 I4

ck1;

function ck1

load datI4 I4%导入数据I4

figure;

plot(I4)

U=I4;

save datU U%存入数据U

I4=I4-04438;

for i=1:166;

for j=1:189;

if I4(i,j)<=0

I4(i,j)=0;

else

I4(i,j)=1;

end

end

end

figure;

plot(I4)

B=sum (sum(I4))

上面是我毕业设计用到的一些程序,图像名字'ckbmp',我毕业设计也是关于图像处理的,不过跟你的方向不同,我是处理图像中的缺陷,希望对你有所帮助。

I=imread('lenabmp');% 提取图像

BW1=edge(I,'sobel'); %用SOBEL算子进行边缘检测

BW2=edge(I,'roberts');%用Roberts算子进行边缘检测

BW3=edge(I,'prewitt'); %用prewitt算子进行边缘检测

BW4=edge(I,'log'); %用log算子进行边缘检测

BW5=edge(I,'canny'); %用canny算子进行边缘检测

h=fspecial('gaussian’,5);

BW6=edge(I,’canny’);

subplot(2,3,1), imshow(BW1);

title(‘sobel edge check’);

subplot(2,3,2), imshow(BW2);

title(‘sobel edge check’);

subplot(2,3,3), imshow(BW3);

title(‘prewitt edge check’);

subplot(2,3,4), imshow(BW4);

title(‘log edge check’);

subplot(2,3,5), imshow(BW5);

title(‘canny edge check’);

subplot(2,3,6), imshow(BW6);

title(‘gasussian&canny edge check’);%此为用高斯滤波后Canny算子边缘检测结果

(注意:代码中有一些标点是中文模式,若输入代码后标点显示红色,则为中文标点,改回来就行了)

用mesh语句似乎可以,具体也不了解你的情况,感觉怪怪的,发一段我以前些的程序,用罗伯特算子写的,把算子一改就是sobel了。两种边缘检测近似算法奉上:

clc

close all

clear all

%%%生成高斯平滑滤波模板%%%

%%%%%%%%%%%%%%%%%%%%%%%%%

hg=zeros(3,3); %设定高斯平滑滤波模板的大小为33

delta=05;

for x=1:1:3

for y=1:1:3

u=x-2;

v=y-2;

hg(x,y)=exp(-(u^2+v^2)/(2pidelta^2));

end

end

h=hg/sum(hg(:));

%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%读入图像%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%

f = imread('1111tif'); % 读入图像文件

f=rgb2gray(im2double(f));

imshow(f)

title('原始图像');

[m,n]=size(f);

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=hmod;

ftemp(x,y)=sum(A(:));

end

end

f=ftemp

figure,imshow(f)

title('通过高斯滤波器后的图像');

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% %%%利用roberts算子进行边缘检测%%%

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

sx=[-1 -2 -1;0 0 0;1 2 1];

sy=[-1 0 1;-2 0 2;-1 0 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=sxmod;

fsy=symod;

ftemp(x,y)=sqrt((sum(fsx(:)))^2+(sum(fsy(:)))^2);

end

end

fr=im2uint8(ftemp);

figure,imshow(fr)

title('用roberts算子边缘检测的原始图像');

%%%域值分割%%%

TH1=60; %设定阈值

for x=2:1:rowhigh-1

for y=2:1:colhigh-1

if (fr(x,y)>=TH1)&((fr(x,y-1) <= fr(x,y)) & (fr(x,y) > fr(x,y+1)) )

fr(x,y)=200;

elseif(fr(x,y)>=TH1)&( (fr(x-1,y) <=fr(x,y)) & (fr(x,y) >fr(x+1,y)))

fr(x,y)=200;

else fr(x,y)=50;

end

end

end

figure,imshow(fr)

title('用roberts算子边缘检测并细化后的图像');

%%%%%%%%%%%%%%%%%%%%%%%%%%

利用第一种近似算法进行边缘检测%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%33的sobel算子%%%%%%%%

sx=[-1 -2 -1;0 0 0;1 2 1];

sy=[-1 0 1;-2 0 2;-1 0 1];

%sx=[0 1 2;-1 0 1;-2 -1 0];

%sy=[-2 -1 0;-1 0 1;0 1 2];

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=sxmod;

fsy=symod;

ftemp(x,y)=abs(sum(fsx(:)))+abs(sum(fsy(:)));

end

end

fs=im2uint8(ftemp);

figure,imshow(fs)

title('用第一种近似算法进行边缘检测的原始图像');

%%%域值分割%%%

TH2=200; %设定阈值

for x=2:1:rowhigh-1

for y=2:1:colhigh-1

if (fs(x,y)>=TH2)&((fs(x,y-1) <= fs(x,y)) & (fs(x,y) > fs(x,y+1)) )

fs(x,y)=200;

elseif(fs(x,y)>=TH2)&( (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

figure,imshow(fs)

title('采用第一种近似算法进行边缘检测后的图像');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%利用第二种近似算法进行边缘检测%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%33的sobel算子%%%%%%%%

sx=[-1 -2 -1;0 0 0;1 2 1];

sy=[-1 0 1;-2 0 2;-1 0 1];

%sx=[0 1 2;-1 0 1;-2 -1 0];

%sy=[-2 -1 0;-1 0 1;0 1 2];

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=sxmod;

fsy=symod;

ftemp(x,y)=max(abs(sum(fsx(:))),abs(sum(fsy(:))));

end

end

fs=im2uint8(ftemp);

figure,imshow(fs)

title('用第二种近似算法进行边缘检测的原始图像');

%%%域值分割%%%

TH2=200; %设定阈值

for x=2:1:rowhigh-1

for y=2:1:colhigh-1

if (fs(x,y)>=TH2)&((fs(x,y-1) <= fs(x,y)) & (fs(x,y) > fs(x,y+1)) )

fs(x,y)=200;

elseif(fs(x,y)>=TH2)&( (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

figure,imshow(fs)

title('采用第二种近似算法进行边缘检测后的图像');

以上就是关于求程序:灰度图像 椒盐噪声 matlab全部的内容,包括:求程序:灰度图像 椒盐噪声 matlab、matlab车牌识别运行I2=edge(img,'robert',0.15,'both');报错,高分求大神解决!、高手帮下忙用matlab进行图像增强跟图像分割实验!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zz/9842416.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存