欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

图像预处理——matlab

程序员文章站 2022-07-13 08:52:47
...

参考此书:数字图像处理与机器视觉,第二版,张铮

—————————————————————————————————————
%图像预处理
RGBIm = imread(‘lena.jpg’);%读入图片
GRAYImage = rgb2gray(RGBIm);%图像灰度转换
threshold = graythresh(GRAYImage);%阈值
BW = im2bw(RGBIm,threshold);%二值化图像
imshow(BW );
—————————————————————————————————————

%%
%图像反转
RGBIm = imread(‘lena.jpg’);%读入图片
doubleimage = double(RGBIm);
doubleimage = -doubleimage +256-1;
H = uint8(doubleimage);
subplot(1,2,1);imshow(doubleimage);
subplot(1,2,2);imshow(H);

—————————————————————————————————————

%%
%直方图均衡
RGBIm = imread(‘lena.jpg’);%读入图片
gray = rgb2gray(RGBIm);
figure;
subplot(2,2,1);imshow(gray);
subplot(2,2,2);imhist(gray);%获得直方图
histIm = histeq(gray);%直方图均衡后的图片
subplot(2,2,3);imshow(histIm);
subplot(2,2,4);imhist(histIm);%获得直方图

—————————————————————————————————————

%%
%线性平滑滤波器
RGBIm = imread(‘lena.jpg’);%读入图片
gray = rgb2gray(RGBIm);
noiseIm = imnoise(gray,‘salt & pepper’,0.02);%添加椒盐噪声
K1 = filter2(fspecial(‘average’,3),noiseIm)/255; %进行33模板平滑滤波
K2 = filter2(fspecial(‘average’,5),noiseIm)/255; %进行5
5模板平滑滤波
%K3 = filter2(fspecial(‘average’,7),noiseIm)/255;%进行77模板平滑滤波
%K4 = filter2(fspecial(‘average’,9),noiseIm)/255;%进行9
9模板平滑滤波
K3 = filter2(fspecial(‘gaussian’),noiseIm)/255; %进行gaussian滤波
K4 = filter2(fspecial(‘prewitt’),noiseIm)/255; %用于边缘增强,大小为【3 3】,无参数
figure;
subplot(2,3,1);imshow(RGBIm);title(‘原始图像’);
subplot(232);imshow(noiseIm);title(‘添加椒盐噪声’);
subplot(233),imshow(K1);title(‘33模板平滑滤波’);
subplot(234),imshow(K2);title('5
5模板平滑滤波’);
subplot(235),imshow(K3);title(‘高斯滤波’);
subplot(236),imshow(K4);title(‘prewitt滤波’);

—————————————————————————————————————

%%
%中值滤波器
RGBIm = imread(‘lena.jpg’);%读入图片
gray = rgb2gray(RGBIm);
noiseIm = imnoise(gray,‘salt & pepper’,0.02);
K3 = medfilt2(noiseIm);%进行33模板中值滤波
K5 = medfilt2(noiseIm,[5,5]);%进行5
5模板中值滤波
K7 = medfilt2(noiseIm,[7,7]);%进行77模板中值滤波
K9 = medfilt2(noiseIm,[9,9]);%进行9
9模板中值滤波
figure;
subplot(231);imshow(RGBIm);title(‘原始图片’);
subplot(232);imshow(noiseIm);title(‘添加椒盐图片’);
subplot(233);imshow(K3);title(‘33模板中值滤波’);
subplot(234);imshow(K5);title('5
5模板中值滤波’);
subplot(235);imshow(K7);title(‘77模板中值滤波’);
subplot(236);imshow(K9);title('9
9模板中值滤波’);

—————————————————————————————————————

%%
%Sobel算子和拉普拉斯算子
RGBIm = imread(‘lena.jpg’);
gray = rgb2gray(RGBIm);
threshold = graythresh(gray);%阈值
BW = im2bw(RGBIm,threshold);

%选择sobel算子,用于边缘提取
fs_sobel = fspecial(‘sobel’);
SobelIm = filter2(fs_sobel,BW);%卷积运算

%拉普拉斯算子,默认值0.2
% h = [0 1 0,1 -4 1,0 1 0];
%laIm = conv2(BW,unit8(h),‘same’);
fs_lap = fspecial(‘laplacian’,0.7);
LapIm = filter2(fs_lap,BW);

%梯度算子检测边缘
roIm = edge(BW,‘roberts’);
soIm = edge(BW,‘sobel’);
prIm = edge(BW,‘prewitt’);

%log算子检测边缘
logIm = edge(BW,‘log’);
%canny算子
canIm = edge(BW,‘canny’);

