MATLAB: Generating multiple ROIs from a single image. (Automatic generation of ROI required)

Image Processing Toolboxroi generation

I have this code below which works fine, but it is time consuming.
Is there a better and faster solution possible?
if true im = imread('my_image.png'); I = single(rgb2gray(im))/255;
%getting height and width of image
width = size(im,2);
height = size(im,1);
k = 15;
x = randsample(height,k);
y = randsample(width,k);
sP = horzcat(x,y);
for i = 1:k
if (((sP(i,1)+100) < height) && ((sP(i,2)+100) < width) && ((sP(i,1)-100) > 0) && ((sP(i,2)-100) > 0))
x_min = sP(i,1)-100;
x_max =sP(i,1) + 100;
y_min = sP(i,2)-100;
y_max = sP(i,2)+100;
roi = im(x_min:x_max,y_min:y_max,:);
imwrite(roi,['MYlocation' strcat(num2str(i) ,'_roi.png')],'png');
elseif((((sP(i,1)+200) < height) || ((sP(i,1)- 200) > 0)) && (((sP(i,2)+200) < width) || ((sP(i,2)- 200) > 0)))
if ((sP(i,1)+200) < height)
x_min = sP(i,1);
x_max = (sP(i,1)+200);
elseif ((sP(i,1)- 200) > 0)
x_min = sP(i,1)-200;
x_max = sP(i,1);
end
if((sP(i,2)+200) < width)
y_min = sP(i,2);
y_max = (sP(i,2)+200);
else
y_min = (sP(i,2)- 200);
y_max = sP(i,2);
end
roi = im(x_min:x_max,y_min:y_max,:);
imwrite(roi,['MYlocation' strcat(num2str(i) ,'_roi.png')],'png');
else
continue;
end
end
end

Best Answer

It's not time consuming on my computer - it's pretty fast.
Elapsed time is 0.675276 seconds.
And that even included an imshow() to see what I was saving. How long does it take for you?