Clear Filters
Clear Filters

Plot histogram of a series of images and extract cracks

1 view (last 30 days)
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)

Accepted Answer

Ruchika Parag
Ruchika Parag on 17 Jul 2024
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:
  1. Loop through images and plot histograms: Ensure that you are plotting the histogram for the first and the last images correctly.
  2. Clear previous figures: Use 'clf' to clear the current figure before plotting new images and histograms.
  3. Figure and subplot management: Make sure you create a new figure for each set of subplots to avoid overwriting.
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.
  1 Comment
Elisa
Elisa on 18 Jul 2024
Dear Ruchika Parag, tahnk you very much for your help!!!!
i modified the code according to your suggestion and i also used a png image cause is more simple to be processed by my computer. Anyway, I tried so set thresholds without having the right final result. I would like to remove the black background at first, an then to extract cracks within the circle. Can you help me finding what is not working properly? I tried with circles2mask using imfindcircle at first, then i also tried to create a mask using a threshold for black pixels. here you can find the modified code:
clear all
close all
clc
A= imread('image_1567.png');
%% method 1
[centers,radii] = imfindcircles(A,[50 100],Sensitivity=0.9);
mask = circles2mask(centers,radii,size(A));
figure
montage({A,mask})
A(~(A == mask)) = nan
%% method 2
FG = fliplr(imadjust(I,[0.05 1]));
sout = size(I);
squaresize = [10 10];
xx = mod(0:(sout(2)-1),squaresize(2)*2)<squaresize(2);
yy = mod(0:(sout(1)-1),squaresize(1)*2)<squaresize(1);
BG = im2uint8(0.3 + bsxfun(@xor,xx,yy')*0.4);
mask = FG>10;
outpict = BG;
outpict(mask) = FG(mask);
outpict = uint8(double(FG).*mask + double(BG).*(1-mask));
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(outpict);
[counts, grayLevels] = imhist(outpict);
bar(grayLevels, counts, 'EdgeColor', 'r', 'FaceColor', 'b', 'BarWidth', 1);
xlim([0, max(outpict(:))]);
title('Histogram of output image with threshold 60');
subplot(3,2,4);
thresholdValue1 = 60;
f.InvertHardcopy = 'off';
binaryImage1 = outpict > thresholdValue1;
imshow(binaryImage1, []);
title('Binary Image threshold 60');
f.InvertHardcopy = 'off';
figure (1)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!