没有计算机视觉系统工具箱将文本插入图像 - Matlab

时间:2014-10-15 14:57:32

标签: matlab matlab-cvst

在Matlab中心(http://www.mathworks.com/matlabcentral/answers/26033-how-to-insert-text-into-image),我找到了这段代码:

Text = sprintf('Create text inserter object \nwith same property values');
H = vision.TextInserter(Text);
H.Color = [1.0 1.0 0];
H.FontSize = 20;
H.Location = [25 25];
I = im2double((imread('football.jpg')));
InsertedImage = step(H, I);
imshow(InsertedImage);

如果不使用计算机视觉系统工具箱,我该怎么做呢?

由于

2 个答案:

答案 0 :(得分:4)

跟进我的评论:

A = imread('football.jpg');

YourText = sprintf('Create text inserter object \nwith same property values');

figure;

imshow(A);

hText = text(25,25,YourText,'Color',[1 1 0],'FontSize',20);

给出以下内容:

enter image description here

查看doc页面以查看可用于修改/自定义文本的所有选项。请注意,您不必使用sprintf来生成字符串,但必须在\n内使用换行符(sprintf)才能生效。

编辑为了回应下面的评论,如果您需要保存包含文字的图片,您可以使用getframe获取图中的内容,然后通过imwrite保存:

hFrame = getframe(gca) %// Get content of figure

imwrite(hFrame.cdata,'MyImage.tif','tif') %// Save the actual data, contained in the cdata property of hFrame

编辑#2 作为使用getframe的替代方案,请查看here,因为提出了2个不错的想法,即使用saveas或avi文件。

答案 1 :(得分:0)

上面的答案(Benoit_11)要求您首先显示图像,写入文本,然后保存图像。如果您正在处理视频的帧(不是单个图像或其中一些图像),这将变得非常慢。要获得更快的处理时间,您必须直接写入图像矩阵。我能想到的另一种选择(但不是很优雅)是为字母字符(例如20x20)创建(或下载)小模板,然后用这些模板覆盖所需区域中的图像矩阵。例如,对于RGB“真彩色”图像,我们将具有以下内容:

im(y:y+19,x:x+19,:)=template;