String to image (quick)

버전 1.0.2.1 (5.51 KB) 작성자: Serge
Convert text string to RGB image (quickly) by using a dictionary of pre-generated images.
다운로드 수: 432
업데이트 날짜: 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