Clear Filters
Clear Filters

Extract part from an RGB image based on the obtained pixel locations.

2 views (last 30 days)
I had an RGB image and I converted this to a binary image. Then, I got desired pixel locations from a binary image. I want to extract that part from the same RGB image (from which binary image was developed) which lies in the obtained desired location mentioned above.
Can anyone suggest me some way out?

Accepted Answer

DGM on 23 Sep 2023
Edited: DGM on 23 Sep 2023
"Extract" tends to be a word that gets used in ways that make it difficult to know what anyone means when they use it. Here are three different interpretations.
% you have an RGB image
inpict = imread('peppers.png');
% and a logical mask
mask = imread('redpepmask.png')>128;
% you could apply the mask to the image
% this is just cosmetic and typically isn't very useful
% for processing purposes
maskedpict = inpict.*cast(mask,class(inpict));
% or you can extract only those pixels as a color table
% bear in mind that this will retain the class and scale of inpict
% and so generally won't be compatible with other tools that handle color tables
% unless rescaled to unit-scale float.
selectedpixels = inpict(repmat(mask,[1 1 size(inpict,3)]));
selectedpixels = reshape(selectedpixels,[],3)
selectedpixels = 18944×3
187 66 0 186 63 0 185 61 0 182 59 0 181 56 0 181 55 0 179 52 2 179 50 3 176 51 0 170 46 0
% if you want a picture that contains only the ROI content,
% you can do that, but it's usually not what people think they want.
f = factor2(size(selectedpixels,1),'ordered',false);
onlythepepper = reshape(selectedpixels,[f(end,:) 3]);
DGM on 24 Sep 2023
Depending on your familiarity with the concepts, perhaps I could have elaborated.
We want to blank part of an array by filling it with zeros. There are two basic approaches: indexing and multiplication.
For indexing, there are multiple ways. Say we start with a logical mask:
% the inputs
Agray = imread('sadberry_gray.png'); % a single-channel uint8 image
mask = imread('sadberry_mask.png'); % a mask selecting the _background_
% you could do this using logical indexing
B1 = Agray;
B1(mask) = 0;
% or you could convert the logical mask to linear indices
idx = find(mask);
B2 = Agray;
B2(idx) = 0;
% or you could convert the logical mask to subscripts
[y x] = find(mask);
% ... at which point you'd have to either use a set of loops
% or convert the subscripts back to linear indices
idx = sub2ind(size(mask),y,x);
B3 = Agray;
B3(idx) = 0;
If we're creating a logical output from some test, it's usually easiest to use the logical array directly. There are some complications in this application though. Because our image is RGB, it doesn't have the same size as the mask. It has the same page geometry, but it has 3 pages instead of 1.
If we were to replicate the above example with logical indexing, we'd need to expand the mask to match, otherwise we'd only select the first page.
% an RGB input
Argb = imread('sadberry.png');
% use logical indexing with an expanded mask
Brgb1 = Argb;
Brgb1(repmat(mask,[1 1 3])) = 0;
Alternatively, instead of directly using the mask for indexing, we can implicitly use it as a numeric array and multiply it with the image. In modern versions of MATLAB with implicit array expansion, this obviates the need to expand the mask, but it does present the problem of mixed-class arithmetic. In order to avoid that issue, we can explicitly cast the mask to an appropriate numeric class.
% image composition via multiplication
% this works for both gray or RGB
B4 = Agray.*cast(~mask,class(Agray));
Brgb2 = Argb.*cast(~mask,class(Argb));
Alternatively, if the image is always float class, the explicit casting of the mask isn't strictly necessary.

Sign in to comment.

More Answers (0)


Find more on Modify Image Colors 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!