How to calculate every peak in plot

6 views (last 30 days)
pizzaa
pizzaa on 1 Jul 2023
Answered: Star Strider on 1 Jul 2023
How to total every peak in this plot?
This is my code:
fabric_image = imread('Kain1.png'); % Replace 'fabric_image.jpg' with the actual path to your fabric image
gray_fabric = rgb2gray(fabric_image);
threshold = graythresh(gray_fabric);
binary_fabric = imbinarize(gray_fabric, threshold);
horizontal_profile = mean(binary_fabric, 2);
vertical_profile = mean(binary_fabric, 1);
figure;
subplot(2, 1, 1);
plot(horizontal_profile, 'k');
xlabel('Distance Pixel');
ylabel('Average Intensity');
title('Profile Warp');
subplot(2, 1, 2);
plot(vertical_profile, 'k');
xlabel('Distance Pixel');
ylabel('Average Intensity');
title('Profile Weft');
i want to calculate every peak of that every intensity
  2 Comments
Image Analyst
Image Analyst on 1 Jul 2023
Knowing the peak location of those profiles won't provide you with any useful information because your view of the fabric is oblique. Using findpeaks will be a lesson in failure.

Sign in to comment.

Answers (2)

Animesh
Animesh on 1 Jul 2023
To calculate the peaks in the intensity profiles, you can use the findpeaks function in MATLAB. This function finds local maxima in a given vector. Here's how you can modify your code to calculate the peaks:
fabric_image = imread('Kain1.png');
gray_fabric = rgb2gray(fabric_image);
threshold = graythresh(gray_fabric);
binary_fabric = imbinarize(gray_fabric, threshold);
horizontal_profile = mean(binary_fabric, 2);
vertical_profile = mean(binary_fabric, 1);
% Calculate peaks in the horizontal profile
[horizontal_peaks, horizontal_locations] = findpeaks(horizontal_profile);
% Calculate peaks in the vertical profile
[vertical_peaks, vertical_locations] = findpeaks(vertical_profile);
% Plot the profiles with peaks
figure;
subplot(2, 1, 1);
plot(horizontal_profile, 'k');
hold on;
plot(horizontal_locations, horizontal_peaks, 'ro');
xlabel('Distance Pixel');
ylabel('Average Intensity');
title('Profile Warp');
legend('Horizontal Profile', 'Peaks');
hold off;
subplot(2, 1, 2);
plot(vertical_profile, 'k');
hold on;
plot(vertical_locations, vertical_peaks, 'ro');
xlabel('Distance Pixel');
ylabel('Average Intensity');
title('Profile Weft');
legend('Vertical Profile', 'Peaks');
hold off;
This code calculates the peaks in both the horizontal and vertical intensity profiles and plots the profiles with the identified peaks marked as red circles.
You can refer the following documentation for more details:

Star Strider
Star Strider on 1 Jul 2023
Peak locations, full-width-half-maximum, and areas —
fabric_image = imread('Kain1.png'); % Replace 'fabric_image.jpg' with the actual path to your fabric image
gray_fabric = rgb2gray(fabric_image);
threshold = graythresh(gray_fabric);
binary_fabric = imbinarize(gray_fabric, threshold);
horizontal_profile = mean(binary_fabric, 2);
vertical_profile = mean(binary_fabric, 1);
figure;
subplot(2, 1, 1);
plot(horizontal_profile, 'k');
xlabel('Distance Pixel');
ylabel('Average Intensity');
title('Profile Warp');
subplot(2, 1, 2);
plot(vertical_profile, 'k');
xlabel('Distance Pixel');
ylabel('Average Intensity');
title('Profile Weft');
hidx = horizontal_profile < 1;
hy = horizontal_profile(hidx);
hy = sgolayfilt(hy, 3, 5);
hx = (0 : nnz(hidx)-1).';
vidx = vertical_profile < 1;
vy = vertical_profile(vidx).';
vy = sgolayfilt(vy, 3, 5);
vx = (0 : nnz(vidx)-1).';
hv = {[hx hy], [vx vy]};
ttl = {'Horizontal Profile'; 'Vertical Profile'};
figure
tiledlayout(2,1)
for k1 = 1:numel(hv)
nexttile
x = hv{k1}(:,1);
y = hv{k1}(:,2);
[pks, plocs] = findpeaks(y);
plot(x, y)
hold on
plot(x(plocs), pks, 'sr')
hold off
grid
title(ttl{k1})
end
for k1 = 1:numel(hv)
x = hv{k1}(:,1);
y = hv{k1}(:,2);
[pks, plocs] = findpeaks(y);
[vys, vlocs] = findpeaks(-y);
vys = [vys; y(end)];
vlocs = [vlocs; numel(x)];
for k = 1:numel(pks)
% k
% Q0 = [vlocs(k) vlocs(k+1)]
idxrng = vlocs(k) : vlocs(k+1);
baseline = [x(idxrng) ones(size(x(idxrng)))] * ([x(idxrng([1 end])) ones(2,1)] \ y(idxrng([1 end])));
idx = ismember(plocs, idxrng);
if nnz(idx) < 1
break
end
pkidx(k,:) = plocs(idx);
xpk(k,:) = x(pkidx(k,:));
ypk(k,:) = y(pkidx(k,:));
ixvr = vlocs(k) : pkidx(k,:);
hwv(1,k) = median(y(ixvr([1 end])));
hw(1,k) = interp1(y(ixvr), x(ixvr), hwv(1,k));
ixvf = pkidx(k,:) : vlocs(k+1);
hwv(2,k) = median(y(ixvf([1 end])));
hw(2,k) = interp1(y(ixvf), x(ixvf), hwv(2,k));
auc(k,:) = trapz(x(idxrng), y(idxrng)-baseline);
end
fwhm = diff(hw).';
Results = table(table(xpk, ypk, fwhm, auc, 'VariableNames',{'Peak X','Peak Y','FWHM','AUC'}), 'VariableNames',ttl(k1))
end
Results = 46×1 table
Horizontal Profile Peak X Peak Y FWHM AUC ________________________________________ 7 0.53918 5.9165 0.72751 20 0.4738 7.9264 0.55584 34 0.48441 6.1844 0.55624 47 0.47743 7.2204 0.57743 60 0.53571 6.1809 0.61367 65 0.46008 1 0.0021224 72 0.54747 6.6988 0.5129 85 0.56971 5.4371 0.52684 92 0.47065 1 0.0029184 97 0.52065 7.2364 0.36265 109 0.50735 5.2293 0.27816 119 0.44188 1.8226 0.010449 123 0.47702 5.6774 0.24514 131 0.42608 1 0.0047347 134 0.43486 2.9299 0.047265 141 0.42061 1.9692 0.036939
Results = 66×1 table
Vertical Profile Peak X Peak Y FWHM AUC _________________________________________ 10 0.45246 6.0789 0.28003 21 0.43434 2.6445 0.051755 35 0.45366 10.361 0.28996 42 0.42786 1 0.001932 44 0.42819 1 0.00081633 49 0.45317 5.6125 0.13981 59 0.44822 2.0512 0.0028844 63 0.46444 2.5559 0.030857 68 0.45693 4.9749 0.056109 77 0.42661 2.91 0.008381 86 0.416 1.6064 0.0039184 91 0.43254 4.7985 0.091265 102 0.45518 2.7289 0.0096327 106 0.464 2.8341 0.034503 111 0.45045 1.0714 0.0019592 118 0.46487 7.3131 0.16648
.

Community Treasure Hunt

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

Start Hunting!