1.批量二值化及保存
clc;
clear;
file_path = 'E:\spyder\spineunet-1\data\train\image\';% 图像文件夹路径
img_path_list = dir(strcat(file_path,'*.jpg'));%获取该文件夹中所有jpg格式的图像
img_num = length(img_path_list);%获取图像总数量
if img_num > 0 %有满足条件的图像
for j = 1:img_num %逐一读取图像
image_name = img_path_list(j).name;% 图像名
IM = imread(strcat(file_path,image_name));
%fprintf('%d %d %s',i,j,strcat(file_path,image_name));% 显示正在处理的图像名
%自适应滤波
im=wiener2(IM,[3,3]);
%二值化
thresh=graythresh(im);
im=im2bw(im,thresh);
% %****平滑滤波****
% h=ones(3,3)/5;
% h(1,1)=0;h(1,3)=0;h(3,1)=0;h(1,3)=0;
% im=imfilter(IM,h);
% %****中值滤波****
% im=medfilt2(IM,[3,3]);
% %****均值滤波****
% fsp=fspecial('average',3);
% im=imfilter(IM,fsp);
%figure;
%imshow(im);
saveddir='E:\matlab\gxy\';
savedname=fullfile(saveddir,image_name);
imwrite(im,savedname);
end
end
`2.滤波、开闭运算
I=imread('test16.bmp');
thresh=graythresh(I);
bw=im2bw(I,thresh);
%****平滑滤波****
h=ones(3,3)/5;
h(1,1)=0;h(1,3)=0;h(3,1)=0;h(1,3)=0;
I1=imfilter(I,h);
thresh1=graythresh(I1);
bw1=im2bw(I1,thresh1);
se1=strel('line',5,200);%直线长度7,角度85
img1=imopen(bw1,se1);%开运算
img2=imclose(bw1,se1);%闭运算
img3=imclose(img1,se1);%先开后闭运算
img4=imopen(img2,se1);%先闭后开运算
img5=imdilate(bw1,se1);%膨胀操作
img6=imerode(bw1,se1);%腐蚀
figure(1);
subplot(331)