String to image (quick)

版本 1.0.2.1 (5.5 KB) 作者: Serge
Convert text string to RGB image (quickly) by using a dictionary of pre-generated images.
429.0 次下载
更新时间 2022/4/9

查看许可证

Two functions str2im and str2imq.
str2im: Convert text string to RGB image.
% str2im %display example text
% str2im(str) %text as char array or cellstr
% str2im(str,pad) %margin: [N] or [H V] or [L R T B] as pixels or nan
% str2im(__,props) %text property as value pairs, doc text
% I = str2im(__) %return RGB image data as uint8
% [I,A] = str2im(__) %return background alpha channel as uint8
% [I,A,H,W] = str2im(__) %return image height and width
%
%Remarks:
%-If a margin is nan then the background is cropped up to the text.
%-Slow because nothing is cached, a figure is generated for each call and
% print is used to generate the image (not getframe).
%-Maximum image size is limited by screen resolution.
Example:
clf,I=str2im({'str' '2im'},[10 8 -6 -5],'Color','b','Background','y','FontName','FixedWidth','FontWeight','bold')
clf,str2im('\phi_k^\pi',nan,'FontSize',200,'interpreter','tex')
clf,str2im('$$\int_0^2x^2\sin(x)dx$$',nan,'interpreter','latex')
Example: burn text into image
[I,A,H,W]=str2im(datestr(now),[0 0 -6 -3],'FontName','FixedWidth','Color','y','Background','k');
im=imread('peppers.png');x=size(im,2)-W;y=size(im,1)-H;im(y+(1:H),x+(1:W),:)=im(y+(1:H),x+(1:W),:)*0.6+I;imshow(im)
str2imq: Quickly convert text to image by making and using an image dictionary.
% str2imq([]) -clear the cache
% str2imq(L,P) -cache text & properties
% D = str2imq() -get dictionary
% I = str2imq(T,P) -convert text to image using cache
% [I,A] = str2im(__) -return alpha (see str2im)
% [I,A,H,W] = str2im(__) -return height & width (see str2im)
%L: List of letters (char) or list string fragments (cellstr).
%P: Text padding & text properties (see str2im).
%D: Image dictionary of letters/string fragments (struct).
%T: Can also pointers at dictionary entries (integer vector).
%
%Remarks:
%-Dictionary images must have same height to concatenate horizontally.
%-Dictionary and text properties are cached as persistent variables.
%-Missing dictionary element will get appended on the fly (SLOW).
%-Text properties must be exactly same: property order, spelling, etc.
Example:
[I,A]=str2imq('Hello!','FontSize',80); %fast repeat calls
imagesc(I,'AlphaData',A), axis off equal
Example: string fragments
imagesc(str2imq({'\pi' '\int' 'xyz'},'Interpreter','tex')), axis equal tight
Example: clock
str2imq('0123456789:.',[0 0 0 0],'FontName','FixedWidth') %init
while 1,imagesc(str2imq(datestr(now,'HH:MM:SS.FFF'))),drawnow,end %play

引用格式

Serge (2024). String to image (quick) (https://www.mathworks.com/matlabcentral/fileexchange/60133-string-to-image-quick), MATLAB Central File Exchange. 检索来源 .

MATLAB 版本兼容性
创建方式 R2022a
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Convert Image Type 的更多信息

Community Treasure Hunt

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

Start Hunting!
版本 已发布 发行说明
1.0.2.1

Description

1.0.2

Added str2imq

1.0.1

Fixed pad feature by replacing buggy getframe with a print command. Much slower now...

1.0.0.0

Minor changes
Minor changes
Fix to keep window on top

Minor changes
Minor changes