Save 360 arrays in a cell

Hello,
My programs loads 360 images (laser stripe) and from each image gets the 0 crossing of all rows. My issue is that after processing all images, im not able to save the 0 crossing arrays into a cell so I can post process them. Here my code:
clc; clear all; close all;
%% CARGAR LAS IMAGENES (load images)
for k = 1:360
jpgFilename = sprintf('%d.jpg', k);
fullFileName = fullfile('C:\MASTER\S2\percepcion\laser360\imagenes', jpgFilename);
if exist(fullFileName, 'file')
imageData = imread(fullFileName );
imageData=imrotate(imageData,90);
% se binariza la imagen (binarization)
img_o=rgb2gray(imageData);
mask=(img_o(:,:) > 50);
imageData = bsxfun(@times, img_o, cast(mask, 'like', img_o));
% figure()
% imshow(imageData)
% title('Binarizada')
%
[rows, columns, numberOfColorChannels] = size(imageData);
img_peaks = NaN(rows, 1);
img_puntos = zeros(rows, columns);
Xp = [];
Yp = [];
Xn=[];
Yn=[];
m=[];
n=[];
zerocross=[];
coefficients=[];
lineaslaser=cell(k,1);
%se busca la linea del 0 crossing
for icross=1:rows
img_puntos(icross,:)=imageData(icross,:);
%filtrado
filtrado=sgolayfilt(double(img_puntos(icross,:)),3,33 );
for ks=1:length(filtrado)
if filtrado(ks)<0
filtrado(ks)=0;
end
end
%derivada (gradient)
derivada=gradient(filtrado);
%maximo y minimo
[Yp(icross),Xp(icross)]=max(derivada);
[Yn(icross),Xn(icross)]=min(derivada);
%coeficientes de la recta (line coefs)
m(icross)= (Yp(icross)-Yn(icross))/(Xp(icross)-Xn(icross));
n(icross)= (Yp(icross)-m(icross)*Xp(icross));
% 0 crossing
zerocross(icross)= -n(icross)/m(icross);
img_peaks(icross) = zerocross(icross);
end
axis=1:1920;
img_peaks((1:10),1)=NaN;
img_peaks((1500:1920),1)=NaN;
% save 360 lines in a cell
lineaslaser{k}=img_peaks(k);
% figure()
% plot(axis,img_peaks)
% title('plot perfil de la pieza (0 crossing)')
else
warningMessage = sprintf('Warning: image file does not exist:\n%s', fullFileName);
uiwait(warndlg(warningMessage));
end
end

3 Comments

DGM
DGM on 15 Apr 2021
Edited: DGM on 15 Apr 2021
What exactly is the problem? Should I guess?
You're saving a single element from a numeric vector into a cell vector. Are you expecting to be saving the entire vector?
lineaslaser{k}=img_peaks(k);
Considering that you do this right before that assignment, the first 10 entries in lineaslaser will be scalar NaNs.
img_peaks((1:10),1)=NaN;
But none of that really matters, because every time the loop cycles, you delete the entire array:
lineaslaser=cell(k,1);
If you know the size of the arrays needed, preallocate them as the required size instead of growing them in a loop.
Xp = [];
Yp = [];
Xn=[];
Yn=[];
m=[];
n=[];
Yes, I want to save an entire vector in a cell for each loop. There is my problem, for each loop a vector is generated, so my aim is to create a variable that saves 360 vectors. I thought that cell is maybe the right way to do that.
Apart from that:
img_peaks((1:10),1)=NaN;
The first 10 elements are NaN because each image has some noise, I delete that noise this way. Its just, for cleaning.
Okay. If you move the preallocation outside the main loop and set that line to
lineaslaser{k}=img_peaks;
does it help?

Sign in to comment.

 Accepted Answer

Edit:
I have achieved what I was looking for. As you said I deleted this line:
lineaslaser=cell(k,1);
and I changed my this line:
lineaslaser{k}=img_peaks(k);
for this one:
lineaslaser{k}=img_peaks(:,1);
now works, I have a 360 element cell where each cell is a 1920x1 vector. Thanks for your help!

More Answers (0)

Categories

Products

Release

R2020b

Asked:

on 15 Apr 2021

Answered:

on 15 Apr 2021

Community Treasure Hunt

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

Start Hunting!