フィルターのクリア

How can I extract a portion of an image specified by mask generated from a ROI in Image Processing Toolbox 7.3 (R2011b)?

12 ビュー (過去 30 日間)
I have an image where I have specified a region of interest (ROI) using functions such as IMELLIPSE or IMFREEHAND. I would like to extract the part of the image contained within the region specified by such a function.

採用された回答

MathWorks Support Team
MathWorks Support Team 2012 年 2 月 14 日
We first create a binary mask from a specified ROI, which returns a matrix containing only 1 or 0. To return the selected portion of the image, we perform a logical AND of the binary mask matrix with the image using logical indexing. The resulting image will contain the extracted portion.
The steps below provide an example of this process. Here, we use a sample image provided by the Image Processing Toolbox.
% 1) Open the image file as a workspace variable:
img = imread('peppers.png');
% 2) Display the image and store its handle:
h_im = imshow(img);
% 3) Create an ellipse defining a ROI:
e = imellipse(gca,[55 10 120 120]);
% 4) Create a mask from the ellipse:
BW = createMask(e,h_im);
% 4a) (For color images only) Augment the mask to three channels:
BW(:,:,2) = BW;
BW(:,:,3) = BW(:,:,1);
% 5) Use logical indexing to set area outside of ROI to zero:
ROI = img;
ROI(BW == 0) = 0;
% 6) Display extracted portion:
figure, imshow(ROI);
  1 件のコメント
Image Analyst
Image Analyst 2018 年 7 月 3 日
Yes. Just use BW as a logical index for img (or ROI, the copy of img):
img(BW) = 0; % Set img = 0 inside the BW mask.
See attached demos to learn more.

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

その他の回答 (1 件)

michael scheinfeild
michael scheinfeild 2019 年 2 月 26 日
if you have some mask (nr*nc image)use find where the indexes are 1 in the mask only for rectangular mask !
okind=find(Mask>0);
[ii,jj]=ind2sub(size(Mask),okind);
ymin=min(ii);ymax=max(ii);xmin=min(jj);xmax=max(jj);
imCropped=imcrop(Y,[xmin,ymin,xmax-xmin+1,ymax-ymin+1]);

製品


リリース

R2011b

Community Treasure Hunt

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

Start Hunting!

Translated by