MATLAB Answers

extract the intersection of two matrices

2 views (last 30 days)
ali eskandari
ali eskandari on 24 Jul 2021
Edited: DGM on 24 Jul 2021
Hi
I have two matrices, one is binary (A) and other one with values between [0-1] (B). My aim is to extract the intersection of these two matrices. I did the following but I don't know why another area has been added to it.
Binary image:
imshow(A)
Masked image-binary image
B:
imagesc(B)
A = double(A);
A(A==0) = -1; % Change zeros to -1 to keep the zero values in matrix B
A = A.*B;
imagesc(A,'alphadata',A>=0);
caxis([0 1])
colormap jet
here is the output image, and some extra pixels have been selected as well.

Accepted Answer

DGM
DGM on 24 Jul 2021
Edited: DGM on 24 Jul 2021
I have a feeling that the range of data in B isn't what you think it is. Using the compositing method involving -1 is probably causing problems. If B contains very small negative values near zero, A.*B will result in those regions becoming positive. Then when you set your alpha data to A>=0, those regions show up.
Let me see if I can make an example.
B = repmat(linspace(-0.1,1,256),[256 1]); % the image (numeric)
A = B>0.4 & B<0.6; % the mask (logical)
A = double(A);
A(A==0) = -1; % Change zeros to -1 to keep the zero values in matrix B
C = A.*B;
imagesc(C,'alphadata',C>=0);
caxis([0 1])
colormap jet
Note the extra area selected on the left. It's also possible that the issue isn't negative values in the image or mask, but simply the fact that A==0 will fail to select any near-zero (positive) values in a floating point copy of what should properly be a logical mask.
If you just want to mask off everything but what's defined by the (logical) mask, just use the mask.
B = repmat(linspace(-0.1,1,256),[256 1]); % the image (numeric)
A = B>0.4 & B<0.6; % the mask (logical)
imagesc(B,'alphadata',A)
colormap(jet)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!