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)??
15 次查看(过去 30 天)
显示 更早的评论
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 个评论
Simon Chan
2021-7-16
Noticed the size of the figures are not the same, since I only resize the images with noise only
Newforegnd_noise = imnoise(double(Newforegnd), 'gaussian',0, 0.01);
Newforegnd_noise = imresize(Newforegnd_noise,[120 120]);
%
background_noise = imnoise(double(background), 'gaussian',0, 0.01);
background_noise = imresize(background_noise,[120 120]);
采纳的回答
Simon Chan
2021-7-15
编辑:Simon Chan
2021-7-15
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 个评论
Walter Roberson
2021-7-16
Right. imnoise() expects to be adding noise that only a fraction of the range of values for the data type, but logical() only has values 0 and 1 -- you cannot have 0.02 as a logical value for example.
With logical only supporting 0 and 1, requesting 0.01 gaussian would not round to 1 until you reached 50 standard deviations. If I read the tables right, that is a probability of about
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!