How do I fuse a JPEG image and a PNG with transparency.
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have a JPG image :
And a PNG image :
I would like to add the png image over the jpg image at certain positions and orientations.
The output I'm aiming for is something like :
I tried the code below but the color is all off and I have no means of repositioning the png image in different orientations and positions. Please advise .
clear all;
img_background = imread('/Users/admin/Documents/Stamper/originals/220px-Benjamin_Franklin2_1895_Issue-1c.jpg');
[img_overlay,map,alpha] = imread('/Users/admin/Documents/Stamper/Imprints/Imprint 1.png');
f= imshowpair(img_background,img_overlay,'blend','Scaling','joint');
1 comentario
Nikunj Kothari
el 31 de En. de 2017
You can try using the
wfusimg
function to fuse the two images after reading them in MATLAB?
Respuestas (1)
DGM
el 26 de Sept. de 2024
Editada: DGM
el 26 de Sept. de 2024
Here's something simple for dealing with the arbitrary overlap.
% inputs
[FG,~,FGa] = imread('cancellation.png');
BG = imread('stamp.jpeg');
% parameters
fgscale = 0.7;
fgos = [-95 75]; % [x y]
% make sure everything is a common class
FG = im2double(FG);
FGa = im2double(FGa);
BG = im2double(BG);
% resize the FG as needed
FG = imresize(FG,fgscale);
FGa = imresize(FGa,fgscale);
% get input, output coordinates
szf = size(FG,1:2);
szb = size(BG,1:2);
xrf = 1:szf(2); % input space
yrf = 1:szf(1);
xrb = intersect(xrf + fgos(1),1:szb(2)); % get intersection in output space
yrb = intersect(yrf + fgos(2),1:szb(1));
xrf = intersect(xrb - fgos(1),xrf); % transform back to input space
yrf = intersect(yrb - fgos(2),yrf);
% composition only needs to happen if the images overlap
outpict = BG;
if ~isempty(xrf) && ~isempty(yrf)
% crop FG and compose output
FG = FG(yrf,xrf,:);
FGa = FGa(yrf,xrf,:);
outpict(yrb,xrb,:) = FG.*FGa + BG(yrb,xrb,:).*(1-FGa);
end
% show the result
imshow(outpict)
0 comentarios
Ver también
Categorías
Más información sobre Read, Write, and Modify Image en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!