Code covered by the BSD License  

Highlights from
myLabel 0.04

image thumbnail
from myLabel 0.04 by Nikolay Chumerin
Image labeling tool for Matlab.

cut_slices(im, mask, object_id, output_dir, output_file, output_format, min_size_ratio, percentage, images2save, output_size, enlarge_coef, max_sizes, rotations, dangle, verbose)
function cut_slices(im, mask, object_id, output_dir, output_file, output_format, min_size_ratio, percentage, images2save, output_size, enlarge_coef, max_sizes, rotations, dangle, verbose)

if ~exist('output_file','var') || isempty(output_file),
    output_file = 'image'; % maximal number of resizings
end
if ~exist('object_id','var') || isempty(object_id),
    object_id = sprintf('%08.0f',round(1e8*rand));
end
if ~exist('max_sizes','var') || isempty(max_sizes),
    max_sizes = 0; % maximal number of resizings
end
if ~exist('enlarge_coef','var') || isempty(enlarge_coef),
    enlarge_coef = .5; % border size in respect to object extends
end
if ~exist('output_dir','var') || isempty(output_dir),
    output_dir = '/huge_sidonia/nick/myIMOs/test';
end

if ~exist('output_format','var') || isempty(output_format),
    output_format = '.png';
end

if ~exist('min_size_ratio','var') || isempty(min_size_ratio),
    min_size_ratio = 1/5;
end

if ~exist('output_size','var') || isempty(output_size),
    output_size = 64;
end
if ~exist('rotations','var') || isempty(rotations),
    rotations = 0;
end
if ~exist('verbose','var') || isempty(verbose),
    verbose = 0;
end

if ~exist('percentage','var') || isempty(percentage),
    percentage = 70;
end
if ~exist('images2save','var') || isempty(images2save),
    images2save = 5;
end
saved_images= 0;

mcols    = find(sum(mask(:,:,1)));
mrows    = find(sum(mask(:,:,1),2));
min_mrows= min(mrows);
min_mcols= min(mcols);
max_mrows= max(mrows);
max_mcols= max(mcols);

drows    = max_mrows - min_mrows + 1;
dcols    = max_mcols - min_mcols + 1;
max_d    = min(drows, dcols);
min_d    = max_d * min_size_ratio;
attempt  = 0;
max_attempts =  images2save * 100;
%  figure(2)
%  imagesc(mask);
while saved_images<images2save & attempt < max_attempts,
    attempt = attempt + 1;
    d = round(min_d + rand*(max_d-min_d));
    col = ceil(min_mcols-d + dcols*rand);
    row = ceil(min_mrows-d + drows*rand);
    if (col>0)&&(row>0)&&(row+d<=size(mask,1))&&(col+d<=size(mask,2)),
        if sum(sum(mask((0:d-1)+row,(0:d-1)+col)))/d^2 >= percentage/100,
            cut_object(im,[col row col+d row+d], [], output_dir, output_file,...
                output_format, output_size, enlarge_coef, max_sizes, rotations, dangle, verbose);
            saved_images = saved_images + 1;
        end
    end
end
%  attempt

Contact us at files@mathworks.com