extract the intersection of two matrices

12 次查看(过去 30 天)
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.

采纳的回答

DGM
DGM 2021-7-24
编辑:DGM 2021-7-24
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)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Colormaps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by