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개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!