Iterative threshold selection on an input gray-level image

2 Ansichten (letzte 30 Tage)
Perform iterative threshold selection on an input gray-level image to include a variable that counts the number of iterations and an array that stores the values of T for each iteration.Please, this is what i have tried but i know i am wrong, somebody help me out.
I = imread('coins.png');
Id = im2double(I);
Imax = max(Id(:));
Imin = min(Id(:));
T = 0.5*(min(Id(:)) + max(Id(:)));
deltaT = 0.01;
done = false;
while ~done
g = Id >= T;
Tnext = 0.5*(mean(Id(g)) + mean(Id(~g)));
done = abs(T - Tnext) < deltaT;
T = Tnext;
end
imshow(g);

Akzeptierte Antwort

Image Analyst
Image Analyst am 17 Jun. 2017
You need to save T into an array that is indexed by the loop iteration counter. Like this:
clc; % Clear the command window.
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
grayImage = imread('coins.png');
subplot(2, 2, 1);
imshow(grayImage);
title('Original Image', 'FontSize', fontSize);
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(grayLevels, pixelCount); % Plot it as a bar chart.
grid on;
title('Histogram of original image', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Gray Level', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
Id = im2double(grayImage);
Imax = max(Id(:));
Imin = min(Id(:));
T = 0.5*(min(Id(:)) + max(Id(:)));
deltaT = 0.01;
done = false;
counter = 1;
while ~done
savedThresholds(counter) = T;
g = Id >= T;
Tnext = 0.5*(mean(Id(g)) + mean(Id(~g)));
done = abs(T - Tnext) < deltaT;
T = Tnext;
% Display g
subplot(2, 2, 3);
imshow(g);
title('Binary Image', 'FontSize', fontSize);
% Plot savedThresholds
subplot(2, 2, 4);
plot(savedThresholds, 'b*-', 'MarkerSize', 15, 'LineWidth', 2);
grid on;
title('Thresholds', 'FontSize', fontSize);
xlabel('Iteration', 'FontSize', fontSize);
ylabel('Threshold', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
promptMessage = sprintf('Threshold for iteration %d is %f.\nDo you want to Continue processing,\nor Quit processing?', ...
counter, savedThresholds(counter));
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if strcmpi(buttonText, 'Quit')
return;
end
% Increment loop interation counter.
counter = counter + 1;
end
  1 Kommentar
Gideon Oyediran
Gideon Oyediran am 17 Jun. 2017
wow..you have really save me, i really appreciate this so much. Thanks once again

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Images finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by