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 CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by