how to add final result value after all images and use it in the same loop for thresholding

1 view (last 30 days)
%% Image Acquisition
clc;
clear all;
close all;
result = 0;
for i = 1:2;
[fname path] = uigetfile('*.*','Enter an image');
fname = strcat(path,fname);
img = imread(fname);
%figure
%imshow(img);title('Input image');
img1 = imresize(img,[256 256]);
%figure,
%imshow(img1);title('Resized image');
%% Preprocessing
I1=fspecial('average');
I1=imfilter(img1,I1);
I2 = imgaussfilt(img1);
I3 = medfilt3(img1);
average_psnr = psnr(img1,I1);
gaussian1_psnr = psnr(img1,I2);
median_psnr = psnr(img1,I3);
figure,
%subplot(332);
%imshow(I1);
%title(sprintf('Average Filtered Image (PSNR = %fdB)', average_psnr));
%subplot(335);
%imshow(I2);
%title(sprintf('Gaussian Filtered Image (PSNR = %fdB)', gaussian1_psnr));
%subplot(338);
%imshow(I3);
%title(sprintf('Median Filtered Image (PSNR = %fdB)', median_psnr));
%% Segmentation
numColors = 3;
L = imsegkmeans(I2,numColors);
B = labeloverlay(I2,L);
lab_he = rgb2lab(I2);
ab = lab_he(:,:,2:3);
ab = im2single(ab);
pixel_labels = imsegkmeans(ab,numColors,NumAttempts=3);
B2 = labeloverlay(I2,pixel_labels);
mask1 = pixel_labels == 1;
cluster1 = I2.*uint8(mask1);
mask2 = pixel_labels == 2;
cluster2 = I2.*uint8(mask2);
mask3 = pixel_labels == 3;
cluster3 = I2.*uint8(mask3);
L = lab_he(:,:,1);
L_blue = L.*double(mask3);
L_blue = rescale(L_blue);
idx_light_blue = imbinarize(nonzeros(L_blue));
blue_idx = find(mask3);
mask_dark_blue = mask3;
mask_dark_blue(blue_idx(idx_light_blue)) = 0;
blue_nuclei = I2.*uint8(mask_dark_blue);
%% plotting
%figure,
%subplot(231)
%imshow(cluster1)
%title("Objects in Cluster 1");
%subplot(232)
%imshow(cluster2)
%title("Objects in Cluster 2");
%subplot(233)
%imshow(cluster3)
%title("Objects in Cluster 3");
%% binarizing the image
I11 = im2bw(cluster1,0.1);
I12 = im2bw(cluster2);
I13 = im2bw(cluster3,0.1);
BW = imfill(I13,"holes");
%figure
%subplot(231);
%imshow(I11);
%title('Binary image of Cluster1');
%subplot(232);
%imshow(I12);
%title('Binary image of Cluster2');
%subplot(233);
%imshow(I13);
%title('Binary image of Cluster3');
%subplot(235)
%imshow(BW);
%title('FILLED IMAGE');
%% Filter the Image
BW0 = bwareaopen(BW,120);
%% find the number of cells
cells = bwconncomp(BW);
noofcells = cells.NumObjects;
%% Count the circles in the image and Highlight them
figure;
imshow(BW0);
title('RBC count');
hold on;
% Count number of RBC
[rcenters, rradii, rmetric] = imfindcircles(BW0,[10 10],'ObjectPolarity','bright','Sensitivity',0.99,'Method','twostage');
rh = viscircles(rcenters,rradii, 'Color', 'r');
[rm,rn]=size(rcenters);
result = result +rm;
fprintf('Number of RBC: %d\n', rm) %RBC COUNT
%% Patient is anemic or non-anemic
if rm < 115;
disp('Patient is anemic');
else
disp('Patient is non-anemic');
end
end

Answers (1)

Himanshu
Himanshu on 30 Aug 2023
Hello Abdul,
I understand that you are trying to accumulate the total count of detected RBCs over multiple images processed in a loop and then make anemic/non-anemic decisions for the images.
You are incrementing the "result" variable within the loop, and at the end of each iteration, you are checking the number of red blood cells (RBCs) in the image and making a diagnosis based on that count. The issue you're facing is that the result value doesn't seem to be available outside the loop for subsequent operations.
You should use the cumulative result value after all images have been processed within the loop for further calculations, such as thresholding.
Initialize the result variable before the loop to ensure that it starts with a value of 0 for each iteration.
After the loop has finished processing all images, you can use the final result value for further calculations or thresholding.
I hope this helps.

Community Treasure Hunt

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

Start Hunting!