How do I fuse a JPEG image and a PNG with transparency.
2 ビュー (過去 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 件のコメント
Nikunj Kothari
2017 年 1 月 31 日
You can try using the
wfusimg
function to fuse the two images after reading them in MATLAB?
回答 (1 件)
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)
0 件のコメント
参考
カテゴリ
Help Center および File 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!