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:
- https://www.mathworks.com/help/signal/ref/findpeaks.html
- https://www.mathworks.com/help/images/ref/graythresh.html
Hope that helps!