How to calculate every peak in plot
4 views (last 30 days)
Show older comments
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
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.
Answers (2)
Animesh
on 1 Jul 2023
Hey @pizzaa
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:
0 Comments
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
.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!