lhimergeobjects.m

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

M
82
字号
function  [annotation,ind] = LHIMergeObjects(annotation,HOMELABELMAPS,ind1,ind2,newname)
% This function will merge two objects in same image into one and change
% the labelmap accordingly.
% 
% Input variables:
%   ind1: indexing of object one 
%   ind2: indexing of object two
%   newname(optional): new name of the merged object, leave blank will be
%   set to default name (name of object one)

% Output variables:
%   ind: indexing of merged object
%
% The merged object is the Union of two objects, in the sense that:
% object(ind).regions = Union(object(ind1).regions,object(ind1).regions)
% ojbect(ind).subgraphs = Union(object(ind1).subgraphs,object(ind1).subgraphs)
%

if nargin < 5
    newname = annotation.object(ind1).name;
end

object = [];

object.name = newname;
% load labelmap
labelmap = LHILMread(annotation,HOMELABELMAPS);

% Merge colors and labelmaps
color1 = annotation.object(ind1).labelmap;
color2 = annotation.object(ind2).labelmap;

if length(color1) ~= 0 && length(color2) ~= 0
    object.labelmap = color1; % keep the color of object one
    mask = ((labelmap(:,:,1)==str2num(color2.R))&(labelmap(:,:,2) == str2num(color2.G))&(labelmap(:,:,3) == str2num(color2.B)));
    labelmap = imoverlay(labelmap,mask,[str2num(color1.R);str2num(color1.G);str2num(color1.B)]);
    filename = fullfile(HOMELABELMAPS,annotation.folder,strrep(annotation.filename,'.jpg','.png'));
    imwrite(labelmap,filename);
elseif length(color1) > length(color2)
    object.labelmap = color1;
elseif length(color1) < length(color2)
    object.labelmap = color2;
else
    object.labelmap = [];
end

% Merge regions 
n = length(annotation.object(ind1).regions);
object.regions = annotation.object(ind1).regions;
for i=1:length(annotation.object(ind2).regions)
    object = setfield(object,'regions',{n+i},annotation.object(ind2).regions(i));
end

% Merge subgraphs
n = length(annotation.object(ind1).subgraphs);
object.subgraphs = annotation.object(ind1).subgraphs;
for i=1:length(annotation.object(ind2).subgraphs)
    object = setfield(object,'subgraphs',{n+i},annotation.object(ind2).subgraphs(i));
end

% Merge parts
n = length(annotation.object(ind1).parts);
object.parts = annotation.object(ind1).parts;
for i=1:length(annotation.object(ind2).parts)
    object = setfield(object,'parts',{n+i},annotation.object(ind2).parts(i));
end

% Set id
object.id = annotation.object(ind1).id;

annotation.object(ind1) = object;

% Erase object 2
for i=ind2+1:length(annotation.object)
    annotation.object(i-1) = annotation.object(i);
    annotation.object(i-1).id = annotation.object(i).id - 1;
end

annotation.object = annotation.object(1:(length(annotation.object)-1));

ind = ind1;

⌨️ 快捷键说明

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