小波多尺度边缘检测:
- clc,clear,close all;clear all;
- warning off
- I = imread('lena.jpg');
- % I = imread('lion.jpg');
- if size(I,3)>1
- I = rgb2gray(I);
- end
- h = [0.125,0.375,0.375,0.125]; % 滤波掩膜
- gx = [0.5,-0.5]; % 水平掩膜
- gy = [0.5;-0.5]; % 垂直掩膜
- imfilter_iter(:,:,:,1) = conv2(h,h,I,'same'); % 图像滤波
- dx(:,:,:,1) = conv2(I,gx,'same'); % X方向卷积
- dy(:,:,:,1) = conv2(I,gy,'same'); % Y方向卷积
- figure('color',[1,1,1])
- subplot(131),imshow(I,[]),title('original image')
- subplot(132),imshow(dx(:,:,:,1),[]),title('X方向卷积')
- subplot(133),imshow(dy(:,:,:,1),[]),title('Y方向卷积')
- imfilter_norm(:,:,:,1) = sqrt(dx(:,:,:,1).^2+dy(:,:,:,1).^2); % 模
- % 阈值分割
- % threshold = graythresh(imfilter_norm(:,:,:,1));
- % imfilter_bw(:,:,:,1) = im2bw(imfilter_norm(:,:,:,1), threshold);
- imfilter_bw(:,:,:,1) = imfilter_norm(:,:,:,1) > 5;
- S = 3; % 尺度
- for i=1:S
- imfilter_iter(:,:,:,i+1) = conv2(h,h,imfilter_iter(:,:,:,i),'same'); % 图像滤波
- dx(:,:,:,i+1) = conv2(imfilter_iter(:,:,:,i),gx,'same'); % X方向卷积
- dy(:,:,:,i+1) = conv2(imfilter_iter(:,:,:,i),gy,'same'); % Y方向卷积
- imfilter_norm(:,:,:,i+1) = sqrt(dx(:,:,:,i+1).^2+dy(:,:,:,i+1).^2); % 模
- % 阈值分割
- % threshold = graythresh(imfilter_norm(:,:,:,i+1));
- % imfilter_bw(:,:,:,i+1) = im2bw(imfilter_norm(:,:,:,i+1), threshold);
- imfilter_bw(:,:,:,i+1) = imfilter_norm(:,:,:,i+1) > 5;
- end
- figure('color',[1,1,1])
- subplot(321),imshow(I,[]),title('original image')
- subplot(322),imshow(imfilter_norm(:,:,:,1),[]),title('小波多尺度边缘检测--滤波图像模')
- subplot(334),imshow(imfilter_norm(:,:,:,2),[]),title('小波多尺度边缘检测--S=1图像模')
- subplot(335),imshow(imfilter_norm(:,:,:,3),[]),title('小波多尺度边缘检测--S=2图像模')
- subplot(336),imshow(imfilter_norm(:,:,:,4),[]),title('小波多尺度边缘检测--S=3图像模')
- subplot(337),imshow(imfilter_bw(:,:,:,2),[]),title('小波多尺度边缘检测--S=1图像边缘')
- subplot(338),imshow(imfilter_bw(:,:,:,3),[]),title('小波多尺度边缘检测--S=2图像边缘')
- subplot(339),imshow(imfilter_bw(:,:,:,4),[]),title('小波多尺度边缘检测--S=3图像边缘')
- figure('color',[1,1,1])
- subplot(121),imshow(imfilter_norm(:,:,:,4),[]),title('小波多尺度边缘检测--S=3图像模')
- subplot(122),imshow(imfilter_bw(:,:,:,4),[]),title('小波多尺度边缘检测--S=3图像边缘')
复制代码
|