- Loop through images and plot histograms: Ensure that you are plotting the histogram for the first and the last images correctly.
- Clear previous figures: Use 'clf' to clear the current figure before plotting new images and histograms.
- Figure and subplot management: Make sure you create a new figure for each set of subplots to avoid overwriting.
Plot histogram of a series of images and extract cracks
1 次查看(过去 30 天)
显示 更早的评论
Dear all,
I'm trying to plot histogram of a series of tomography images to assess the threshold for extracting cracks. I don't understand why the histogram is not displayed. I would like to select at least the first and the last images to determine the threshold. Here is the code:
Thanks a lot for any help
clear all
close all
clc
a= load('tomo_carota_8bit.mat')
x=a.x_8;
x=double(x);
% clear a
%%
figure,
for ii = 1:size(x, 3)
ii
I = squeeze(x(:,:,ii));
imshow(I, []);
pause(0.1)
end
subplot(3,2,1);
imshow(I,[]);
title('Original Grayscale Image', 'FontSize', 15);
subplot(3,2,2);
imhist(I);
title('Histogram of original image');
subplot(3,2,3);
imhist(I);
[counts, grayLevels] = imhist(I);
bar(grayLevels, counts, 'EdgeColor', 'r', 'FaceColor', 'b', 'BarWidth', 1);
xlim([0, max(I(:))]);
%xlim([0 100])
title('Histogram of original image zoomed');
subplot(3,2,4);
thresholdValue = 85;
thresholdValue1 = 60;
f.InvertHardcopy = 'off';
binaryImage1 = I > thresholdValue1;
imshow(binaryImage1, []);
title('Binary Image threshold 85');
f.InvertHardcopy = 'off';
binaryImage = I > thresholdValue;
subplot(3,2,5);
imshow(binaryImage, []);
title('Binary Image threshold 60');
figure (1)
0 个评论
采纳的回答
Ruchika Parag
2024-7-17
Hi Elisa, it looks like you have a few issues in your code that might be causing the histogram not to be displayed properly. Here are some suggestions and corrections to help you plot the histogram of a series of tomography images effectively:
Here is a modified version of your code:
clear all
close all
clc
a = load('tomo_carota_8bit.mat');
x = a.x_8;
x = double(x);
figure,
for ii = 1:size(x, 3)
I = squeeze(x(:, :, ii));
imshow(I, []);
pause(0.1)
end
figure;
I_first = squeeze(x(:, :, 1));
subplot(3, 2, 1);
imshow(I_first, []);
title('First Grayscale Image', 'FontSize', 15);
subplot(3, 2, 2);
imhist(I_first);
title('Histogram of first image');
subplot(3, 2, 3);
[counts, grayLevels] = imhist(I_first);
bar(grayLevels, counts, 'EdgeColor', 'r', 'FaceColor', 'b', 'BarWidth', 1);
xlim([0, max(I_first(:))]);
title('Histogram of first image zoomed');
I_last = squeeze(x(:, :, end));
subplot(3, 2, 4);
imshow(I_last, []);
title('Last Grayscale Image', 'FontSize', 15);
subplot(3, 2, 5);
imhist(I_last);
title('Histogram of last image');
subplot(3, 2, 6);
[counts, grayLevels] = imhist(I_last);
bar(grayLevels, counts, 'EdgeColor', 'r', 'FaceColor', 'b', 'BarWidth', 1);
xlim([0, max(I_last(:))]);
title('Histogram of last image zoomed');
thresholdValue1 = 60;
thresholdValue2 = 85;
figure;
binaryImage1 = I_first > thresholdValue1;
subplot(2, 2, 1);
imshow(binaryImage1, []);
title('Binary Image threshold 60 (First Image)');
binaryImage2 = I_first > thresholdValue2;
subplot(2, 2, 2);
imshow(binaryImage2, []);
title('Binary Image threshold 85 (First Image)');
binaryImage3 = I_last > thresholdValue1;
subplot(2, 2, 3);
imshow(binaryImage3, []);
title('Binary Image threshold 60 (Last Image)');
binaryImage4 = I_last > thresholdValue2;
subplot(2, 2, 4);
imshow(binaryImage4, []);
title('Binary Image threshold 85 (Last Image)');
By making these changes, you should be able to visualize the histograms of the first and last tomography images and determine the appropriate threshold for extracting cracks.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!