How to display all image in cell array using loop for each dimensions?

1 visualización (últimos 30 días)
I have problem to display image using imshow,it does not show all image for each intensity difference from variable IntDiff_no for each index,can anyone guide and tell me where is wrong for the code?The code is inserted as below.
for i=1:numImages
for font_size = num_fonts(i)
figure('Name','Sauvola thresholding with %f font size,%f percent ','NumberTitle','off');
for IntDiff_no=1:each_intensityDifference
for IntDiff_percentage= intensityDifference(IntDiff_no)
for Noise_no =1:each_noise
for noise= num_noiseimg(Noise_no)
subplot(5,1,Noise_no)
imshow(alphabetSauvImage_collection{i,IntDiff_no,Noise_no});
title(sprintf('Image with %f font size %f intensity difference %f noise',font_size,IntDiff_percentage,noise));
end
end
end
end
end
end
The for loop above is the for loop that I faced the problem.I aim to display image subplot with varying noise, but for same font size and same intensity difference,so in total it will have 25 figures.Hope to guide from someone.Thank you.
%create blank image
w = 200;
h = 200;
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 80 to 160
font_start = 100;
font_end = 180;
num_fonts = font_start:20:font_end;
% get the number of fonts
numImages = length(num_fonts);
% create a cell array with number of fonts to fill in the image in next step
Output_collection = cell(1, numImages);
Orig_grayImage = cell(1, numImages);
alphabet_segmented = cell(5,5,5);
check_foreground = cell(5,5,5);
% alphabet_Uppercase = cell(1,26);
% alphabet_Uppercase = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
Int_image_array = cell(1, numImages);
A = cell(1, numImages);
Binaryimg = cell(1, numImages);
alphabet_orginal = cell(1, numImages);
% alphabet_Image = cell(26,5);
noise_start = 0.005;
noise_end = 0.025;
num_noiseimg = noise_start:0.005:noise_end;
each_noise = length(num_noiseimg);
alphabetNoiseimg_collection = cell(5,5,5);
alphabetSauvImage_collection = cell(5,5,5);
true = 0;
noise_pos = 1;
Int_image = cell(1, numImages);
intensityDifference = 20:20:100;
intensityDifference = round(intensityDifference);
each_intensityDifference=length(intensityDifference);
zeta = cell(5,5,5);
zeta_array = zeros(5,5,5);
zeta_array_intnoise = zeros(5,5);
zeta_array_fontnoise = zeros(5,5);
zeta_array_fontint = zeros(5,5);
check_value = zeros(1,125);
%%
% for loop to create letter 'A'
% grayscale
% store into the cell array
% for all_alphabet=1:
for i=1:numImages
for font_size = num_fonts(i)
Orgimage= insertText(blankImage,[position_x position_y],'A','Font','Times New Roman','FontSize',font_size,'TextColor','black','BoxColor','w','BoxOpacity',0,'AnchorPoint','Center');
grayImage= rgb2gray(Orgimage);
BWImage = imcomplement(grayImage);
A{1,i} = BWImage;
Orig_grayImage{1,i} = A{1,i};
Binaryimg{1,i} = imbinarize(Orig_grayImage{1,i});
alphabet_orginal{1,i} = sum(Binaryimg{1,i}(:));
% figure(i);
% imshow(A{1,i});
% impixelinfo;
% background = BWImage == 0;
% foreground = ~background;
% Newforegnd = foreground;
b=insertText(blankImage,[position_x position_y],'b','Font','Times New Roman', 'FontSize', font_size, 'TextColor','black','BoxColor','w','BoxOpacity',0,'AnchorPoint','Center');
o=insertText(blankImage,[position_x position_y],'o','Font','Times New Roman Bold', 'FontSize', font_size, 'TextColor','black','BoxColor','w','BoxOpacity',0,'AnchorPoint','Center');
BWImage_bin = imbinarize(BWImage);
out = repmat('b',size(BWImage_bin));
BoldO = '<strong>o</strong>';
out(BWImage_bin) = 'o';
display(out);
Output_collection{1,i} = out;
% subplot(5,1,i);
% imshow(BWImage);
% axis on;
%
for IntDiff_no=1:each_intensityDifference
for IntDiff_percentage= intensityDifference(IntDiff_no)
% backgroundGrayLevel = IntDiff_percentage;
% Int_image = A{1,i};
% background_range = A{1,i} ==0;
% Int_image(background_range)= backgroundGrayLevel;
% foregroundGrayLevel = backgroundGrayLevel + ((backgroundGrayLevel/100)*255);
% foreground_range = A{1,i} ==255;
% Int_image(foreground_range) = foregroundGrayLevel;
% Int_image_array{i,IntDiff_no} = Int_image;
highIntensity = (IntDiff_percentage/100)* 255; % 20%
Int_image = uint8(rescale(A{1,i}, 0, highIntensity));
Int_image_array{i,IntDiff_no} = Int_image;
figure('Name',sprintf('Font size of %d for Gaussian noise',font_size),'NumberTitle','off');
for Noise_no =1:each_noise
for noise= num_noiseimg(Noise_no)
% Apply Gaussiannoise on the alphabet,
%using 0.01 to 0.05 (increament of 0.01 per step)standard deviation
NoiseImg = imnoise(Int_image_array{i,IntDiff_no}, 'gaussian',0, noise);
alphabetNoiseimg_collection{i,IntDiff_no,Noise_no}=NoiseImg;
%apply sauvola's thresholding
%m is mean value
%k is a constant between 0.2 to 0.5%
%s is standard deviation
%R is gray level
%figure('Name',sprintf('Image of alphabet with font size %d ',font_size),'NumberTitle','off');
%S = m* (1 + k * (s/R - 1));
%R = 128;
%k = 0.2;
sauvImage = sauvola(NoiseImg, [200 200]);
alphabetSauvImage_collection{i,IntDiff_no,Noise_no} = sauvImage;
%for checking total image
% fprintf('i is%d',i);
% imshow(sauvImage);
% figure('Name','Sauvola thresholding bef and after','NumberTitle','off');
Image_segmented = nnz(sauvImage);
alphabet_segmented{i,IntDiff_no,Noise_no}= Image_segmented;
check_foreground{i,IntDiff_no,Noise_no} = sum(sauvImage(:));
zeta{i,IntDiff_no,Noise_no} = alphabet_segmented{i,IntDiff_no,Noise_no}/alphabet_orginal{1,i};
zeta_array(i,IntDiff_no,Noise_no) = alphabet_segmented{i,IntDiff_no,Noise_no}/alphabet_orginal{1,i};
zeta_array_intnoise(IntDiff_no,Noise_no) = alphabet_segmented{i,IntDiff_no,Noise_no}/alphabet_orginal{1,i};
zeta_array_fontnoise(i,Noise_no) = alphabet_segmented{i,IntDiff_no,Noise_no}/alphabet_orginal{1,i};
zeta_array_fontint(i,IntDiff_no) = alphabet_segmented{i,IntDiff_no,Noise_no}/alphabet_orginal{1,i};
% plot(num_noiseimg,zeta_array);
% = zeta_array;
if alphabet_segmented{i,IntDiff_no,Noise_no} == check_foreground{i,IntDiff_no,Noise_no}
true = true +1;
end
end
end
end
end
figure('Name',sprintf('Font size of %d for Intensity Difference',font_size),'NumberTitle','off');
for int_no =1:IntDiff_no
subplot(5,1,int_no);
imshow(Int_image_array{i,int_no});
axis on;
title(sprintf('Intensity difference %d percent for alphabet',intensityDifference(int_no)));
end
end
end
% fprintf('%d of sauvola image',true);
figure('Name','Image with different font_size','NumberTitle','off');
for font_no =1:numImages
for each_font = num_fonts(font_no)
subplot(5,1,font_no);
imshow(Orig_grayImage{1,font_no});
axis on;
title(sprintf('Image with %d font size',each_font));
end
end
for i=1:numImages
for font_size = num_fonts(i)
figure('Name','Sauvola thresholding with %f font size,%f percent ','NumberTitle','off');
for IntDiff_no=1:each_intensityDifference
for IntDiff_percentage= intensityDifference(IntDiff_no)
for Noise_no =1:each_noise
for noise= num_noiseimg(Noise_no)
subplot(5,1,Noise_no)
imshow(alphabetSauvImage_collection{i,IntDiff_no,Noise_no});
title(sprintf('Image with %f font size %f intensity difference %f noise',font_size,IntDiff_percentage,noise));
end
end
end
end
end
end

