MATLAB数字图像处理实验三

MATLAB数字图像处理实验三

实验一:用点检测模板检测图像中的点;
实验二:用线检测模板检测图像中的线,并检测图像中120°方向的线;
实验三:检测并显示图像用Sobel、Prewitt、Roberts、LoG及Canny边缘检测的结果;
实验四:对图像加标准差为20的高斯白噪声,重做2、3;
实验五:对图像进行hough变换,在图中画出检测到的最长直线

%%
%%点检测
f=imread('hei.png');
I1=rgb2gray(f);
w=[-1 -1 -1;-1 8 -1;-1 -1 -1];%点检测掩模
gi=abs(imfilter(double(I1),w));
T=max(gi(:));
gi=gi>=T;
subplot(211);imshow(I1);title('原图像');
subplot(212);imshow(gi);title('点检测');
%%
%%线检测
f=imread('hei.png');
I=rgb2gray(f);
w=[2 -1 -1;-1 2 -1;-1 2 -1];
g=imfilter(double(I),w);
subplot(2,1,1);imshow(f);title('原图像');
subplot(2,1,2);imshow(g,[]);title('线检测120°方向');
%%
%边缘检测
I=imread('hei.png');
I=rgb2gray(I);
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 算子进行滤波
subplot(231),imshow(I),title('原图');
subplot(232),imshow(BW1),title('Sobel算子边缘检测');
subplot(234),imshow(BW2),title('Roberts算子');
subplot(233),imshow(BW3),title('Priwitt算子');
subplot(235),imshow(BW4),title('LOG算子');
subplot(236),imshow(BW5),title('Canny算子');
%%
%添加20的标准差gaussian噪音
I=imread('hei.png');
f=imnoise(I,'gaussian',0,20);
I1=rgb2gray(f);
w=[-1 -1 -1;-1 8 -1;-1 -1 -1];%点检测掩模
gi=abs(imfilter(double(I1),w));
T=max(gi(:));
gi=gi>=T;
subplot(211);imshow(I1);title('原图像');
subplot(212);imshow(gi);title('点检测');


%%
%添加20的标准差gaussian噪音
I=imread('hei.png');
I=rgb2gray(I);
f=imnoise(I,'gaussian',0,20);
w=[2 -1 -1;-1 2 -1;-1 2 -1];
g=imfilter(double(f),w);
subplot(2,1,1);imshow(f);title('原图像');
subplot(2,1,2);imshow(g,[]);title('线检测120°方向');
%%%%Hougn变换
I=imread('hei.png');
f=rgb2gray(I);%RGB-->gray
f=f(round(end/2):end,1:round(end/2));
BW=edge(f,'canny');%edge:以灰度图像为输入,'canny'为边缘检测算子,
                   %%输出BW为二值图像,边缘处为白(255)其余部分为黑(0)
subplot(211),imshow(f),title('原始图像');
[row,col]=size(BW);
rhomax=round((row*row+col*col)^0.5);
A=zeros(2*rhomax,180);   %这里,实际上rho的取值范围为[-rhomax,rhomax],
                         %但是为了后面进行数量统计,转变为[1,2rhomax]
for m=1:row
    for n=1:col
        if BW(m,n)>0 %判断为边缘
            for theta=1:180
                r=theta/180*pi; %角度转换
                rho=round(m*cos(r)+n*sin(r));
                %Hough变换
                rho=rho+rhomax+1;   %坐标平移
                                    %这里的理解,首先matlab中数组是从1开始计数,所以+1;
                                    %数组坐标不能<0,所以 +rhomax
                A(rho,theta)=A(rho,theta)+1;   %数量统计
            end
        end
    end
end
[rho,theta]=find(A>130);   %超过130个点视为共线,rho列号,theta行号
nma=length(rho);
subplot(212),imshow(BW);
for i=1:nma
    hold on
    m=1:row;
    r=theta(i)/180*pi;
    n=(rho(i)-rhomax-m*cos(r))/(0.0001+sin(r));
    plot(n,m,'w-','LineWidth',6);
end
title('hough线检测');
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值