function dual_side_fragment_classification()
% 初始化 clc; clear; close all; warning off; % 选择文件夹 folder_path = uigetdir('', '选择包含双面碎片的文件夹(附件5)'); if folder_path == 0 error('用户取消选择'); end %% 1. 加载并配对正反面文件 [pairs, unmatched] = load_and_pair_fragments(folder_path); fprintf('已加载 %d 对有效碎片,%d 个未匹配碎片\n', size(pairs,1), length(unmatched)); %% 2. 提取行特征(仅使用正面) features = zeros(size(pairs,1), 100); % 100维特征 parfor i = 1:size(pairs,1) img = preprocess_image(fullfile(folder_path, pairs{i,1})); features(i,:) = extract_row_features(img); end %% 3. 行分类(22行) row_labels = classify_rows(features, 14); %% 4. 生成分类结果表 create_result_table(folder_path, pairs, row_labels, unmatched); fprintf('分类完成!结果已保存到:%s\n', fullfile(folder_path, 'classification_results.xlsx'));
end
%% 文件加载与配对函数(修改版)
function [pairs, unmatched] = load_and_pair_fragments(folder)
% 获取所有.bmp文件 file_list = dir(fullfile(folder, '*.bmp')); if isempty(file_list) file_list = dir(fullfile(folder, '*.BMP')); end % 使用结构数组替代Map all_files = struct('id', {}, 'a', '', 'b', ''); % 先索引所有文件 for i = 1:length(file_list) [~, name, ~] = fileparts(file_list(i).name); id_str = name(1:end-1); % 去掉最后一位a/b id_num = str2double(id_str); % 查找或创建条目 found = false; for j = 1:length(all_files) if all_files(j).id == id_num if endsWith(name, 'a') all_files(j).a = file_list(i).name; else all_files(j).b = file_list(i).name; end found = true; break; end end if ~found new_entry.id = id_num; if endsWith(name, 'a') new_entry.a = file_list(i).name; new_entry.b = ''; else new_entry.a = ''; new_entry.b = file_list(i).name; end all_files(end+1) = new_entry; end end % 配对正反面 pairs = cell(0,2); unmatched = {}; for i = 1:length(all_files) if ~isempty(all_files(i).a) && ~isempty(all_files(i).b) pairs(end+1,:) = {all_files(i).a, all_files(i).b}; elseif ~isempty(all_files(i).a) unmatched{end+1} = all_files(i).a; else unmatched{end+1} = all_files(i).b; end end % 按编号排序 [~, idx] = sort([all_files.id]); all_files = all_files(idx); % 重新组织已排序的数据 pairs = cell(0,2); unmatched = {}; for i = 1:length(all_files) if ~isempty(all_files(i).a) && ~isempty(all_files(i).b) pairs(end+1,:) = {all_files (i).a, all_files(i).b}; elseif ~isempty(all_files(i).a) unmatched{end+1} = all_files(i).a; else unmatched{end+1} = all_files(i).b; end end
end
%% 图像预处理(适配.bmp)
function img = preprocess_image(filepath)
img = imread(filepath); if size(img,3) == 3 img = rgb2gray(img); end img = imbinarize(img, 'adaptive'); img = bwareaopen(img, 20);
end
%% 行特征提取(保持不变)
function features = extract_row_features(bw_img)
h_proj = sum(~bw_img, 2); features = imresize(h_proj, [100, 1])';
end
%% 行分类函数(保持不变)
function labels = classify_rows(features, k)
[~, score] = pca(features); [labels, ~] = kmeans(score(:,1:10), k, 'Replicates', 5);
end
%% 创建结果表格(修改版)
function create_result_table(folder, pairs, row_labels, unmatched)
% 初始化22x19的cell数组 result_front = cell(22, 19); result_back = cell(22, 19); % 按行组织碎片 for row = 1:max(row_labels) idx = find(row_labels == row); row_pairs = pairs(idx,:); % 填充到表格(最多19列) for col = 1:min(19, size(row_pairs,1)) % 正面碎片(去掉扩展名) [~, name_front, ~] = fileparts(row_pairs{col,1}); result_front{row, col} = name_front; % 对应背面碎片 [~, name_back, ~] = fileparts(row_pairs{col,2}); result_back{row, col} = name_back; end end % 写入Excel文件 output_file = fullfile(folder, 'classification_results.xlsx'); % 正面碎片分类 writecell({'正面碎片分类结果(22行×19列)'}, output_file, 'Sheet', '正面', 'Range', 'A1'); writecell(result_front, output_file, 'Sheet', '正面', 'Range', 'A2'); % 背面碎片分类 writecell({'背面碎片分类(对应正面位置)'}, output_file, 'Sheet', '背面', 'Range', 'A1'); writecell(result_back, output_file, 'Sheet', '背面', 'Range', 'A2'); % 未匹配碎片 if ~isempty(unmatched) unmatched_names = cellfun(@(x) x(1:end-4), unmatched, 'UniformOutput', false); writecell({'未匹配碎片(缺少对应面)'}, output_file, 'Sheet', '未匹配', 'Range', 'A1'); writecell(unmatched_names, output_file, 'Sheet', '未匹配', 'Range', 'A2'); end % 添加说明 notes = { '说明:'; '1. 文件格式要求:000a.bmp, 000b.bmp,...,208a.bmp,208b.bmp'; '2. 每行最多显示19个碎片,实际数量可能不同'; '3. 未匹配碎片可能缺少对应正反面文件'; '4. 需要人工验证跨行分类准确性'}; writecell(notes, output_file, 'Sheet', '说明', 'Range', 'A1');
end这个代码有的图片的正反面分不出来请修改以解决这个问题
回答
向我提问的人太多了。正在努力扩容中,请稍后再试。
function dual_side_fragment_classification()
% 初始化 clc; clear; close all; warning off; % 选择文件夹 folder_path = uigetdir(‘’, ‘选择包含双面碎片的文件夹(附件5)’); if folder_path == 0 error(‘用户取消选择’); end %% 1. 加载并配对正反面文件 [pairs, unmatched] = load_and_pair_fragments(folder_path); fprintf(‘已加载 %d 对有效碎片,%d 个未匹配碎片\n’, size(pairs,1), length(unmatched)); %% 2. 提取行特征(仅使用正面) features = zeros(size(pairs,1), 100); % 100维特征 parfor i = 1:size(pairs,1) img = preprocess_image(fullfile(folder_path, pairs{i,1})); features(i,:) = extract_row_features(img); end %% 3. 行分类(22行) row_labels = classify_rows(features, 14); %% 4. 生成分类结果表 create_result_table(folder_path, pairs, row_labels, unmatched); fprintf(‘分类完成!结果已保存到:%s\n’, fullfile(folder_path, ‘classification_results.xlsx’));
end
%% 文件加载与配对函数(修改版)
function [pairs, unmatched] = load_and_pair_fragments(folder)
% 获取所有.bmp文件 file_list = dir(fullfile(folder, ‘.bmp’)); if isempty(file_list) file_list = dir(fullfile(folder, '.BMP’)); end % 使用结构数组替代Map all_files = struct(‘id’, {}, ‘a’, ‘’, ‘b’, ‘’); % 先索引所有文件 for i = 1:length(file_list) [~, name, ~] = fileparts(file_list(i).name); id_str = name(1:end-1); % 去掉最后一位a/b id_num = str2double(id_str); % 查找或创建条目 found = false; for j = 1:length(all_files) if all_files(j).id == id_num if endsWith(name, ‘a’) all_files(j).a = file_list(i).name; else all_files(j).b = file_list(i).name; end found = true; break; end end if ~found new_entry.id = id_num; if endsWith(name, ‘a’) new_entry.a = file_list(i).name; new_entry.b = ‘’; else new_entry.a = ‘’; new_entry.b = file_list(i).name; end all_files(end+1) = new_entry; end end % 配对正反面 pairs = cell(0,2); unmatched = {}; for i = 1:length(all_files) if ~isempty(all_files(i).a) && ~isempty(all_files(i).b) pairs(end+1,:) = {all_files(i).a, all_files(i).b}; elseif ~isempty(all_files(i).a) unmatched{end+1} = all_files(i).a; else unmatched{end+1} = all_files(i).b; end end % 按编号排序 [~, idx] = sort([all_files.id]); all_files = all_files(idx); % 重新组织已排序的数据 pairs = cell(0,2); unmatched = {}; for i = 1:length(all_files) if ~isempty(all_files(i).a) && ~isempty(all_files(i).b) pairs(end+1,:) = {all_files (i).a, all_files(i).b}; elseif ~isempty(all_files(i).a) unmatched{end+1} = all_files(i).a; else unmatched{end+1} = all_files(i).b; end end
end
%% 图像预处理(适配.bmp)
function img = preprocess_image(filepath)
img = imread(filepath); if size(img,3) == 3 img = rgb2gray(img); end img = imbinarize(img, ‘adaptive’); img = bwareaopen(img, 20);
end
%% 行特征提取(保持不变)
function features = extract_row_features(bw_img)
h_proj = sum(~bw_img, 2); features = imresize(h_proj, [100, 1])';
end
%% 行分类函数(保持不变)
function labels = classify_rows(features, k)
[~, score] = pca(features); [labels, ~] = kmeans(score(:,1:10), k, ‘Replicates’, 5);
end
%% 创建结果表格(修改版)
function create_result_table(folder, pairs, row_labels, unmatched)
% 初始化22x19的cell数组 result_front = cell(22, 19); result_back = cell(22, 19); % 按行组织碎片 for row = 1:max(row_labels) idx = find(row_labels == row); row_pairs = pairs(idx,:); % 填充到表格(最多19列) for col = 1:min(19, size(row_pairs,1)) % 正面碎片(去掉扩展名) [~, name_front, ~] = fileparts(row_pairs{col,1}); result_front{row, col} = name_front; % 对应背面碎片 [~, name_back, ~] = fileparts(row_pairs{col,2}); result_back{row, col} = name_back; end end % 写入Excel文件 output_file = fullfile(folder, ‘classification_results.xlsx’); % 正面碎片分类 writecell({‘正面碎片分类结果(22行×19列)’}, output_file, ‘Sheet’, ‘正面’, ‘Range’, ‘A1’); writecell(result_front, output_file, ‘Sheet’, ‘正面’, ‘Range’, ‘A2’); % 背面碎片分类 writecell({‘背面碎片分类(对应正面位置)’}, output_file, ‘Sheet’, ‘背面’, ‘Range’, ‘A1’); writecell(result_back, output_file, ‘Sheet’, ‘背面’, ‘Range’, ‘A2’); % 未匹配碎片 if ~isempty(unmatched) unmatched_names = cellfun(@(x) x(1:end-4), unmatched, ‘UniformOutput’, false); writecell({‘未匹配碎片(缺少对应面)’}, output_file, ‘Sheet’, ‘未匹配’, ‘Range’, ‘A1’); writecell(unmatched_names, output_file, ‘Sheet’, ‘未匹配’, ‘Range’, ‘A2’); end % 添加说明 notes = { ‘说明:’; ‘1. 文件格式要求:000a.bmp, 000b.bmp,…,208a.bmp,208b.bmp’; ‘2. 每行最多显示19个碎片,实际数量可能不同’; ‘3. 未匹配碎片可能缺少对应正反面文件’; ‘4. 需要人工验证跨行分类准确性’}; writecell(notes, output_file, ‘Sheet’, ‘说明’, ‘Range’, ‘A1’);
end这个代码有的图片的正反面分不出来请修改以解决这个问题,我的代码是对于碎片拼接的,我的碎片地址是D:\BaiduNetdiskDownload\MATLAB R2024a\bin\project\附件5