Cropping out the image/generating image the same size as the matrix it comes from
1 次查看(过去 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 !
0 个评论
采纳的回答
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!