close all;
clear all;
clc;
%% ***************************************
directory = uigetdir('','选择指定文件夹:');
if isequal(directory,0) %判断是否选择
msgbox('没有选择任何文件夹');
return
else
dirs = dir(directory);%dirs结构体类型,不仅包括文件名,还包含文件其他信息。(有表格名称)
%cd(directory); % 转到文件夹
dircell = struct2cell(dirs)'; %类型转化,转化为元组类型(无表格名称)
%然后根据后缀名筛选出指定类型文件并读入
filenames = dircell(:,1) ;%文件类型存放在第一列
[n m] = size(filenames);%获得大小
filenames = dircell(3:n,1) ;%文件类型存放在第一列(去除系统自带的两个隐文件夹)
for i = 1:n-2
if ~isempty( findstr(filenames{i},'.tiff') )%筛选出文件
filename = filenames{i};
filepath = fullfile(directory,filename);
Data_file_path{i} = filepath;
end
end
Data_true = cellfun(@isempty,Data_file_path);
Data_file_path = Data_file_path(~Data_true);
Data_file_path = Data_file_path';
end
%% 遍历图片处理*************4444444444444
for j = 1:n-2
image_data = imread(char(Data_file_path(j,1)));
[pathname,name,ext]=fileparts(char(Data_file_path(j,1)));
% 2.灰度转换
if ndims(image_data) > 2
image_data = rgb2gray(image_data);
end
need_path = strcat(pathname,'\',name(1:end-3),'\');%文件夹
if ~exist(need_path)
mkdir(need_path) % 若不存在,在当前目录中产生一个子目录
end
%% Canny操作图像
canny_image = image_data;
start = 0.1;
final =0.6;
num_step = 12;
num_extend =100;
for threshold_value = start:final/num_step:final
for standard_value = start*num_extend:final*num_extend/num_step:final*num_extend
image_canny = edge(canny_image,'Canny',threshold_value,standard_value);%阈值(越大越明显)、标准差(越小越明显)
subplot(122)
imshow(image_canny,[]);
title('Canny算子检测边缘')
%% image_canny1 = abs(image_canny-1); %反转
threshold_value_str = sprintf('%1.2f',threshold_value);
threshold_var_value = strcat('_T',threshold_value_str,'_S',num2str(standard_value));
imwrite(mat2gray(image_canny),strcat(need_path,name(1:end-3),'_Canny',threshold_var_value,'.tiff'),'Resolution',300);
end
end
end