What to do if insertText doesn't work, but there are no error codes?

9 views (last 30 days)
I'm trying to add text to an image I have but the insertText function isn't working. No errors show up when I run the Matlab script, but neither does the text. Here is what I have:
t = Tiff('TEmp.tiff', 'r'); % Create .tiff object
figure
imageData = read(t); % Create image data
imageData = imageData(:,:,1:3); %remove redundant data
imshow(imageData) % Show the image in a figure
hold on
% Plot a line.
maxy = 300;
a=[0 maxy];
b = [0 maxy];
plot(a, b, 'b')
hold on
RGB = insertText(imageData,[1 150],'Hello','FontSize',30,'BoxColor','red')
When I run this, the line found using plot(a,b) shows up, but the phrase 'Hello' does not. Any idea why this isn't working? I have the Computer Vision System Toolbox installed.

Accepted Answer

Cris LaPierre
Cris LaPierre on 23 Dec 2021
insertText essentially creates a new image. You need to add another imshow command for RGB to see it. See the examples here.
Here is your code modified to run here.
t = Tiff('cameraman.tif', 'r'); % Create .tiff object
imageData = read(t); % Create image data
% imageData = imageData(:,:,1:3); %remove redundant data
imshow(imageData) % Show the image in a figure
hold on
% Plot a line.
maxy = 300;
a=[0 maxy];
b = [0 maxy];
plot(a, b, 'b')
hold off
figure
RGB = insertText(imageData,[1 150],'Hello','FontSize',30,'BoxColor','red');
imshow(RGB)

More Answers (1)

yanqi liu
yanqi liu on 24 Dec 2021
yes,sir,may be use text to add text object on figure,or use insertText to generate a new image and imshow by another figure,such as
clc; clear all; close all;
t = Tiff('example.tif', 'r'); % Create .tiff object
figure
imageData = read(t); % Create image data
imageData = imageData(:,:,1:3); %remove redundant data
imshow(imageData) % Show the image in a figure
hold on
% Plot a line.
maxy = 300;
a=[0 maxy];
b = [0 maxy];
plot(a, b, 'b')
hold on
RGB = insertText(imageData,[1 150],'Hello','FontSize',30,'BoxColor','red');
h = text(1,150,'Hello','FontSize',30,'Color','red','EdgeColor','red');
figure; imshow(RGB, []);

Community Treasure Hunt

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

Start Hunting!