MATLAB Answers

Cropping out the image/generating image the same size as the matrix it comes from

3 views (last 30 days)
Hello,
So I have a matrix that I display with imagesc(). I gray it in order to apply imcontour(), and plot this contour on the original image.
The issue is that I have an image bigger than my matrix, and I want it to be the same size. So I have 2 questions:
  • Is there a way to save directly the image from the matrix, and have it the same size ?
  • How to crop the black part of the image below in order to have a figure the same size as my matrix ?
Thanks for your help !
tocrop.png

  0 Comments

Sign in to comment.

Accepted Answer

Subhadeep Koley
Subhadeep Koley on 10 Feb 2020
Question 1: Saving and resizing your matrix
% Resizing image matrix
yourMatrixResized = imresize(yourMatrix, [512, 512], 'bicubic'); % instead of [512, 512] use your desired height and width
% Save image from your matrix
imwrite(uint8(rescale(yourMatrixResized, 0, 255)), 'yourMatrix.png');
Question 2: Cropping the black part of the image (Download the attached "tocrop.png" image)
img = imread('tocrop.png');
imgGray = rgb2gray(img);
[r, c] = find(imbinarize(imgGray));
row1 = min(r); row2 = max(r);
col1 = min(c); col2 = max(c);
croppedImage = img(row1:row2, col1:col2, :);
figure; imshow(croppedImage, []);

More Answers (0)

Sign in to answer this question.