How does matlab add transparent fonts to pictures?

14 ビュー (過去 30 日間)
cui,xingxing
cui,xingxing 2021 年 9 月 13 日
編集済み: DGM 2024 年 4 月 27 日
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?

採用された回答

cui,xingxing
cui,xingxing 2021 年 9 月 13 日
編集済み: cui,xingxing 2024 年 4 月 27 日
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

その他の回答 (1 件)

DGM
DGM 2024 年 4 月 27 日
編集済み: DGM 2024 年 4 月 27 日
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:

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by