How does matlab add transparent fonts to pictures?

44 visualizzazioni (ultimi 30 giorni)
As far as I know, only "text", "insertText" function can add text to the picture, I prefer to use "insertText" to add font to the existing picture, but I want the font to be semi-transparent, the function lacks " transparent" property specified, where "BoxOpacity" is not the property I want, how do I do it?

Risposta accettata

cui,xingxing
cui,xingxing il 13 Set 2021
Modificato: cui,xingxing il 27 Apr 2024 alle 2:33
After trying to figure it out, I gave the answer , for example:
% your params
img = imread('printedtext.png');
Transparency = 0.6;
fontColor = [1,1,1]; % RGB,range [0,1]
position = [700,200];
%% add watermark
mask = zeros(size(img),'like',img);
outimg = insertText(mask,position,'china', ...
'BoxOpacity',0,...
'FontSize',200,...
'TextColor', 'white');
bwMask = imbinarize(rgb2gray(outimg));
finalImg = labeloverlay(img,bwMask,...
'Transparency',Transparency,...
'Colormap',fontColor);
imshow(finalImg)
-------------------------Off-topic interlude, 2024-------------------------------
I am currently looking for a job in the field of CV algorithm development, based in Shenzhen, Guangdong, China,or a remote support position. I would be very grateful if anyone is willing to offer me a job or make a recommendation. My preliminary resume can be found at: https://cuixing158.github.io/about/ . Thank you!
Email: cuixingxing150@gmail.com

Più risposte (1)

DGM
DGM il 27 Apr 2024 alle 8:17
Modificato: DGM il 27 Apr 2024 alle 10:58
I wouldn't do it that way. You'd necessarily have to binarize the input to labeloverlay() because of the way it's being used, but that means you lose all your antialiasing -- which is one of the advantages to using insertText() when compared to other ways of inserting text.
You could do this by composition, using insertText() to create the mask, but for the given task, it can be simplified further. Here's an example of using basic composition to preserve antialiasing.
% your params
img = imread('peppers.png');
fontColor = [0.3,1,0.7]; % RGB, unit-scale float
fontAlpha = 0.7; % unit-scale float
position = [75,100];
textstr = 'pepper';
% insertText()/insertShape() don't interpret color tuples sensibly
% so you'll either have to force the image to float,
% or you'll have to conditionally rescale the tuple --
% for which there is no convenient method in base/IPT/CVT.
BG = im2double(img);
% add opaque watermark to one copy of the image
FG = insertText(BG,position,textstr, ...
'BoxOpacity',0,...
'FontSize',100,...
'TextColor', fontColor);
% compose the image
% this works so long as both images are float
outimg = FG.*fontAlpha + BG.*(1-fontAlpha);
% zoom in on a spot
zoomed = imresize(outimg(150:250,100:300,:),4,'nearest');
imshow(zoomed,'border','tight')
One of the caveats of using insertText() and insertShape() is that they don't interpret color tuples in a helpful manner. To make things worse, the webdocs are wrong (see the link below). The color tuples are interpreted relative to the scale implied by the class of the input image, not the color tuple itself. You must always take external measures to ensure that your images and parameters are all on a common scale, regardless of the class of the color tuples. Again, see the link below for details.
Other ways of inserting text into images:
On the ridiculousness of CVT parameter scale conventions:
On the limitations of trying to misuse imoverlay()/labeloverlay() as more general composition tools:

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by