% 首先读取图片 图片的命名方式按照功能 clear; clc; SourceImage=imread('c:\img.jpg'); AverageImage= zeros( size(SourceImage),'uint8'); BwImage= zeros( size(SourceImage),'uint8'); EageImage= zeros( size(SourceImage),'uint8'); ErodeImage= zeros( size(SourceImage),'uint8'); DilateImage= zeros( size(SourceImage),'uint8'); OpenImage= zeros( size(SourceImage),'uint8'); CloseImage= zeros( size(SourceImage),'uint8');
%图像平滑化【均值滤波 中值滤波等】 AverageImage=filter2( fspecial('average',3),SourceImage);
%AverageImage=medfilt2( SourceImage );
%灰度阈值获取 uint8 averagelevel; average_level=( ( min(SourceImage(:))+max( SourceImage(:)) )/2 ); graythresh_level = graythresh(SourceImage);
%灰度图像2值化处理 uint8 level_chose; double level;
level_chose=1;
switch level_chose case 0 level=average_level/255; case 1 level=graythresh_level; otherwise end
%BwImage=im2bw(SourceImage,level); %二值化处理 BwImage=im2bw( uint8(AverageImage) ,level); %二值化处理
%图片形态学处理 se=strel('square',5);
DilateImage=imdilate( BwImage,se); ErodeImage=imerode( BwImage,se);
CloseImage=imclose( BwImage,se); OpenImage=imopen( BwImage,se);
%边缘提取 EageImage=edge(BwImage,'canny');
%显示处理结果
subplot(241); imshow(SourceImage); title('Source Image');
subplot(242); imshow( uint8(AverageImage) ); title('图像平滑后');
subplot(243); imshow(BwImage); title('Bw Image');
subplot(244); imshow(EageImage); title('边缘提取');
subplot(245); imshow(ErodeImage); title('ErodeImage');
subplot(246); imshow(DilateImage); title('DilateImage');
subplot(247); imshow(OpenImage); title('OpenImage');
subplot(248); imshow(CloseImage); title('CloseImage');
|