Respuesta aceptada

Walter Roberson
Walter Roberson el 20 de Jul. de 2021
for i=1:numImages
So far we can expect to run numImages times. We can work backwards to find that numImages = 5 in the current code.
for font_size = num_fonts(i)
num_fonts is a vector, not a function, so num_fonts(i) is a scalar. There is not much point using a for loop for a single iteration; it would be clearer to have written that flow as just font_size = num_fonts(i); without using for . Anyhow, as this is a scalar, the number of times executing loops so far is numImages*1 which is numImages
figure('Name','Sauvola thresholding with %f font size,%f percent ','NumberTitle','off');
for IntDiff_no=1:each_intensityDifference
Okay, so now we are at numImages*1*each_intensityDifference loop iterations, which is 5*1*5 = 25 when you work backwards to find out what each_intensityDifference is.
for IntDiff_percentage= intensityDifference(IntDiff_no)
Again, no point using a for loop for a scalar; just use IntDiff_percentage = intensityDifference(IntDiff_no);
for Noise_no =1:each_noise
We can work backwards to see that each_noise = 5. So now we are at numImages*1*each_IntensityDifference*1*each_noise = 5*1*5*1*5 = 125 loop iterations
for noise= num_noiseimg(Noise_no)
Again, no point using a for loop for a scalar; just use noise = num_noiseimg(Noise_no); without a loop structure. So we are now at numImages*1*each_IntensityDifference*1*each_noise*1 = 5*1*5*1*5*1 = 125 loop iterations
subplot(5,1,Noise_no)
5 subplots possible, a Noise_no has a maximum value of 5, so the for Noise_no loop is filling each of the 5 subplots in turn
imshow(alphabetSauvImage_collection{i,IntDiff_no,Noise_no});
title(sprintf('Image with %f font size %f intensity difference %f noise',font_size,IntDiff_percentage,noise));
No drawnow(), no pause(), no axes() or figure() -- the actual drawing of the images will be postponed for later
end
That ends the for noise loop that has only one iteration. Nothing is present here to trigger drawing yet
end
That ends the for Noise_no loop that does all 5 noises. Nothing is present here to trigger drawing yet.
end
That ends the for IntDiff_precentage loop that has only one iteration. Nothing is present here to trigger drawing yet.
end
That ends the for IntDiff_no loop that has 5 iterations. But nothing was present anywhere in here to trigger drawing, so each of the IntDiff_no iterations triggers drawing 5 images, but the next IntDiff_no iteration draws on top of the same subplots. If you had had a drawnow() or pause() in the loops, they might at least have shown up temporarily before being replaced, but as it is the earlier ones will not be displayed at all.
end
end
... and that happens 5 times more. All you are going to see is the 5 images drawn by the last iteration of for i and the last iteration of for IntDiff_no .
  4 comentarios
Walter Roberson
Walter Roberson el 21 de Jul. de 2021
Editada: Walter Roberson el 21 de Jul. de 2021
That structure should be ok. It puts each group of 5 subplots into a new figure. You will get 25 figures, which might be a bit much.
Have you looked at montage()?
Tuck Wai Yip
Tuck Wai Yip el 21 de Jul. de 2021
Hi,Walter,I would like to ask about my latest code, why is it when I place figure(); on the line after IntDiff_percent it will solve my problem, but when I placed in the for loop of Noise_no ,it just displayed the last one.
I checked montage() previously, but I am not sure how to use it on my use cases because every parameter has five different values.Yeah,it is a bit too much 25 figures.Is there any better way to organize them ?

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by