How to change opacity of an image

20 views (last 30 days)
sonicpen
sonicpen on 8 Jan 2022
Edited: DGM on 21 Apr 2022
Hi, I'm trying to watermark an image with a logo on a chosen place with the mouse. So far I'm able to choose the place and watermark the image at the point. But the logo should be transparent and not carry a background with it. I was able to combine the image and the logo with imadd function but this time the logo's opacity drops. Changing the opacity of the logo is something I want to do, but I did nothing to change this in the script below.
img = imread('C:\Users\img.jpg');
logo = imread('C:\Users\logo.png');
[q,w,e] = size(img);
extend = zeros(q,w,3);
extend = im2uint8(extend);
[m,n,c] = size(logo);
imshow(img);
[x,y]=ginput(1);
X=x;
x=round(y);
y=round(X);
extend(x:m+x-1, y:n+y-1, :) = logo;
final = imadd(img,extend,'uint8');
imshow(final);
I though that if I add the two images black pixels of the logo wouldn't matter when I sum the images and background should disappear. It did disappear but there is an opacity drop on it now. How should I combine the logo with the image without background, and how do I change the opacity of the logo are my questions. Thank you.
Final image looks like below;
But I want it to look like this without the black fillings first, and then change the opacity of the logo.
My final image should be this. And then I want to change the opacity of the logo. What should I do?
  1 Comment
sonicpen
sonicpen on 8 Jan 2022
After adding the lines below, I managed to watermark the image without the background. Now all I need is help on changing opacity.
for i=1:q
for j=1:w
for k=1:e
if extend(i,j,k)==0
extend(i,j,k)=img(i,j,k);
end
end
end
end

Sign in to comment.

Accepted Answer

DGM
DGM on 8 Jan 2022
Edited: DGM on 21 Apr 2022
There are many ways to approach it, but if logical masking suffices, simple indexing works:
fg = imread('image.png'); % foreground
bg = imread('peppers.png'); % background
bg = imresize(bg,2);
offset = [100 100]; % [y x]
mask = repmat(rgb2gray(fg)>5,[1 1 3]);
% extract ROI from BG
roirows = offset(1):offset(1)+size(fg,1)-1;
roicols = offset(2):offset(2)+size(fg,2)-1;
roi = bg(roirows,roicols,:);
roi(mask) = fg(mask); % compose FG and ROI
out = bg; % copy BG
out(roirows,roicols,:) = roi; % insert ROI into output
imshow(out)
If it's desirable to adjust FG opacity, that can also be done by multiplication. The ROI is essentially the weighted average of the FG and BG in the region defined by the logical mask.
fg = imread('image.png');
bg = imread('peppers.png');
bg = imresize(bg,2);
offset = [100 100]; % [y x]
fgopacity = 0.5; % range [0 1]
mask = repmat(rgb2gray(fg)>5,[1 1 3]);
roirows = offset(1):offset(1)+size(fg,1)-1;
roicols = offset(2):offset(2)+size(fg,2)-1;
roi = bg(roirows,roicols,:);
roi(mask) = fgopacity*fg(mask) + (1-fgopacity)*roi(mask); % simple opacity
out = bg;
out(roirows,roicols,:) = roi;
imshow(out)
Multiplicative composition is more expensive, but it is generally more flexible. Often, logical masks produce visually poor results due to the hard edges. It may be desirable to have a feathered mask. The opacity parameter can even be incorporated into the mask itself for simplicity. In this example, the composition no longer uses logical indexing; instead, the entire ROI is a weighted average, where the mask itself is a weighting map.
fg = imread('image.png');
bg = imread('peppers.png');
fg = padarray(fg,[1 1],0,'both');
bg = imresize(bg,2);
offset = [100 100]; % [y x]
fgopacity = 0.5;
% feather mask and apply opacity to mask
mask = rgb2gray(fg)>5;
mask = fgopacity*imadjust(imgaussfilt(im2double(mask),1),[0.5 1]);
roirows = offset(1):offset(1)+size(fg,1)-1;
roicols = offset(2):offset(2)+size(fg,2)-1;
roi = bg(roirows,roicols,:);
roi = uint8(mask.*double(fg) + (1-mask).*double(roi)); % fully multiplicative
out = bg;
out(roirows,roicols,:) = roi;
imshow(out)
Attention needs to be paid to image and mask class all throughout these operations.
Nonstandard Tools
The composition process itself can be simplified greatly with basic tools from MIMT. The function replacepixels() simply does multiplicative composition of two images and a supplied mask.
fg = imread('image.png');
bg = imread('peppers.png');
bg = imresize(bg,2);
offset = [100 100]; % [y x]
fgopacity = 0.5;
% pad FG to match BG geometry
padse = imsize(bg,2)-imsize(fg,2)-offset;
fg = addborder(fg,[offset(1) padse(1) offset(2) padse(2)]);
mask = rgb2gray(fg)>5;
mask = fgopacity*imadjust(imgaussfilt(im2double(mask),1),[0.5 1]);
out = replacepixels(fg,bg,mask);
imshow(out)
Alternatively, imblend() can do both blending and compositing of I/IA/RGB/RGBA images. It's trivial to incorporate the mask into the FG to make an RGBA image. The support for RGBA images in MIMT opens the door to simplified workflows. Since imblend() supports scalar opacity as a parameter, the opacity does not even need to be incorporated into the mask. Note that while the output is RGBA, the BG is solid, so the alpha channel can be discarded afterwards. Fair warning, no tools in base MATLAB or Image Processing Toolbox (e.g. imshow(), imwrite()) can handle RGBA images in this form, so you'll have to decouple or discard alpha content when it comes time to write to disk.
fg = imread('image.png');
bg = imread('peppers.png');
bg = imresize(bg,2);
offset = [100 100]; % [y x]
fgopacity = 0.5;
padse = imsize(bg,2)-imsize(fg,2)-offset;
fg = addborder(fg,[offset(1) padse(1) offset(2) padse(2)]);
mask = rgb2gray(fg)>5;
mask = imadjust(imgaussfilt(im2double(mask),1),[0.5 1]);
fg = cat(3,fg,im2uint8(mask)); % add alpha channel to FG
out = imblend(fg,bg,fgopacity,'normal'); % the output here is RGBA
imshow(out(:,:,1:3)) % IPT tools can't handle alpha!
Of course, imblend() is comprehensive, supporting well over a hundred blend modes and several compositing modes, most of which have been parameterized to allow for further flexibility.
out = imblend(fg,bg,fgopacity,'overlay',2);
out = imblend(fg,bg,fgopacity,'equivalence',1);
That's probably enough for now.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!