matlab数字图像处理期末实习作业代码 图像增强(多种方法) 彩色图像处理 边缘检测 形态学处理 图像分割

clc,clear,close all
imfinfo(‘001.bmp’);
I=imread(‘001.bmp’);
I1=im2double(I);
figure();
subplot(221)
imshow(I);
title(‘原始图像’)

%旋转图像
%双线性插值法(bilinear)旋转30°,并剪切图像,使得到的图像和原图像大小一致(crop)
a1=imrotate(I1,15,’bilinear’,’crop’);
a2=imrotate(I1,90,’bilinear’,’crop’);           
a3=imrotate(I1,180,’bilinear’,’crop’);
subplot(222)
imshow(a1);
title(‘旋转15°’);
subplot(223)
imshow(a2);
title(‘旋转90°’);
subplot(224)
imshow(a3);
title(‘旋转180°’);

%绘制图像直方图
I1=im2double(I);
I2=rgb2gray(I1);
figure()
imhist(I2);
title(‘图像直方图’);

%图像增强
figure()
subplot(221)
imshow(I);
title(‘原始图像’)
%分段线性变换增强
[M,N]=size(I2);
for x = 1:M
    for y = 1:N
        if I2(x,y)<=0.2          %a=0.2,a’=0.2
            I2(x,y)=I1(x,y);
        elseif I2(x,y)<=0.6      %b=0.6,b’=0.8
            I2(x,y)=(0.8-0.2)/(0.6-0.2)*( I2(x,y)-0.2)+0.2;
        else                     %M’=1,M=0.96
            I2(x,y)=(1-0.2)/(0.96-0.2)*( I2(x,y)-0.6)+0.8;
        end
    end
end
subplot(222)
imshow(I2)
title(‘分段线性变换增强’);
%对数变换增强
I1=im2double(I);
[M,N]=size(I1);
for x = 1:M
    for y = 1:N
        I1(x,y)=2*log(I1(x,y)+1);
    end
end
subplot(223)
imshow(I1)
title(‘对数变换增强’);

%对比度增强
I1=im2double(I);
I2=imadjust(I1,[0.3 0.7],[0 1],1);
subplot(224)
imshow(I2)
title(‘对比度增强图像’)

%均值滤波器平滑增强图像
I1=im2double(I);
%生成滤波器,guassian, average, disk, laplacian, prewitt
w=fspecial(‘average’,[3 3]);
%‘replicate’  图像大小通过复制外边界的值来扩展
%‘symmetric’  图像大小通过镜像反射其边界来扩展
%‘circular’  图像大小通过将图像看成是一个二维周期函数的一个周期来扩展
g=imfilter(I1,w,’replicate’);
figure()
subplot(221)
imshow(I);
title(‘原始图像’)
subplot(222)
imshow(g)
title(‘均值滤波器平滑增强图像’);

%laplacian锐化增强图像
I1=im2double(I);
w=fspecial(‘laplacian’,0);
g=I1+imfilter(I1,w,’replicate’);
subplot(223)
imshow(g)
title(‘laplacian锐化增强图像’);
w2=[-1 -1 -1;-1 8 -1;-1 -1 -1];
g2=I1-imfilter(I1,w2,’replicate’);
subplot(224)
imshow(g2)
title(‘laplacian锐化增强图像’);

%彩色图像增强
%提取出彩色图像r g b 3个层
fr=I2(:,:,1);
fg=I2(:,:,2);
fb=I2(:,:,3);
%分别对不同层进行中值滤波去噪
fr_medfilt=medfilt2(fr);
fg_medfilt=medfilt2(fg);
fb_medfilt=medfilt2(fb);
%将处理后的彩色3层合并
fc_medfilt=cat(3,fr_medfilt,fg_medfilt,fb_medfilt);

%边缘检测
%Sobel 和 Prewitt 方法可以检测垂直方向和/或水平方向的边缘。
%Roberts 方法可以检测与水平方向成 45 度角和/或 135 度角的边缘。
I2=rgb2gray(fc_medfilt);
BW1=edge(I2,’sobel’);
BW2=edge(I2,’prewitt’);
BW3=edge(I2,’roberts’);
BW4=edge(I2,’canny’);
figure()
subplot(231)
imshow(fc_medfilt)
title(‘彩色处理滤波图像’)
subplot(232)
imshow(I2)
title(‘灰度图像’)
subplot(233)
imshow(BW1)
title(‘sobel边缘检测’)
subplot(234)
imshow(BW2)
title(‘prewitt边缘检测’)
subplot(235)
imshow(BW3)
title(‘roberts边缘检测’)
subplot(236)
imshow(BW4)
title(‘canny边缘检测’)

%图像形态学处理,腐蚀和膨胀
%腐蚀
%strel:运用各种形状和大小构造元素
%diamond菱形结构元素,disk圆形结构元素,line线性结构元素,octagon八边形结构元素
%pair和periodicline平坦结构元素,rectangel矩形结构元素,square方形结构元素
se1=strel(‘disk’,5,4);%0,4,6,8
se2=strel(‘diamond’,5);
se3=strel(‘square’,5);
%膨胀
A1=imdilate(fc_medfilt,se1);
A2=imdilate(fc_medfilt,se2);
A3=imdilate(fc_medfilt,se3);
figure()
subplot(221)
imshow(fc_medfilt)
title(‘彩色处理滤波图像’)
subplot(222)
imshow(A1)
title(‘disk’)
subplot(223)
imshow(A2)
title(‘diamond’)
subplot(224)
imshow(A3)
title(‘square’)

%图像分割
%全阈值分割
%imcomplement():二值图像颜色取反
I1=rgb2gray(I);
f=im2double(I1);
T2=graythresh(f);
g1=imcomplement(im2bw(f,T2));
figure()
subplot(131);
imshow(I1);
title(‘灰度图像’);
subplot(132);
imshow(g1);
title(‘阈值分割图像’);
%迭代方法
T=0.5*(double(min(f(:)))+double(max(f(:))));
done=false;
while(~done)
    g=f>=T;
    Tnext=0.5*(mean(f(g))+mean(f(~g)));
    done=abs(T-Tnext)<0.5;T=Tnext;
end
g=imcomplement(im2bw(f,T));
subplot(133);
imshow(g);
title(‘阈值分割图像’);
 

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2022年5月26日
下一篇 2022年5月26日

相关推荐