-
Notifications
You must be signed in to change notification settings - Fork 7
/
matlab_to_txt.m
75 lines (69 loc) · 2.18 KB
/
matlab_to_txt.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
function matlab_to_txt(groundTruthData)
% 功能:把matlab imageLabeler APP中的groundTruth变量导出到txt文本中,
% 每个图像对应一个txt。请自行选择导出文件夹。
%输入:
% groundTruthData,groundTruth类型或table类型标注文件
%输出:无
%
%Example ;
% matlab_to_txt(groundTruthData)
%
if ~istable(groundTruthData)&&~strcmpi(class(groundTruthData),'groundTruth')
error('请在matlab imageLabeler APP中导出标注变量数据!');
end
if strcmpi(class(groundTruthData),'groundTruth')
imageFilename = groundTruthData.DataSource.Source;
classTable = groundTruthData.LabelData;
mylabel = [cell2table(imageFilename),classTable];
else
mylabel = groundTruthData;
end
folder_name = fileparts( mylabel.imageFilename{1});
imds = imageDatastore(folder_name,'FileExtensions',{'.jpg'});
imageNums = length(imds.Files);
numSamples = size(mylabel,1);
variableNames = mylabel.Properties.VariableNames;
numVariables = length(variableNames);
%% delete unlabeled images and TXT
if imageNums>numSamples
A = imds.Files;
B = mylabel.imageFilename;
Lia = ismember(A,B);
removeFile = imds.Files(~Lia);
for i = 1:size(removeFile,1)
name = char(removeFile(i));
name = name(1:end-4);
delete([name,'.jpg']);
delete([name,'.txt']);
end
end
%% write
h = waitbar(0,'Please wait...');
steps = numSamples;
for i =1:numSamples
rowTable = mylabel(i,:);
[~,imagename,~] = fileparts(char(rowTable{1,1}));
txtName = imagename;
filename = fullfile(folder_name,[txtName,'.txt']);
%%
numROIs = 0;
fid = fopen(filename,'w');
fprintf(fid,'%2d\r\n',0);
for j = 2:numVariables
rects = [rowTable{1,j}];
if iscell(rects)
rects = cell2mat(rects);
end
for k = 1:size(rects,1)
numROIs = numROIs+1;
fprintf(fid,'%s %d %d %d %d %d %d %d %d\r\n',variableNames{j},...
round(rects(k,1:2)-1),round(rects(k,3:4)),zeros(1,4));
end
end
fseek(fid, 0, 'bof');
fprintf(fid,'%2d\r\n',numROIs); %写入ROI个数
fclose(fid);
waitbar(i / steps);
end
close(h)
warndlg('已覆盖到原有标注txt文件');