TencentYoutuResearch / CrowdCounting-P2PNet

The official codes for the ICCV2021 Oral presentation "Rethinking Counting and Localization in Crowds: A Purely Point-Based Framework"
Other
423 stars 114 forks source link

如何将数据集中的mat文件转换为TXT文件 #72

Open jone222 opened 7 months ago

mpmmpmmmp commented 7 months ago

以下是一个例子, 你需要根据x, y坐标的对应位置决定是否需要交换x和y坐标的位置

root_folder = '';

% 获取根文件夹中所有子文件夹的列表 subfolders = dir(root_folder); subfolders = subfolders([subfolders.isdir] & ~strcmp({subfolders.name}, '.') & ~strcmp({subfolders.name}, '..'));

% 遍历每个子文件夹 for s = 1:length(subfolders) subfolder_path = fullfile(root_folder, subfolders(s).name, 'mats');

% 获取子文件夹中所有.mat文件的列表
mat_files = dir(fullfile(subfolder_path, '*.mat'));

% 获取.txt文件保存的路径
output_folder = fullfile(fileparts(subfolder_path), 'txt');
if ~exist(output_folder, 'dir')
    mkdir(output_folder);
end

% 循环处理每个.mat文件
for i = 1:length(mat_files)
    % 读取.mat文件
    file_path = fullfile(subfolder_path, mat_files(i).name);
    data = load(file_path);

    % 提取image_info.location
    locations = data.image_info.location;

    % 交换x和y坐标的位置
    swapped_locations = [locations(:,2), locations(:,1)];

    % 获取.mat文件的基本文件名(不包括扩展名)
    [~, base_name, ~] = fileparts(mat_files(i).name);

    % 构建.txt文件的保存路径
    output_file_path = fullfile(output_folder, [base_name, '.txt']);

    % 打开输出的文本文件
    output_file = fopen(output_file_path, 'w');

    % 写入.txt文件
    fprintf(output_file, '%f %f\n', swapped_locations');

    % 关闭输出文件
    fclose(output_file);
end

end

jone222 commented 7 months ago

感谢,我尝试一下