MRimageResCrop

Repmat an img by ns, then crop from center using original img size + percent offset on all sides:

MRimgResCrop:

function [imgC] = MRimgResCrop(img, nS, percent)

%
%   repmat the img by ns, then crop from center using original img size +
%   percent offset on all sides
%
%   img = image data
%
%   ns = two element integer vector for repmat: [3 3]
%
%   percent = percentage by which to increase the original image size for
%   cropping
%
%   Example
%   ——-
%   I = imread(‘cameraman.tif’);
%   I2 = MRimgResCrop(I,[3 3], 50);
%   figure, imshow(I)
%   figure, imshow(I2)

imgSizeOrig = size(img);

imgL = repmat(img, nS);
center = size(imgL) / 2;

percOffsetX = ((imgSizeOrig(1) / 100) * percent) * 2;
percOffsetY = ((imgSizeOrig(2) / 100) * percent) * 2;

cropRect = [(center(2) – (imgSizeOrig(2) / 2) – (percOffsetX / 2))…
(center(1) – (imgSizeOrig(1) / 2) – (percOffsetY / 2))…
(imgSizeOrig(2) + percOffsetX)…
(imgSizeOrig(1) + percOffsetY)];

imgC = imcrop(imgL, cropRect);

end

 

mrimgrescrop1

original image

mrimgrescrop2

image using MRimgResCrop(I,[3 3], 50)

download_mrimgrescrop