st = regionprops(S, 'BoundingBox' )
eg:
z = randi(15,size(S))
ij = ceil(st.BoundingBox);
out = z(ij(2) + (0:ij(4)-1),ij(1) + (0:ij(3)-1))
added after the Kash's comment
Img = imread('Q8vjJ.png');
BW = im2bw(Img,graythresh(Img));
BW(:,1) = false;
BW2 = cumsum(BW,2) & fliplr(cumsum(BW(:,end:-1:1),2));
BW2 = cumsum(BW2) & flipud(cumsum(BW2(end:-1:1,:)));
st = regionprops(SBW2, 'BoundingBox' );
imcrop(Img,st.BoundingBox + [-1 -1 1 1]);
Best Answer