How can I index Image pixel with 2d logical mtrix ?

10 views (last 30 days)
I want to index image with 2d matrix logical indexing data.
I attaced the file please help me...
  1 Comment
Akira Agata
Akira Agata on 22 Oct 2020
What do you mean by "index image" with 2d logical matrix?
Is that something like this?
imshowpair(BEV,Red_idx)

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 22 Oct 2020
output = BEV(Red_idx);
is valid MATLAB, and gives a 64x1 vector.
When you index an array by a smaller logical vector, the missing entries are assumed to be false (not selected), so it is valid MATLAB to index a 3D array by a 2D logical array, and the result would be the same as indexing the first pane of the 3D array by the 2D logical array.
If you have a hard requirement to not use more than two dimensions in the logical array index, and you want to get all three of the image panes, then you will need to do something like
temp = BEV(:,:,1);
output(:,1) = temp(Red_idx);
temp = BEV(:,:,2);
output(:,2) = temp(Red_idx);
temp = BEV(:,:,3);
output(:,3) = temp(Red_idx);
but I would recommend that you instead give up indexing with a 2D logical array and make it a 3D logical array instead:
output = reshape(BEV(repmat(Red_idx,1,1,3)), [], 3);
It would not be common that you would want to do this kind of indexing. More common by far would be:
output = BEV.*repmat(uint8(Red_idx),1,1,3);
which would create an RGB array the same size as BEV but with the pixels not selected set to black.
Also common would be to crop out the section corresponding to the selected pixels, such as
mask = any(Red_idx,1);
fc = find(mask,1);
lc = find(mask,1,'last');
mask = any(Red_idx,2);
fr = find(mask,1);
lr = find(mask,1,'last');
output = BEV(fr:lr, fc:lc, :);

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!