Clear Filters
Clear Filters

Imfuse 'blend' issues

5 views (last 30 days)
Christopher Samaan
Christopher Samaan on 19 Mar 2018
Edited: DGM on 5 Jun 2024
I have a function that takes two images and compares their overlapping areas using the imfuse 'blend' command. My function works most of the time, as it relies on the overlapping area to be black while the non-overlapping area is grey. However, in some cases, when I use the fuse command on two images, one of the two resulting images will remain completely black, even if it does not overlap the second image. This leads to my function not working when this happens, as my function depends on the non-overlapping area to be grey. I have attached both an example of the blend command where the images blend how I want them to, and I have attached an example of the blend command where the images do NOT blend how I want them to. Here is my function:
function [PSame,PDiff] = Comparison(FinalImage2,RotatedImage)
FI2=FinalImage2;
RI=RotatedImage;
Combined=imfuse(FI2,RI,'blend');
imshow(Combined);
Total=sum(Combined(:)~=255);
Same=sum(Combined(:)==0);
Diff=sum(Combined(:)==128);
IM1=sum(FinalImage2(:)==0);
IM2=sum(RotatedImage(:)==0);
PSame=(Same/Total)*100;
PDiff=(Diff/Total)*100;
PIM1=((IM1-Same)/Total)*100;
PIM2=((IM2-Same)/Total)*100;
fprintf("Percent Same: %0.2f \n",PSame)
fprintf("Percent Different: %0.2f \n",PDiff)
fprintf("Percent Just Image 1: %0.2f \n",PIM1)
fprintf("Percent Just Image 2: %0.2f \n",PIM2)
end
If you need any more information, please let me know! Thanks!

Answers (1)

DGM
DGM on 5 Jun 2024
Edited: DGM on 5 Jun 2024
Like most cases, I don't see the point of using imfuse() here. If you're treating the images as if they're binarized, make sure they're binarized, and handle them using basic logical operations.
A = imread('monojagblob.png');
B = imread('tiltellipse.png');
B = imresize(B,size(A,1:2));
% if you want to visualize, imfuse() is a convenience
imshow(imfuse(A,B))
% the actual programmatically useful output doesn't require imfuse()
[PSame,PDiff] = Comparison(A,B,'positive') % the object is white on black
Percent Same: 44.89 Percent Different: 55.11 Percent Just Image 1: 13.67 Percent Just Image 2: 41.44
PSame = 44.8899
PDiff = 55.1101
[PSame,PDiff] = Comparison(A,B,'negative') % the object is black on white
Percent Same: 81.36 Percent Different: 18.64 Percent Just Image 1: 14.01 Percent Just Image 2: 4.62
PSame = 81.3623
PDiff = 18.6377
function [PSame,PDiff] = Comparison(A,B,polarity)
% instead of presuming that both images are necessarily uint8
% just work in unit-scale and ignore any quirks of imfuse()
% you're also assuming that the images are strictly binarized
% and contain no antialiasing or compression artifacts.
% if you want to treat things as binarized, make sure they are.
A = im2double(A)>0.5; % objects are light-on-dark features
B = im2double(B)>0.5;
% flip polarity if required
if nargin > 2
switch lower(polarity)
case 'positive'
% do nothing
case 'negative'
A = ~A;
B = ~B;
otherwise
error('write some error message here')
end
end
Total = nnz(A | B); % union (either)
Same = nnz(A & B); % intersection (both)
Diff = nnz(xor(A,B)); % disjunctive union (either, but not both)
areaA = nnz(A); % the total area of A
areaB = nnz(B); % the total area of B
% as percentages
PSame = (Same/Total)*100;
PDiff = (Diff/Total)*100;
PIM1 = ((areaA-Same)/Total)*100;
PIM2 = ((areaB-Same)/Total)*100;
fprintf("Percent Same: %0.2f \n",PSame)
fprintf("Percent Different: %0.2f \n",PDiff)
fprintf("Percent Just Image 1: %0.2f \n",PIM1)
fprintf("Percent Just Image 2: %0.2f \n",PIM2)
end

Community Treasure Hunt

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

Start Hunting!