How do I solve imnoise error when inserting variables that store

Technical Source
3 min readMar 4, 2022

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)??

Error using imnoise Expected input number 1, I, to be one of these types:uint8, uint16, double, int16, singleInstead 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.

NOTE:-

Matlabsolutions.com provide latest MatLab Homework Help,MatLab Assignment Help for students, engineers and researchers in Multiple Branches like ECE, EEE, CSE, Mechanical, Civil with 100% output.Matlab Code for B.E, B.Tech,M.E,M.Tech, Ph.D. Scholars with 100% privacy guaranteed. Get MATLAB projects with source code for your learning and research.

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);

SEE COMPLETE ANSWER CLICK THE LINK

--

--

Technical Source

Simple! That is me, a simple person. I am passionate about knowledge and reading. That’s why I have decided to write and share a bit of my life and thoughts to.