How do I fuse a JPEG image and a PNG with transparency.

3 次查看(过去 30 天)
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 个)

DGM
DGM 2024-9-26
编辑:DGM 2024-9-26
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)

类别

Help CenterFile Exchange 中查找有关 Read, Write, and Modify Image 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by