lhireplacepartname.m

来自「This code can parse any image in matlab.」· M 代码 · 共 64 行

M
64
字号
function D = LHIreplaceobjectname(D, name1, name2, method, querymethod)
% Replace an object name by another name.
% It is not case sensitive.
%
% D = LHIreplaceobjectname(D, 'person walking', 'pedestrian')
%
% Reemplaces all the object names that contain the string 'person' by the
% short name 'person'
% D = LHIreplaceobjectname(D, 'person', 'person', 'rename')
%
% To prevent mistake replacement (e.g. intepretate 'basket'-> 'basketball'), 
% use querymethod  'word'
% D = LHIreplaceobjectname(D, 'bakset', 'container', 'rename','word') 
% Detail for querymethod see LHIquery.m
%
% Reemplaces all the object names that contain the strings 'person' or 'pedestrian' by the
% short name 'person'
% D = LHIreplaceobjectname(D, 'person,pedestrian', 'person', 'rename')
%
% It does not modify the annotation files. It only modifies the index
% struct.

if nargin < 4
    method = 'replace';
end

name1 = lower(name1);
name2 = lower(name2);

if nargin < 5
    [d, j] = LHIquery(D, 'object.parts.name', name1);
else
    [d, j] = LHIquery(D, 'object.parts.name', name1, querymethod);
end
clear d

Nimages = length(j);
for n = 1:Nimages
    Nobjects = length(D(j(n)).annotation.object);
    for m = 1:Nobjects
        if nargin < 5
            jc = LHIpartindex(D(j(n)).annotation.object(m),name1);
        else
            jc = LHIpartindex(D(j(n)).annotation.object(m),name1,querymethod);
        end

        for i = jc
            currentname = lower(D(j(n)).annotation.object(m).parts(i).name);
            switch method
                case 'replace'
                    % replace each substring name1 by name2
                    D(j(n)).annotation.object(m).parts(i).name = strrep(currentname, name1, name2);
                case 'rename'
                    % rename the object
                    D(j(n)).annotation.object(m).parts(i).name = name2;
            end
        end
    end
    

end

disp(sprintf('%d entries replaced', Nimages))

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?