MATLAB Answers

How do I solve imnoise error when inserting variables that store the single English alphabet and its background respectively which are logical image(which are binary image)??

1 view (last 30 days)
Tuck Wai Yip
Tuck Wai Yip on 15 Jul 2021
Commented: Simon Chan on 16 Jul 2021
Error using imnoise Expected input number 1, I, to be one of these types:
uint8, uint16, double, int16, single
Instead its type was logical.
Error in imnoise>ParseInputs (line 107) validateattributes(a, {'uint8','uint16','double','int16','single'}, {}, mfilename, ...
Error in imnoise (line 86) [a, code, classIn, classChanged, p3, p4] = ParseInputs(args{:});
Error in create_images_trial (line 34) Newforegnd_noise = imnoise(Newforegnd, 'gaussian',0, 0.01);
This is the error I received.How should I handle this? Because I need the background and single English alphabet to continue the process as next I will need to threshold on those separately.I attach my code below.
%create blank image
w = 150;
h = 150;
blankImage= 255*ones(w,h,3,'uint8');
%position of the letter in the empty cell
position_x = (w+1)/2;
position_y = (h+1)/2;
% varying the font size, start from 10 to 16
font_start = 58;
font_end = 64;
num_fontsA = font_start:2:font_end;
% get the number of fonts
numImagesA = length(num_fontsA);
% create a cell array with number of fonts to fill in the image in next step
A = cell(1, numImagesA);
% for loop to create letter 'A'
% grayscale
% store into the cell array
for i=1:numImagesA
for font_size = num_fontsA(i)
img= insertText(blankImage,[position_x position_y],'A','Font','Times New Roman','FontSize',font_size,'TextColor','black','BoxColor','w','BoxOpacity',0,'AnchorPoint','Center');
grayImage= rgb2gray(img);
BWImage = ~grayImage;
background = BWImage == 0;
foreground = ~background;
Newforegnd = foreground;
% figure('Name','Background and Object');
montage({Newforegnd, background});
% Apply noise on the image
% Apply noise on the alphabet, using 0.01 standard deviation
Newforegnd_noise = imnoise(Newforegnd, 'gaussian',0, 0.01);
%Apply noise on the background, using 0.01 standard deviation
background_noise = imnoise(background, 'gaussian',0, 0.01);
%Format figure name into string with font size automatically
%iterates using assigned variable font_size
% %d is decimal notation for the font size variable as it is in
% whole number
f = figure('Name','Foreground & background image before & after adding noise','NumberTitle','off');
p = uipanel('Parent',f,'BorderType','none');
p.Title = sprintf('For alphabet with font size of %d ',font_size);
p.TitlePosition = 'centertop';
%divide the figure into subplot
%which above is foreground of the alphabet
%while the background of alphabet at below
subplot(2,2,1,'Parent',p);
imshow(Newforegnd);
h1 = gca;
h1.Visible = 'on';
title("Foreground image of the single alphabet");
subplot(2,2,2,'Parent',p);
imshow(Newforegnd_noise);
h2 = gca;
h2.Visible = 'on';
title("Image of the single alphabet(after adding noise) ");
subplot(2,2,3,'Parent',p);
imshow(background);
h3 = gca;
h3.Visible = 'on';
title("Background of the single alphabet");
subplot(2,2,4,'Parent',p);
imshow(background_noise);
h4 = gca;
h4.Visible = 'on';
title("Background of the single alphabet(after adding noise)");
Is that anyone could guide me what to do next for putting the gaussian noise ,the image now is in logical form,it can be grayscale it is made into binary because I separate the alphabet and its background into different variable?Thank you.
  4 Comments
Tuck Wai Yip
Tuck Wai Yip on 16 Jul 2021
Sorry,it is because I viewed this in phone,I overlook the whole output.thank you for your help,I will try this straightaway after this.

Sign in to comment.

Accepted Answer

Simon Chan
Simon Chan on 15 Jul 2021
Edited: Simon Chan on 15 Jul 2021
Newforegnd & background are BW image which is class logical and imnoise does not support.
You may change it to class double or uint8 as follows as an example.
Newforegnd_noise = imnoise(double(Newforegnd), 'gaussian',0, 0.01);
%Apply noise on the background, using 0.01 standard deviation
background_noise = imnoise(double(background), 'gaussian',0, 0.01);
%
%
%
Newforegnd_noise = imnoise(uint8(Newforegnd*255), 'gaussian',0, 0.01);
%Apply noise on the background, using 0.01 standard deviation
background_noise = imnoise(uint8(background*255), 'gaussian',0, 0.01);
  2 Comments

Sign in to comment.

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by