フィルターのクリア

Automatic Thresholding using Histograms

8 ビュー (過去 30 日間)
John Brathwaite
John Brathwaite 2011 年 4 月 18 日
回答済み: Vidhi Agarwal 2024 年 8 月 1 日 9:39
Anybody know of an algorithm that will help me identify peaks in a histogram?
What about an automatic thresholder algorithm that could calculate the threshold by looking at the boundary between two peaks?
I am also looking for a reference source that describes the technique of using a percentage of the cumulative histogram to calculate a threshold. In this algorithm, I want the software to analyse the stack of pics and determine the percentage to apply to the cumulative histogram.

回答 (1 件)

Vidhi Agarwal
Vidhi Agarwal 2024 年 8 月 1 日 9:39
Hi John,
I understand you have a query regarding a way to identify the peaks in histogram. “findpeaks” function of MATLAB is used to locate local maxima in a dataset, which can be useful for identifying peaks in a histogram. Here is an example of how to use “findpeaks”:
% Example data
data = randn(1000,1);
% Create a histogram
[counts, binCenters] = hist(data, 50);
% Find peaks in the histogram
[peaks, locs] = findpeaks(counts, 'MinPeakProminence', 10);
% Plot the histogram and the peaks
figure;
bar(binCenters, counts);
hold on;
plot(binCenters(locs), peaks, 'r^', 'MarkerFaceColor', 'r');
title('Histogram with Peaks');
xlabel('Value');
ylabel('Count');
hold off;
An automatic thresholding algorithm that calculates the threshold by looking at the boundary between two peaks is the Otsu's method. Here is an example of how to use Otsu's method in MATLAB:
% Example grayscale image
I = imread('cameraman.tif');
% Compute the threshold using Otsu's method
threshold = graythresh(I);
% Convert the threshold to the range of the image
thresholdValue = threshold * 255;
% Apply the threshold to create a binary image
binaryImage = imbinarize(I, threshold);
% Display the original and binary images
figure;
subplot(1, 2, 1);
imshow(I);
title('Original Image');
subplot(1, 2, 2);
imshow(binaryImage);
title(['Binary Image (Threshold = ', num2str(thresholdValue), ')']);
A technique for thresholding based on a percentage of the cumulative histogram involves calculating the cumulative distribution function (CDF) of the histogram and selecting a threshold based on a specified percentile. Here is an example of how to calculate a threshold based on a specified percentile of the cumulative histogram:
% Example data
data = randn(1000,1);
% Create a histogram
[counts, binCenters] = hist(data, 50);
% Calculate the cumulative histogram
cdf = cumsum(counts) / sum(counts);
% Specify the desired percentile (e.g., 90%)
percentile = 0.90;
% Find the threshold corresponding to the specified percentile
thresholdIndex = find(cdf >= percentile, 1, 'first');
thresholdValue = binCenters(thresholdIndex);
% Plot the histogram and the threshold
figure;
bar(binCenters, counts);
hold on;
plot([thresholdValue, thresholdValue], [0, max(counts)], 'r--', 'LineWidth', 2);
title('Histogram with Threshold');
xlabel('Value');
ylabel('Count');
legend('Histogram', ['Threshold (', num2str(percentile*100), '%)']);
hold off;
For better understanding of “findpeaks” and “graythresh”, please refer to the following documentation:
Hope that helps!

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by