Combine images to create a composite without imfuse
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Is there a way to overlay an image file onto another, without using imfuse?
I have a transparent image file I wish to overlay on top of another image file that I wish to visible in the background, however I find the 'blend' method used in the imfuse function distorts the overlay pixels too much. This is the closest I have come using imfuse. The order of A (the background) & B (the overlay) has had no effect.
C = imfuse(imread(A), imread(B, 'BackgroundColor', 'none'), 'blend');
imwrite(C, fullfile(File_Folder, Output_Name));
A =
B =
C = 



The transparent image I wish to overlay is of the edge detection results of the background image, which I have coloured to better highlight the outline. Noticeably in C, the gray seen in A is darker. The green seen in B is know no longer solid/consistent and I suspect the pixels may also be distorted. Preferably I would like to create an image that looks like D below (which I have created externally).
D = 

I intend to repeat this part in my process many times as part of a loop, so it would be preferable to keep imwrite and not use a figure & "hold on" approach if possible. I have tried using array structures, but have struggled with using colour mapping.
Thank you for any possible help.
0 commentaires
Réponse acceptée
darova
le 5 Mar 2019
Background == 'none' in matrix is [0 0 0]
[m, n, ~] = size(A);
C = A;
for i = 1:m
for j = 1:n
if sum(B(i,j,:)) % if pixel is not background
C(i,j,:) = B(i,j,:);
end
end
end
But if overlay image has black color [0 0 0] it would be a problem
1 commentaire
Plus de réponses (2)
Image Analyst
le 6 Mar 2019
I think the best way would be to just find the green circle via bwboundaries() or imfindcircle() and then plot the coordinates in the overlay with plot().
2 commentaires
Image Analyst
le 6 Mar 2019
You're not going to get subpixel resolution on your computer display. If you zoom way WAY in, true, you'll see plot go from pixel center to pixel center, and if that bothers you or your user, you can use an image overlaid onto it.
DGM
le 26 Oct 2022
Imfuse() is a simple visualization tool with limited flexibility. It's not a practical image compositing tool.
For an image with what is essentially binarized alpha, this is fairly simple.
% load the images
A = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/206952/A.png');
B = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/206953/B.png');
% create a logical mask, expand it
mk = ~all(B==0,3);
mk = repmat(mk,[1 1 3]);
% do composition by logical indexing
C = A;
C(mk) = B(mk);
imshow(C)
If B had a proper alpha channel, you could just use that instead of needing to generate the mask, though bear in mind that this example still only accomodates a binarized composition.
0 commentaires
Voir également
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!