lhiobjectstats.m

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

M
145
字号
function [objectnames, instancecounts, areacounts, pointcounts, positions] = LHIobjectstats(D, HOMEIMAGES, HOMELABELMAPS)
% 
% Creates a matrix with object counts:
%
% [objectnames, instancecounts] = LHIobjectstats(D,HOMEIMAGES,HOMELABELMAPS);
%
% instancecounts(i,j) = number of times object class i is present in image j.
%    j is the index to the image D(j)
%    i is the index for an object class objectnames{i}
%
% [objectnames, instancecounts, areacounts, pointcounts, positions] = LHIobjectstats(D, HOMEIMAGES, HOMELABELMAPS);
%
% areacounts(i,j) = proportion of pixels occupied by object class i in
%    image j.
% pointscounts(i,j) = number of points to compose polygon by object class i
% in image j.
% positions(i,j,1) = x position of object class i in image j (normalized
% image width to 1;
% positions(i,j,2) = y position of object class i in image j (normalized
% image width to 1;


objectnames = LHIobjectnames(D);

Nimages = length(D);
Nobjects = length(objectnames);

instancecounts = zeros(Nobjects, Nimages);
areacounts = zeros(Nobjects, Nimages);
pointcounts = zeros(Nobjects, Nimages);
positions = zeros(Nobjects,Nimages,2);

for i = 1:Nimages
    i
    if isfield(D(i).annotation, 'object')
        if nargout == 2 | nargout == 0
            objectsinimage = {D(i).annotation.object.name};
            
            [TF, ndx] = ismember(strtrim(lower(objectsinimage)),lower(objectnames));
            for n = 1:length(ndx)
                instancecounts(ndx(n),i) = instancecounts(ndx(n),i)+1;
            end
        else
            [mask, objectsinimage,ptcounts,pos] = LHIobjectmask(D(i).annotation, HOMEIMAGES,HOMELABELMAPS,0.2);
            [nrows ncols no] = size(mask);
            
            [TF, ndx] = ismember(strtrim(lower(objectsinimage)), lower(objectnames));
            for n = 1:length(ndx)
                instancecounts(ndx(n),i) = instancecounts(ndx(n),i)+1;
                areacounts(ndx(n),i) = areacounts(ndx(n),i) + sum(sum(mask(:,:,n), 1),2)/nrows/ncols.* (1/sum(ndx==ndx(n)));
                pointcounts(ndx(n),i) = pointcounts(ndx(n),i) + ptcounts(n).* (1/sum(ndx==ndx(n)));
                positions(ndx(n),i,:) = pos{n};
            end
        end

    end
end


frequency = sum(instancecounts,2);
[ff, ndx] = sort(frequency,'descend');

% sort by frequency:
instancecountsSort = instancecounts(ndx, :);
objectnamesSort = objectnames(ndx);

% Show object counts sorted by frequency
handle=figure; maximize(handle);
imagesc(instancecountsSort)
colormap(gray(256))
set(gca, 'YTick', 1:Nobjects)
set(gca, 'YtickLabel', objectnamesSort)
ylabel('Object index')
xlabel('Image index')
title('Each entry counts the number of times the object y is in the image x')
axis('on')
axis('tight')

layout = [4 4]; offset = 0;
positions = positions(ndx, :, :);
handle=figure; maximize(handle);
for i=1:size(positions,1)
    subplot(layout(1),layout(2),i-offset);
    mask = logical(zeros(64,64));
    for j=1:size(positions,2)
       if (positions(i,j,1) == 0 || positions(i,j,2) == 0)
           continue;
       end
       mask(ceil(64*positions(i,j,2)),ceil(64*positions(i,j,1)))= 1;
    end
    imshow(logical(mask),[]);
    xlabel('X pos'); ylabel('Y pos');
    title(objectnamesSort{i});
    if (rem(i,layout(1)*layout(2)) == 0)
        handle=figure; maximize(handle);
        offset = offset + layout(1)*layout(2);
    end
end

%sort by point number
pointnum = sum(pointcounts,2);
[ff, ndx] = sort(pointnum,'descend');

pointcountsSort = pointcounts(ndx, :);
objectnamesSort = objectnames(ndx);
offset = 0;
handle=figure; maximize(handle);
for i=1:size(pointcountsSort,1)
    subplot(layout(1),layout(2),i-offset);
    %remove 0
    bins = pointcountsSort(i,:);
    bins = bins(bins>0);
    hist(bins); axis normal;
    xlabel('object keypoints number');
    title(objectnamesSort{i});
    if (rem(i,layout(1)*layout(2)) == 0)
        handle=figure; maximize(handle);
        offset = offset + layout(1)*layout(2);
    end
end

%sort by area number
areasum = sum(areacounts,2);
[ff, ndx] = sort(areasum,'descend');

areacountsSort = areacounts(ndx, :);
objectnamesSort = objectnames(ndx);
handle=figure; maximize(handle);
offset = 0;
for i=1:size(areacountsSort,1)
    subplot(layout(1),layout(2),i-offset);
    bins = areacountsSort(i,:);
    bins = bins(bins>0);
    hist(bins); axis('normal');
    h = findobj(gca,'Type','patch');
    set(h,'FaceColor','r','EdgeColor','w')
    xlabel('object/image area ratio');
    
    title(objectnamesSort{i});
    if (rem(i,layout(1)*layout(2)) == 0)
        handle=figure; maximize(handle);
        offset = offset + layout(1)*layout(2);
    end
end

⌨️ 快捷键说明

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