Combine images to create a composite without imfuse

5 vues (au cours des 30 derniers jours)
William Anderson
William Anderson le 5 Mar 2019
Réponse apportée : DGM le 26 Oct 2022
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 = A.png B =B.png C = C.png
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 = D.png
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.

Réponse acceptée

darova
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
William Anderson
William Anderson le 6 Mar 2019
Thank you darova, this works great and gives me image D as intended.
I believe you are right about the black colour in the overlay being a problem, as the transparency is likely 'none' and therefore black [0, 0, 0]. Loading the images prior with B being a transparent file, as I have below, appears to correct it for me.
A = imread(A)
B = imread(B, 'BackgroundColor', 'none')
Alternatively you may be able to replace the 'none' statement with another colour map, or when generating B, as I have shown below, use another colour than black.
B = imwrite(B, File_Location, 'Transparency', [0 0 0])
The above statement would also work for C, if you wanted to maintain the transparency in both A & B (for example you wanted to combine transparent overlays.

Connectez-vous pour commenter.

Plus de réponses (2)

Image Analyst
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
William Anderson
William Anderson le 6 Mar 2019
I found difficulty when trying to create a circle (and similarly a single dot) using plot, as even with the minimum line and marker width options (set 0<x<1) the lines drawn would have a greater width than 1 pixel, which would end up bleeding over the background image similar to that seen using imfuse's blending method. Did I miss something in one of the plot options to only plot points as single pixels? Would you have to resize the axes in some way?
Image Analyst
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.

Connectez-vous pour commenter.


DGM
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.

Catégories

En savoir plus sur Images dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by