# Extract pixel values inside a Binary Mask

75 views (last 30 days)
ali eskandari on 11 Jun 2021
Answered: Joseph Cheng on 11 Jun 2021
I have an image and the ROI points. I did the following to extract the ROI by creating a binary mask with poly2mask. now I want to extact the values of masked image.
S = size(img); % Size of the binary masked
ROI = img;
ROI(bw == 0) = 0;
ROI = ROI(ROI>0); % Just keep the non-zero values

Joseph Cheng on 11 Jun 2021
since you already created a binary mask you can just index multiply " .* " the mask with the original image as outside is 0 and inside the mask is 1.
%dummy image for example
bwimg = single(rgb2gray(img));
[r c]=size(bwimg);
figure(1),subplot(131),imagesc(bwimg); colormap gray
title('original image')
x = c*rand(1,4);
y = r*rand(1,4);
x(end+1) = x(1);
y(end+1) = y(1);
subplot(132),imagesc(bw);colormap gray
%extractROI
%index multiply image with mask as outside is 0 and inside is 1
ROI=bwimg.*bw;

### Categories

Find more on Region and Image Properties in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by