MATLAB vision.TextInserter 不插入文本

MATLAB vision.TextInserter does not insert text

我正在尝试将文本消息插入到图像中。它不显示,我也没有收到任何错误消息。

close all
clear all
clc

hVideoPlayer = vision.VideoPlayer;
hVideoFileReader = vision.VideoFileReader;
hVideoFileReader.Filename = 'img1.jpg';

frame = step(hVideoFileReader);


hcsc = vision.ColorSpaceConverter;
hcsc.Conversion = 'RGB to intensity';
frame = step(hcsc, frame);

at = vision.Autothresholder;
frame = step(at, frame);


ccl = vision.ConnectedComponentLabeler;
[L NUM] = step(ccl, frame);

holeObjCount = 0;

for i=1:NUM
    framei = changem(L==i, 1, i);

    framei = imcomplement(framei);

    [Li NUMi] = step(ccl, framei);

    if NUMi > 1
        holeObjCount = holeObjCount + 1;
    end

end

message = sprintf('%d of %d objects have holes.', holeObjCount, NUM);
disp(message);

osdMsg = vision.TextInserter('%d of %d objects have holes.',... 
                             'Color', uint8([255, 255, 255]), ...
                             'Location', [10 10],...
                             'FontSize', 22);
%The problem is here
frame = step(osdMsg, frame, int32([holeObjCount NUM]));

step(hVideoPlayer, frame); 

release(hVideoFileReader);

release(hVideoPlayer);

问题是因为图像的颜色 space。我试图在无法自然显示的 B/W 图像上显示 RGB 彩色文本。

删除颜色属性后,我可以看到文字

osdMsg = vision.TextInserter('%d of %d objects have holes.',... 
                             'Location', [10 10],...
                             'FontSize', 22);

现在的问题是,插入的文字是黑色的,看不清。

问题是阈值处理后frame是一个逻辑数组。要显示文本,请使用 im2uint8 将其转换为 uint8.

其他一些提示:由于您正在处理单个图像而不是视频,因此您可以使用 imread 而不是 vision.VideoFileReader 来读入它。您也可以使用 imshow 而不是 vision.VideoPlayer 出于同样的原因。此外,如果您的 MATLAB 版本为 R2013a 或更高版本,则可以使用 insertText 函数代替 vision.TextInserter.