figure;
subplot(241);imshow(RGBIm);title(‘原始图片’);
subplot(242);imshow(SobelIm);title(‘sobel锐化图像’);
subplot(243);imshow(roIm);title(‘laplacian检测边缘’);
subplot(244);imshow(soIm);title(‘roberts检测边缘’);
subplot(245);imshow(prIm);title(‘sobel检测边缘’);
subplot(246);imshow(LapIm);title(‘prewitt检测边缘’);
subplot(247);imshow(logIm);title(‘log检测边缘’);
subplot(248);imshow(canIm);title(‘canny检测边缘’);

—————————————————————————————————————

%%
%边界跟踪(bwtraceboundary函数)
RGBIm = imread(‘lena.jpg’);
gray = rgb2gray(RGBIm);
threshold = graythresh(gray);%使用此函数找到最佳阈值
BW = im2bw(gray,threshold);%使用此阈值将图像转化为二值图

dim = size(BW);
col = round(dim(2)/2)+90;%设定一个列坐标
row = find(BW(:,col),1);%在该列中找到第一个不为0的像素坐标
connectivity = 8;%8联通方式
num_points = 180;%设定曲线最长值
%contour中保存的是所有点的坐标
contour = bwtraceboundary(BW,[row,col],‘N’,connectivity,num_points);%使用该函数找到所有以改点为起点的曲线

figure;
%subplot(131);imshow(RGBIm);title(‘原始图片’);
%subplot(132);imshow(gray);title(‘二值化图片’);
%subplot(133);plot(contour(:,2),contour(:,1),‘g’,‘LineWidth’,2);title(‘原始图片’);
imshow(BW);title(‘二值化图像’);
figure;
imshow(RGBIm);hold on;
plot(contour(:,2),contour(:,1),‘g’,‘LineWidth’,2);title(‘原始图片’);title(‘边界跟踪图像’);

—————————————————————————————————————

%%
%Hough变换
RGBIm = imread(‘lena.jpg’);
gray = rgb2gray(RGBIm);
threashold = graythresh(gray);%阈值
BW = im2bw(gray,threashold);%二值化图像
figure;
subplot(221);imshow(gray);title(‘二值化图像’);
preIm = edge(BW,‘prewitt’);
[H,T,R] = hough(preIm);
subplot(222);imshow(H,[],‘XData’,T,‘YData’,R,‘InitialMagnification’,‘fit’);
xlabel(’\theta’), ylabel(’\rho’);
axis on,axis square,hold on;
P=houghpeaks(H,3);
x = T(P(:,2));
y = R(P(:,1));
plot(x,y,‘s’,‘color’,‘white’);%标出极值点
lines = houghlines(preIm,T,R,P);%提取线段
subplot(223);imshow(RGBIm),hold on;
for k=1:length(lines)
xy = [lines(k).point1;lines(k).point2];
plot(xy(:,1),xy(:,2),‘LineWidth’,2,‘Color’,‘green’);%画出线段
plot(xy(1,1),xy(1,2),‘x’,‘LineWidth’,2,‘Color’,‘yellow’);%起点
plot(xy(2,1),xy(2,2),‘x’,‘LineWidth’,2,‘Color’,‘red’);%终点
end

—————————————————————————————————————

%%
%直方图阈值法
RGBIm = imread(‘lena.jpg’);
gray = rgb2gray(RGBIm);
figure;
subplot(221);imshow(gray);title(‘灰度图像’);
axis([50,250,50,200]);axis on;grid on;
[m,n] = size(gray);
GP = zeros(1,256);%预创建存放灰度出现概率的向量
for k=0:255
GP(k+1) = length(find(gray == k))/(m*n);%计算每级灰度出现的概率,将其存放在GP中相应的位置
end
subplot(222);bar(0:255,GP,‘g’);title(‘灰度直方图’);%bar创建条形图
xlabel(‘灰度值’);ylabel(‘出现概率’);
BW1 = im2bw(RGBIm,150/255);
subplot(223);imshow(BW1);title(‘阈值150的分割图像’);
axis([50,250,50,200]);grid on;axis on;
BW2 = im2bw(RGBIm,200/255);
subplot(224);imshow(BW2);title(‘阈值200的分割图像’);
axis([50,250,50,200]);grid on;axis on;

—————————————————————————————————————

%%
%自动阈值法:Otsu法
RGBIm = imread(‘lena.jpg’);
level = graythresh(RGBIm);%在这个函数中,是使用最大类间方差法找到图片的一个合适的阈值
BW = im2bw(RGBIm,level);
subplot(121);imshow(RGBIm);title(‘原始图像’);
axis([50,250,50,200]);axis on;grid on;
subplot(122);imshow(BW);title(‘Otsu法阈值分割图像’);
axis([50,250,50,200]);axis on;grid on;

