多相机depth-rgb图组完整性分拣器_MATLAB实现

发布于:2025-07-17 ⋅ 阅读:(19) ⋅ 点赞:(0)

文件夹中数据规则为,一张BMP格式的RGB图像会有一张同名的raw格式的深度图,一共有三个相机,三个相机的数据为一组,例如:1_0.bmp,1_0.raw,1_1.bmp,1_1.raw,1_2.bmp,1_2.raw为一组相机的数据。现在文件夹中数据存在缺失情况,可能缺失某个相机的raw格式的深度。使用matlab代码筛选文件夹中的数据,将一组数据中存在缺少raw格式的这组数据放在一个文件夹,不存在确实的六个数据统一放在一个文件夹。

分析:
MATLAB 脚本用于把“成组”数据分成两类:

  1. 六个文件齐全(3 个 .bmp + 3 个 .raw);
  2. 缺失任一 .raw 的整组数据(其余文件也一起搬走)。

脚本假设所有文件都在当前工作目录下,并按如下命名规则:
{组号}_{相机号}.bmp{组号}_{相机号}.raw,相机号固定为 0/1/2。

%%
clear; clc;

%% 参数
srcDir   = pwd;              % 原始目录
goodDir  = fullfile(srcDir,'complete');   % 六个文件都有的组
badDir   = fullfile(srcDir,'missing_raw');% 组内任一 raw 缺失

if ~exist(goodDir,'dir'), mkdir(goodDir); end
if ~exist(badDir,'dir'), mkdir(badDir);   end

% 文件列表
files = dir(fullfile(srcDir,'*.*'));
names = {files.name};
isBMP = endsWith(names, '.bmp', 'IgnoreCase', true);
isRAW = endsWith(names, '.raw', 'IgnoreCase', true);

allBMP = names(isBMP);
allRAW = names(isRAW);

% 按组号归类
bmpGroups = regexp(allBMP,'^(\d+)_[012]\.bmp$','tokens','once');
rawGroups = regexp(allRAW,'^(\d+)_[012]\.raw$','tokens','once');

bmpGroups = cellfun(@(c)c{1}, bmpGroups, 'UniformOutput', false);
rawGroups = cellfun(@(c)c{1}, rawGroups, 'UniformOutput', false);

tmp = [bmpGroups, rawGroups];   % 1×(N+M) cell
groups = unique(tmp);           % 去重

%% 扫描每组
for g = groups
    gStr = char(g);
    
    % 该组应有的 6 个文件名
    needFiles = [sprintf('%s_0.bmp',gStr);
                 sprintf('%s_1.bmp',gStr);
                 sprintf('%s_2.bmp',gStr);
                 sprintf('%s_0.raw',gStr);
                 sprintf('%s_1.raw',gStr);
                 sprintf('%s_2.raw',gStr)];
    
    % 实际存在的 6 个文件
    existFlag = ismember(needFiles, names);
    
    % 判断 raw 是否完整
    rawExist  = existFlag(4:6);
    
    if all(rawExist)
        % 六个文件都在 → complete
        tgt = goodDir;
    else
        % 任一 raw 缺失 → missing_raw
        tgt = badDir;
    end
    
    needFiles = strcat(gStr, {'_0.bmp','_1.bmp','_2.bmp', ...
                          '_0.raw','_1.raw','_2.raw'});
    
    % 移动该组所有存在的文件
    existFlag = ismember(needFiles, names);   % needFiles 现在是 cell

    for k = find(existFlag)
        movefile(needFiles{k}, fullfile(tgt, needFiles{k}));
    end
end

disp('分组完成!');

使用方法

  1. 将脚本保存为 classifyGroups.m,放在数据根目录。
  2. 运行 classifyGroups,脚本会自动创建 completemissing_raw 两个子文件夹并把相应文件移入。

实现效果
在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到