Findpeaks creates empty arrays in for loop

2 views (last 30 days)
mikel lasa
mikel lasa on 15 Mar 2021
Commented: dpb on 15 Mar 2021
Hello,
I'm defining a for loop to use on a laser stripe image and does the following:
  1. takes a row
  2. filters the row ( I take only the bigest and lowest peak)
  3. gets the gradient
  4. use findpeaks to get the position and value of the gradient max and min
  5. create a line between these two points
  6. get the 0 crossing
Right now im able to get the 0 crossing of just one line. Im stuck in the "for" loop as the findpeaks function is creating empty arrays I dont know why!
The aim is to get the 0 crossing of all the image rows, then plot all.
Please any help?
img_peaks = NaN(rows, 1);
xp = [];
yp = [];
Xn=[];
Yn=[];
for i=1:rows
img_puntos(i,:)=img(i,:);
%filtrado
filtrado=sgolayfilt(double(img_puntos(i,:)),3,33 );
for k=1:length(filtrado)
if filtrado(k)<0
filtrado(k)=0;
end
end
%derivada
derivada=gradient(filtrado);
%maximo y minimo
[yp,xp]=findpeaks(derivada); % HERE IS THE PROBLEM!!!
[Xn,Yn]=findpeaks(-derivada);% HERE IS THE PROBLEM!!!
%coeficientes de la recta
% coeficientes = polyfit([Xp, Xn], [Yp, -Yn], 1);
% m = coeficientes (1);
% n = coeficientes (2);
% % 0 crossing
% zerocross= -m/n;
img_peaks(i) = subpixel;
end
  1 Comment
dpb
dpb on 15 Mar 2021
Not the problem with the data, but in MATLAB replace
for k=1:length(filtrado)
if filtrado(k)<0
filtrado(k)=0;
end
end
with
filtrado(filtrado<0)=0;
The problem in your code is that you didn't write code to handle the possibility a given row in the image may, indeed, not have a discernible peak.
We have no way to know anything about whether there's some way to be able to help findpeaks locate minimal excursions or not without the data to observe; generally it is pretty adept--but, it's designed for typical time series one obtains from things like measurements of acceleration or the like; depending upon what your image looks like, it might not be the best tool.
Or, there just may not be any discernible peaks in the data you have...

Sign in to comment.

Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!