MATLAB: Remove the unwanted region from an image and rotate it to 45 degree

Image Processing Toolboxremove unwanted portion

I have an image attached with this question. I wanted to remove the unwanted white portion and rotate image to 45 degree angle. The unwanted portion contains values either 255 or 0. Please help me.

Best Answer

Try this:
grayImage = imread('11.bmp');
subplot(2, 2, 1);
imshow(grayImage)
title('Original Image', 'FontSize', 15);
grayImage = imrotate(grayImage, -45);
subplot(2, 2, 2);
imshow(grayImage)
title('Rotated Image', 'FontSize', 15);
% Get mask
mask = bwconvhull(grayImage ~= 255 & grayImage ~= 0);
subplot(2, 2, 3);
imshow(mask)
title('mask', 'FontSize', 15);
% Find cropping index
[rows, columns] = find(mask)
% Crop
croppedImage = grayImage(min(rows):max(rows), min(columns):max(columns));
subplot(2, 2, 4);
imshow(croppedImage)
title('Cropped Image', 'FontSize', 15);