Matlab digital image processing end-of-term internship code image enhancement (multiple methods) color image processing edge detection morphological processing image segmentation

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

%Rotate image
%Bilinear interpolation method (bilinear) rotates 30°, and cuts the image so that the resulting image is the same size as the original image (crop) a1=
imrotate(I1,15,'bilinear','crop');
a2=imrotate(I1,90,'bilinear','crop');           
a3=imrotate(I1,180,'bilinear','crop');
subplot(222)
imshow(a1);
title('rotate 15°' );
subplot(223)
imshow(a2);
title('rotate 90°');
subplot(224)
imshow(a3);
title('rotate 180°');

%Draw image histogram
I1=im2double(I);
I2=rgb2gray(I1);
figure()
imhist(I2);
title('image histogram');

%Image enhancement
figure()
subplot(221)
imshow(I);
title('Original image')
%Piecewise linear transformation enhancement
[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('Piecewise linear transformation enhancement');
% Logarithmic transformation enhancement
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('logarithmic transformation enhanced');

% contrast enhancement
I1=im2double(I);
I2=imadjust(I1,[0.3 0.7],[0 1],1);
subplot(224)
imshow(I2)
title('contrast enhanced image')

% mean filter smoothen enhanced image
I1=im2double(I);
% generate filter, guassian, average, disk, laplacian, prewitt
w=fspecial('average',[3 3]);
%'replicate' image size by copying Extend
the %'symmetric' image size by mirroring its borders by the value of the outer boundary. Extend
the %'circular' image size by treating the image as a period of a two-dimensional periodic function
g=imfilter(I1,w,' replicate');
figure()
subplot(221)
imshow(I);
title('Original Image')
subplot(222)
imshow(g)
title('Mean Filter Smooth Enhanced Image');

%laplacian sharpen and enhance image
I1=im2double(I);
w=fspecial('laplacian',0);
g=I1+imfilter(I1,w,'replicate');
subplot(223)
imshow(g)
title(' laplacian sharpening enhanced image');
w2=[-1 -1 -1;-1 8 -1;-1 -1 -1];
g2=I1-imfilter(I1,w2,'replicate');
subplot(224 )
imshow(g2)
title('laplacian sharpened image');

%Color image enhancement
%Extract three layers of color image rgb
fr=I2(:,:,1);
fg=I2(:,:,2); fb
=I2(:,:,3);
Perform median filtering to denoise layers
fr_medfilt=medfilt2(fr);
fg_medfilt=medfilt2(fg);
fb_medfilt=medfilt2(fb);
%Merge the processed color 3 layers
fc_medfilt=cat(3,fr_medfilt,fg_medfilt,fb_medfilt);

%Edge Detection
%Sobel and Prewitt methods can detect vertical and/or horizontal edges.
The %Roberts method can detect edges at 45-degree angles and/or 135-degree angles from the horizontal.
I2=rgb2gray(fc_medfilt);
BW1=edge(I2,'sobel');
BW2=edge(I2,'prewitt');
BW3=edge(I2,'roberts');
BW4=edge(I2,


















% Image Morphological Processing, Erosion and Dilation
% Corrosion
%strel: use various shapes and sizes to construct elements
%diamond rhombus structuring elements, disk circular structuring elements, line linear structuring elements, octagon octagonal structuring elements
%pair and periodicline flat Structural element, rectangle structural element, square square structural element
se1=strel('disk',5,4);%0,4,6,8
se2=strel('diamond',5);
se3=strel('square ',5);
% expansion
A1=imdilate(fc_medfilt,se1);
A2=imdilate(fc_medfilt,se2);
A3=imdilate(fc_medfilt,se3);
figure()
subplot(221)
imshow(fc_medfilt)
title('color processing filter image')
subplot(222)
imshow(A1)
title('disk')
subplot(223)
imshow(A2)
title('diamond')
subplot(224)
imshow(A3)
title('square')

%image segmentation
%full threshold segmentation
%imcomplement(): binary image color inversion
I1=rgb2gray(I);
f=im2double(I1);
T2=graythresh(f);
g1=imcomplement(im2bw(f,T2)) ;
figure()
subplot(131);
imshow(I1);
title('grayscale image');
subplot(132);
imshow(g1);
title('threshold segmentation image');
% iteration method
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('Threshold segmentation image');
 

Guess you like

Origin blog.csdn.net/zcs2312852665/article/details/124946165