How to crop a masked area (polygon shape) out of an image?

31 次查看(过去 30 天)
Hi all,
Now my code is like below:
I = imread('moon.tif');
figure;
imshow(I)
Poly_Mask = images.roi.Polygon(gca,'Position',[100 150; 200 250; 300 350; 150 450]);
As you can see, the example image is now masked with a polygon shape. I would like to crop the masked area (polygon shape) out of the image. Could you please suggest me how to do this? Thank you.

采纳的回答

Image Analyst
Image Analyst 2022-3-30
Try this:
grayImage = imread('moon.tif');
[rows, columns, numberOfColorChannels] = size(grayImage)
rows = 537
columns = 358
numberOfColorChannels = 1
subplot(2, 1, 1);
imshow(grayImage, []);
axis('on', 'image');
title('Original Image')
x = [100, 200, 300, 150];
y = [150, 250, 350, 450];
% tack on starting point to close it so the plot is not oepn jaw. (Only
% needed if you wan to plot the outline in the overlay.
x = [x, x(1)];
y = [y, y(1)];
hold on;
plot(x, y, 'r-', 'LineWidth', 2);
mask = poly2mask(x, y, rows, columns);
% Erase outside mask by setting to zero
grayImage(~mask) = 0;
% Crop
croppedImage = grayImage(min(y):max(y), min(x):max(x));
subplot(2, 1, 2);
imshow(croppedImage)
axis('on', 'image');
title('Cropped Image')
  2 个评论
Chutiphon Moranon
Chutiphon Moranon 2022-3-30
编辑:Chutiphon Moranon 2022-3-30
Thank you so much. I would like to ask for the method to make the black background transparent.
As I replied to Faraz, I would like the image to be transparent for being used with the normxcorr2 function for more accuracy. Will it be possible and how? Thank you.
Image Analyst
Image Analyst 2022-3-30
You can't make it transparent, and it does not need to be for any reason that I can think of, certainly not for normxcorr2().
You can have any shape you want just by putting in the coordinates of the shape boundaries into poly2mask().

请先登录,再进行评论。

更多回答(1 个)

Tala
Tala 2022-3-29
编辑:Tala 2022-3-29
I cannot see your image. But I used a photo of my dog (because she wont sue me :D) to show the concept:
I=rgb2gray(imread("Tala1.jpg"));
x=[100 200 300 150];% your ROI x values
y=[150 250 350 450];% your ROI y values
[rows, columns,~] = size(I);
mask = imcomplement(poly2mask(x, y, rows, columns));
mul = immultiply(I,mask);
imshow(mul)
  3 个评论
Tala
Tala 2022-3-30
编辑:Tala 2022-3-30
do you need to crop a polygon? you could crop out a rectangular section of image with imcrop(I, [x y dx dy]) and then calculate corrolation or convolution for feature matching.
Chutiphon Moranon
Chutiphon Moranon 2022-3-30
Thank you for your reply. I will use normxcorr2 as you suggested. However, I would like to use an input image that is not rectangular in shape.
I am trying to crop an input image, only the part area I want for more accurate correlation. Therefore the background area of the cropped image will be transparent as the image below. I am not sure whether this method will provide good result but I will try it. Please feel free to provide your suggestions.Thank you.

请先登录,再进行评论。

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by