The following code for rectangle detection somewhat helped me. But now i want to crop the rectangle as a sub image.
img =imread('xyz6.jpg');
bw = im2bw(img);
% find both black and white regions
stats = [regionprops(bw); regionprops(not(bw))]
% show the image and draw the detected rectangles on it
imshow(bw);
hold on;
for i = 1:numel(stats)
rectangle('Position', stats(i).BoundingBox, ...
'Linewidth', 2, 'EdgeColor', 'r', 'LineStyle', '--');
end