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

조회 수: 2 (최근 30일)
abishekh
abishekh 2017년 1월 24일
편집: DGM 2024년 9월 26일
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
Nikunj Kothari 2017년 1월 31일
You can try using the
wfusimg
function to fuse the two images after reading them in MATLAB?

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

답변 (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