lhiimscale.m

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

M
91
字号
function [annotation, img] = LHIimscale(annotation, img, scaling, method);
%
% Scales one image (as in imresize) and the associated annotation.
% The scale factor is given by 'scaling'
% 
% When scaling<1, the image is downsampled.
%
% [annotation, img] = LHIimscale(annotation, img, scaling);
%

% Store the original numerical format of the image and turn it into 'double'.
imgtype = whos('img');
%img = double(img);

if nargin < 4
    method = 'nearest';
end

if scaling ~= 1
    % Image resampling:
    img = imresizefast(img, scaling, method);
    % Change the size of the polygon coordinates
    if isfield(annotation, 'object')
        Nobjects = length(annotation.object); n=0;
        for i = 1:Nobjects
            annotation.object(i) = resizeObjAnnotation(annotation.object(i),scaling);
            Nparts = length(annotation.object(i).parts);
            for j = 1:Nparts
                annotation.object(i).parts(j) = ...
                    resizeObjAnnotation(annotation.object(i).parts(j),scaling);
            end
        end
    end
end

% return the image in its original numeric format
img = feval(imgtype.class, img);


function object = resizeObjAnnotation(object,scaling)

if (isfield(object,'regions'))
    Nregions = length(object.regions);
    for k = 1:Nregions
        object.regions(k).pt = resizePoints(object.regions(k).pt,scaling);
    end
end
if (isfield(object,'subgraphs'))
    Nsubgraphs = length(object.subgraphs);
    for k = 1:Nsubgraphs
        Ncurves = length(object.subgraphs(k).curves);
        for m = 1:Ncurves
            object.subgraphs(k).curves(m).pt ...
            =resizePoints(object.subgraphs(k).curves(m).pt,scaling);
        end
    end
end

function pts = resizePoints(pts,scaling)
Npoints = length(pts);
for j = 1:Npoints
    % Scale each point:
    x=str2num(pts(j).x);
    y=str2num(pts(j).y);

    x = round(x*scaling);
    y = round(y*scaling);

    pts(j).x = num2str(x);
    pts(j).y = num2str(y);
end

function img = imresizefast(img, scaling, method, init);

if nargin<4
    init = 0;
end

if scaling > .5 | strcmp(method, 'nearest');
    img = imresize(img, scaling, method);
else
    img = convn(img, [1 2 1]'/4, 'same'); 
    img = img(init+1:2:end, :, :);
    img = convn(img, [1 2 1]/4, 'same'); 
    img = img(:,init+1:2:end,:);
    img = imresizefast(img, 2*scaling, method, 1-init);
end



⌨️ 快捷键说明

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