图像分割技术
①提取线段
close all;clear all;clc;
I=imread('E:\Matlab_exercise\图片素材\xianduan.jpg');
I=rgb2gray(I);
h1=[-1 -1 -1;2 2 2;-1 -1 -1];%横线
h2=[-1 -1 2;-1 2 -1;2 -1 -1];%45°斜线
h3=[-1 2 -1;-1 2 -1;-1 2 -1];%竖线
h4=[2 -1 -1;-1 2 -1;-1 -1 2];%135°斜线
J1=imfilter(I,h1);
J2=imfilter(I,h2);
J3=imfilter(I,h3);
J4=imfilter(I,h4);
J=J1+J2+J3+J4;
figure;
subplot(121);imshow(I);
subplot(122);imshow(J);
②微分算子
%Roberts算子
close all;clear all;clc;
I=imread('E:\Matlab_exercise\图片素材\gray.jpg');
I=im2double(I);
[J,thresh]=edge(I,'roberts',35/255);%边缘检测,采用Roberts算子,图像分割阈值为35/255
figure;
subplot(121);imshow(I);
subplot(122);imshow(J);
%prewitt算子
close all;clear all;clc;
I=imread('E:\Matlab_exercise\图片素材\gray.jpg');
I=im2double(I);
[J,thresh]=edge(I,'prewitt',[],'both');%边缘检测,水平和垂直边缘都检测
figure;
subplot(121);imshow(I);
subplot(122);imshow(J);
%sobel算子
%eg.1
close all;clear all;clc;
I=imread('E:\Matlab_exercise\图片素材\gray.jpg');
I=im2double(I);
[J,thresh]=edge(I,'sobel',[],'horizontal');%边缘检测,检测水平边缘
figure;
subplot(121);imshow(I);
subplot(122);imshow(J);
%eg.2
close all;clear all;clc;
I = imread('E:\Matlab_exercise\图片素材\lenn.jpg');
subplot(241);imshow(I,[]);title('原图');
J1 = edge(I,'sobel',0.06);
subplot(242);imshow(J1,[]);title('sobel 0.06');
J2 = edge(I,'sobel',0.09);</