Combine images to create a composite without imfuse

조회 수: 5 (최근 30일)
William Anderson
William Anderson 2019년 3월 5일
답변: DGM 2022년 10월 26일
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.

채택된 답변

darova
darova 2019년 3월 5일
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
William Anderson
William Anderson 2019년 3월 6일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Image Analyst
Image Analyst 2019년 3월 6일
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
William Anderson
William Anderson 2019년 3월 6일
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 2019년 3월 6일
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
DGM 2022년 10월 26일
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.

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by