Matlab批量将文件夹内图片进行二值化,并保存

files = dir(fullfile('C:\Users\Administrator\Desktop\ttt\原图1\','*.jpg'));
lengthFiles = length(files);
for i = 1:lengthFiles;
    Img = imread(strcat('C:\Users\Administrator\Desktop\ttt\原图1\',files(i).name));%文件所在路径
    Img = rgb2gray(Img);%将RGB图变为灰度图
    thresh = graythresh(Img); %自动确定阈值
    Img = im2bw(Img,thresh); %对图像二值化
%     Img = medfilt2(Img); %中值滤波
    imshow(Img);
    imwrite(Img,strcat('C:\\Users\\Administrator\\Desktop\\ttt\\原始二值图1\\a',num2str(i),'.jpg'));
    close all;
end

猜你喜欢

转载自blog.csdn.net/qq_38826019/article/details/81043587