How do I fuse a JPEG image and a PNG with transparency.
2 views (last 30 days)
Show older comments
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 Comment
Nikunj Kothari
on 31 Jan 2017
You can try using the
wfusimg
function to fuse the two images after reading them in MATLAB?
Answers (1)
DGM
on 26 Sep 2024
Edited: DGM
on 26 Sep 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 Comments
See Also
Categories
Find more on Read, Write, and Modify Image in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!