⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lhiobjectmask.m

📁 This code can parse any image in matlab. Very elaborate code
💻 M
字号:
function [mask, class, var1, var2] = LHIobjectmask(varargin) 
% Returns a segmentation mask for all the objects in the annotation struct
%
% Uses:
%  [mask, class] = LHIobjectmask(annotation, HOMEIMAGES, HOMELABELMAPS);
%  [mask, class, ptcounts,pos] = LHIobjectmask(annotation, HOMEIMAGES, HOMELABELMAPS);
%  [mask, class, ptcounts,pos] = LHIobjectmask(annotation, HOMEIMAGES, HOMELABELMAPS, scaling);
%  [mask, class, maskpol, classpol] = LHIobjectmask(annotation, HOMEIMAGES, HOMELABELMAPS, objectlist);
%  [mask, class, maskpol, classpol] = LHIobjectmask(annotation, HOMEIMAGES, HOMELABELMAPS, scaling, objectlist);
%  
% Variables:
%  scaling: scaling factor (0 1]
%  mask: is a 3D logical array (pixel = 1 => object present)
%  class: is the name of all the objects.
%  maskpol: is a mask for each polygon in the annotation.
%  classpol: is the class number assigned to each instance.
%
% Example:
%   [D,j] = LHIquery(database, 'name', 'car+side');
%   mask = LHIobjectmask(database(j(1)).annotation, HOMEIMAGES, 'car+side,building,road');
%   figure; imagesc(double(mask));
% The logical matrix 'mask' will be of size [n x m x 3] because there are three objects.


maskpol = []; classpol = []; img = [];

if nargin <= 2
    error('Not enough input arguments.')
end

if nargin > 2
    annotation = varargin{1};
    HOMEIMAGES = varargin{2}; % image folder
    HOMELABELMAPS = varargin{3}; % labelmap folder
    labelmap = LHILMread(annotation, HOMELABELMAPS);
    if strcmp(HOMEIMAGES(1:5), 'http:');
        img = LHIimread(annotation,HOMEIMAGES);
        [nrows,ncols,id]=size(img);
    else
        info = imfinfo(fullfile(HOMEIMAGES, annotation.folder, annotation.filename));
        nrows = info.Height;
        ncols = info.Width;
    end
end
scaling = 1;
if nargin >= 4
    if (~ischar(varargin{4}))
        scaling = varargin{4};
    else
        objectlist = varargin{4};
    end
end

if nargin == 3 || (nargin == 4 && ~ischar(varargin{4}))
    % if a list is not specified it generates segmentation masks for all
    % the annotated objects in the order that they are present. Multiple
    % intances of the same object also get separated masks.
    if(scaling ~= 1)
        if (isempty(img))
            img = LHIimread(annotation,HOMEIMAGES);
        end
        [annotation, img] = LHIimscale(annotation,img, scaling);
        [nrows,ncols,id]=size(img);
        labelmap = imresize(labelmap,[nrows,ncols],'nearest');
    end
    class = []; mask = []; ptcount = []; pos = [];
    if isfield(annotation, 'object')
        Nobjects = length(annotation.object);
        [x,y] = meshgrid(1:ncols,1:nrows);
        mask = zeros([nrows, ncols, Nobjects]);
        Nobjects = length(annotation.object);
        count = 1;
        for i = 1:Nobjects
            Nregions = length(annotation.object(i).regions);
            if Nregions == 0
                continue;
            end
            class{count} = annotation.object(i).name; % get object name
            ptnum = 0;
            X=[];Y=[];
            for j =1:Nregions,
                [a,b] = getLHIpoints(annotation.object(i).regions(j));
                X = [X;a]; Y = [Y;b];
            end
            pos{count} = [mean(X), mean(Y)];
            pos{count} = pos{count} ./ [ncols,nrows];
            ptcount(count) = length(X);
            col = annotation.object(i).labelmap;
            
            mask(:,:,count) = ((labelmap(:,:,1)==str2num(col.R))&(labelmap(:,:,2) == str2num(col.G))&(labelmap(:,:,3) == str2num(col.B)));
            count = count + 1;
        end
    end

    var1 = ptcount;
    var2 = pos;

else
    if(scaling ~= 1)
        img = LHIimread(annotation,HOMEIMAGES);
        [annotation, img] = LHIimscale(annotation,img, scaling);
        [nrows,ncols,id]=size(img);
        labelmap = imresize(labelmap,[nrows,ncols],'nearest');
    end
    if isfield(annotation, 'object')
        Nobjects = length(annotation.object); ni=0;
        maskpol = logical(zeros(nrows, ncols, Nobjects));
        classpol = zeros(Nobjects,1);

        class = []; mask = [];
        j = strfind(objectlist,','); j = [0 j length(objectlist)+1];
        mask = logical(zeros(nrows, ncols, length(j)-1));
        [x,y] = meshgrid(1:ncols,1:nrows);
        for i = 1:length(j)-1
            class{i} = objectlist(j(i)+1:j(i+1)-1); % get object name
            jc = LHIobjectindex(annotation, class{i});
            for n = 1:length(jc)
                col = annotation.object(jc(n)).labelmap;
                ni = ni+1;
                mask(:,:,ni) = ((labelmap(:,:,1)==str2num(col.R))&(labelmap(:,:,2) == str2num(col.G))&(labelmap(:,:,3) == str2num(col.B)));
                classpol(ni) = i;
                mask(:,:,i) = mask(:,:,i) | maskpol(:,:,ni);
            end
        end
        maskpol = maskpol(:,:,1:ni);
        classpol = classpol(1:ni);
    else
        mask = logical(zeros(nrows, ncols, 1));
        class = [];
    end
    var1 = maskpol;
    var2 = classpol;
end

function num = getRegionNum(annotation)
num = 0;
Nobject = length(annotation.object);
for i = 1:Nobject
    Nregions = length(annotation.object(i).regions);
    if (Nregions > 0 )
        num = num + 1;
    end
    Npart = length(annotation.object(i).parts)
    for j = 1:Npart
        Nregions = length(annotation.object(i).parts(j).regions);
        if (Nregions > 0 )
            num = num + 1;
        end
    end
end

⌨️ 快捷键说明

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