Iterative threshold selection on an input gray-level image

1 回表示 (過去 30 日間)
Gideon Oyediran
Gideon Oyediran 2017 年 6 月 17 日
コメント済み: Gideon Oyediran 2017 年 6 月 17 日
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);

採用された回答

Image Analyst
Image Analyst 2017 年 6 月 17 日
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 件のコメント
Gideon Oyediran
Gideon Oyediran 2017 年 6 月 17 日
wow..you have really save me, i really appreciate this so much. Thanks once again

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by