Crop an image using multiple coordinates

I used the "imfreehand" to crop an irregular shape and save its positions into a variable. This position variable is a 85*2 double matrix (85 points, X and Y coordinates). Now, I want to use the same position to crop another image (different layer of the image, but the location of the objects is the same). The functions I can find all requires rectangle positions (X1,X2,Y1,Y2). In my situation, I have 82 different (X,Y) coordinates, how can I use the position information to crop a new image?

 採用された回答

Matt J
Matt J 2018 年 7 月 7 日
編集済み: Matt J 2018 年 7 月 7 日

0 投票

You should save the original imfreehand object, h.
h=imfreehand(_____)
Then you can just do
cropped = double(AnotherImage).*h.createMask

その他の回答 (2 件)

Image Analyst
Image Analyst 2018 年 7 月 7 日

0 投票

See my freehand copy and paste demo. It will be easy for you to adapt it.
Image Analyst
Image Analyst 2018 年 7 月 7 日

0 投票

"I want to crop another image (different layer of the image)." Perhaps you want to mask all the layers/slices of this or another image. So use bsxfun:
% Mask the image using bsxfun() function to multiply the mask by each channel individually.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
rgbImage can be a color image, or a volumetric (multi-slice) image.

1 件のコメント

Matt J
Matt J 2018 年 7 月 7 日
My answer should cover this as well, if you have R2016b or later.

サインインしてコメントする。

カテゴリ

製品

質問済み:

2018 年 7 月 7 日

編集済み:

2018 年 7 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by