How do I make text color black when using the function insertText

Hello I created a white image using the code below. and then i inserted a text. but the text has yellow background. I dont know where this is coming from
N=500;
M=500;
whiteImage = 255 * ones(N, M, 'uint8');
DD= insertText(whiteImage, [100,234],'Hello World','FontSize',18,'TextColor','black');
imshow(DD);
Please help, how do I remove the yellow background under the text

 Respuesta aceptada

Read the rest of the documentation...
'BoxColor' Text box color
'yellow' (default) | character vector | cell array of character vectors | [R G B] vector | M-by-3 matrix
Use
DD= insertText(whiteImage, [100,234],'Hello World','FontSize',18,'TextColor','black','BoxColor','white');
It seems a more logical 'Default' choice would be the existing background color of the image, but I guess that's probably more difficult to ascertain...

8 comentarios

Thanks alot it works
Just a little point, is there a way to repeat hello world text several times like
Hello world
Hello world
Hello world
Nevermind I got it
N=500;
M=500;
whiteImage = 255 * ones(N, M,'uint8');
text_str = cell(3,1);
position = [23 90;23 185;23 137];
for ii=1:3
text_str{ii} = ['Hello world ' ];
end
DD= insertText(whiteImage, position,text_str,'FontSize',18,'TextColor','black','BoxColor','white');
imshow(DD);
You don't need the loop to build an array (of any type), generally...
>> repelem("Hello world!",3,1) % string array
ans =
3×1 string array
"Hello world!"
"Hello world!"
"Hello world!"
>> repelem('Hello world!',3,1) % old style char() array (not recommended)
ans =
3×12 char array
'Hello world!'
'Hello world!'
'Hello world!'
>> repelem({'Hello world!'},3,1) % cellstr array
ans =
3×1 cell array
{'Hello world!'}
{'Hello world!'}
{'Hello world!'}
>>
See if insertText() is smart enough to take the first option of a string array...if not, submit enhancement request. If it hasn't yet been upgraded, use the cellstr() option.
Read up on string handling functionality in MATLAB Getting Started and doc's -- it's been upgraded significantly over last couple of years with introduction of the strings class.
apparently it hasnt been upgraded. But thats fine I will use the earlier method. Im just trying to use this blank image with text as a mask image.
Im trying to save it as greyscale image as:
imwrite(uint8(DD),'dataset/mask1.png');
However when i reload it it shows an RGB image:
F = imread('dataset/mask1.png')
% F = 1014x1024x3
whats wrong.
DD was probably a 3-D variable, rows x columns x 3. Just casting it to uint8 does not convert it to grayscale. Try
imwrite(uint8(DD(:, :, 1)),'dataset/mask1.png'); % Save grayscale image, or red channel image.
It works. Lastly before i stop disturbing u.
Do you reckon this method as a good way to make an image mask?
The plan is to get this image 'mask1.png' and use it as a mask on an input image(Left) to produce the output in image(right)
Input=im2double(imread('dataset/3.jpg'));
mask = double( mat2gray( im2double(imread('dataset/mask1.png')) ) == 1 );
if size(mask,3)==1 && size(input,3)>1
mask = repmat(mask,[1,1,size(input,3)]);
end
output = mask.*Input ;
basically im just trying to recreate this
ff.jpeg
Depends on whether you want to do a weighted average of the images, or if you want to do masking. You're doing masking where you'll set the underlying image to pure black wherever your other image is equal to 1. Try it and see if this is what you want. I would probably suggest a weighted sum though, like 0.7 of one image plus 0.3 of the other image.

Iniciar sesión para comentar.

Más respuestas (1)

It comes from the 'BoxColor' option. See the documentation. To have background be white:
N=500;
M=500;
whiteImage = 255 * ones(N, M, 'uint8');
DD= insertText(whiteImage, [100,234],'Hello World','FontSize',18,'TextColor','black', 'BoxColor', 'white');
imshow(DD);

Categorías

Más información sobre Read, Write, and Modify Image en Centro de ayuda y File Exchange.

Preguntada:

el 3 de Feb. de 2020

Comentada:

el 4 de Feb. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by