—————————————————————————————————————

%%
%膨胀操作
RGBIm = imread(‘lena.jpg’);
gray = rgb2gray(RGBIm);
subplot(221);imshow(RGBIm);title(‘原始图像’);
axis([50,250,50,200]);axis on;grid on;
subplot(222);imshow(gray);title(‘灰度图像’);
axis([50,250,50,200]);axis on;grid on;
se = strel(‘disk’,1);%生成圆形结构元素
I2 = imdilate(gray,se);%用生成的结构元素对图像进行膨胀
subplot(223);imshow(I2);title(‘膨胀后的图像’);
axis([50,250,50,200]);axis on;grid on;

—————————————————————————————————————

%%
%腐蚀操作
RGBIm = imread(‘lena.jpg’);
gray = rgb2gray(RGBIm);
subplot(221);imshow(RGBIm);title(‘原始图像’);
axis([50,250,50,200]);axis on;grid on;
subplot(222);imshow(gray);title(‘灰度图像’);
axis([50,250,50,200]);axis on;grid on;
se = strel(‘disk’,1);%生成圆形结构元素
I2 = imerode(gray,se);%用生成的结构元素对图像进行膨胀
subplot(223);imshow(I2);title(‘腐蚀后的图像’);
axis([50,250,50,200]);axis on;grid on;

—————————————————————————————————————

%%
%开启和闭合操作
RGBIm = imread(‘lena.jpg’);
gray = rgb2gray(RGBIm);
subplot(221);imshow(RGBIm);title(‘原始图像’);
axis([50,250,50,200]);axis on;grid on;
subplot(222);imshow(gray);title(‘灰度图像’);
axis([50,250,50,200]);axis on;grid on;
se = strel(‘disk’,1);%采用半径为1的圆作为结构元素
I2 = imopen(gray,se);%开启操作
I3 = imclose(gray,se);%闭合操作
subplot(2,2,3),imshow(I2);title(‘开启运算后图像’);
axis([50,250,50,200]);axis on;
subplot(2,2,4),imshow(I3);title(‘闭合运算后图像’);
axis([50,250,50,200]);axis on;

—————————————————————————————————————

%%
%开闭组合操作
I=imread(‘lena.jpg’);
subplot(3,2,1),imshow(I);title(‘原始图像’);
axis([50,250,50,200]);axis on;
I1=rgb2gray(I);
subplot(3,2,2),imshow(I1);title(‘灰度图像’);
axis([50,250,50,200]);axis on;
se=strel(‘disk’,1);
I2=imopen(I1,se);
I3=imclose(I1,se);
subplot(3,2,3),imshow(I2);title(‘开启运算后图像’);axis([50,250,50,200]);axis on;
subplot(3,2,4),imshow(I3);title(‘闭合运算后图像’);axis([50,250,50,200]);axis on;
se=strel(‘disk’,1);
I4=imopen(I1,se);
I5=imclose(I4,se);
subplot(3,2,5),imshow(I5); title(‘开—闭运算图像’);axis([50,250,50,200]);axis on;
I6=imclose(I1,se);
I7=imopen(I6,se);
subplot(3,2,6),imshow(I7); title(‘闭—开运算图像’);axis([50,250,50,200]);axis on;

—————————————————————————————————————

%%
%形态学边界提取
I=imread(‘lena.jpg’);
subplot(1,3,1),imshow(I);title(‘原始图像’);axis([50,250,50,200]);grid on;axis on;
BW = im2bw(I);
subplot(1,3,2),imshow(I1);title(‘二值化图像’);axis([50,250,50,200]);grid on; axis on;
I2=bwperim(BW); %获取区域的周长
subplot(1,3,3),imshow(I2);title(‘边界周长的二值图像’);
axis([50,250,50,200]);grid on;axis on;

—————————————————————————————————————

%%
%形态学骨架提取
I=imread(‘lena.jpg’);
subplot(2,2,1),imshow(I);
title(‘原始图像’);axis([50,250,50,200]);axis on;
BW=im2bw(I);
subplot(2,2,2),imshow(BW);title(‘二值图像’);axis([50,250,50,200]);axis on;
I2=bwmorph(BW,‘skel’,1);%骨骼化图像
subplot(2,2,3),imshow(I2);title(‘1次骨架提取’);axis([50,250,50,200]);axis on;
I3=bwmorph(BW,‘skel’,3);
subplot(2,2,4),imshow(I3);title(‘2次骨架提取’);axis([50,250,50,200]);axis on;