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

조회 수: 2 (최근 30일)
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

채택된 답변

Subhadeep Koley
Subhadeep Koley 2020년 2월 10일
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, []);

추가 답변 (0개)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by