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カテゴリ
- MATLAB > Graphics > Images > Convert Image Type >
Help Center および MATLAB Answers で Convert Image Type についてさらに検索
タグ
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!