Index in position 1 is invalid. Array indices must be positive integers or logical values.

2 vues (au cours des 30 derniers jours)
Hi.
close all;
clear variables;
%Files and Pics
data_list = dir('*.bmp');
%Zugreifen auf Ordner und BIlder und Namen zuweisen
name = {data_list.name};
%Konvertieren in Matrix
imBlack = imread("460_dunkel.bmp");
name(strcmp(name,"460_dunkel.bmp")) = []; % remove 460_dunkel.bmp
lambda = 0.633; %Wellenlänge Laser in um
Pixelgroesse = 4.65; %Pixelgroesse in um
for i = 1:length(name)
picture = imread(name{i}); %Einlesen des Bildes in Matrix
current_name = name{i};
zaxe = str2double(name{i}(1:3)); %Auslesen des z Wertes aus Dateinamen
imBlack = double(imBlack);
pictures(:,:,i)=picture; %speichert das aktuelle (i-te)
%Bild als 2D Matrix in der 3D Matrix (:,:,i)
picture = double(picture);
filtered = double(minus(picture,imBlack));
%Ermittlung ROI
ROI = im2double(filtered);
ROIdiff = [1,1];
ROIoffset =[0, 0];
en_density = sum(sum((ROI)));
[row,column] = size(ROI);
xv = (1:column);
yv = (1:row).';
first_Moment_X(i,1) = sum(xv .* ROI, 'all');
Schwerp_X = first_Moment_X(i) / en_density;
Schwerpunkte_X(i) = Schwerp_X;
first_Moment_Y(i,1) = sum(yv .* ROI, 'all');
Schwerp_Y = first_Moment_Y(i) / en_density;
Schwerpunkte_Y(i) = Schwerp_Y;
sec_Moment_X(i,1) = sum((xv - Schwerp_X).^2 .* ROI, 'all');
Sigma_X_plural(i) = sec_Moment_X(i)/en_density;
sec_Moment_Y(i,1) = sum((yv - Schwerp_Y).^2 .* ROI, 'all');
Sigma_Y_plural(i) = sec_Moment_Y(i) / en_density;
sec_Moment_XY(i,1) = sum((xv - Schwerp_X) .* (yv - Schwerp_Y) .* ROI, 'all');
Sigma_XY_plural(i) = sec_Moment_XY(i) / en_density;
%Berechnung des Strahldurchmessers nach Formel 18 und 19 in Norm
dxPixel(i) = 2*sqrt(2)*(sqrt(Sigma_X_plural(i) + Sigma_Y_plural(i)+sign(Sigma_X_plural(i)-Sigma_Y_plural(i)).*(sqrt(((Sigma_X_plural(i)-Sigma_Y_plural(i)).^2)+4*(Sigma_XY_plural(i).^2)))));
dyPixel(i) = 2*sqrt(2)*(sqrt(Sigma_X_plural(i) + Sigma_Y_plural(i)-sign(Sigma_X_plural(i)-Sigma_Y_plural(i)).*(sqrt(((Sigma_X_plural(i)-Sigma_Y_plural(i)).^2)+4*(Sigma_XY_plural(i).^2)))));
dx(i,1) = dxPixel(i) * Pixelgroesse; %Umrechnung von Pixel in Mikrometer
dy(i,1) = dyPixel(i) * Pixelgroesse; %Umrechnung von Pixel in Mikrometer
dz(i,1) = zaxe; %Nur um alles am selben Fleck zu haben
%{
imagesc(picture);
axis on
hold on;
plot(ROIoffset(2)+Schwerpunkte_X, ROIoffset(1)+Schwerpunkte_Y,'r+', 'MarkerSize', 5, 'LineWidth', 1);
t=-pi:0.01:pi;
dx=ROIoffset(2)+Schwerpunkte_X(i)+dxPixel(i)*0.5*cos(t);
dy=ROIoffset(1)+Schwerpunkte_Y(i)+dyPixel(i)*0.5*sin(t);
plot(dx,dy,'g')
hold off
%}
end
for j = 1 : length(name)
delta_x(j) = dx(i) * 1.5;
delta_y(j) = dy(i) * 1.5;
new_roi = filtered(Schwerpunkte_X(i) - delta_x(j) : Schwerpunkte_X(i) + delta_x(j), Schwerpunkte_Y(i) - delta_y(j) : Schwerpunkte_Y(i) + delta_y(j));
structROI(i).ROI= new_roi;
new_ROIdiff = [1,1];
new_ROIoffset =[0, 0];
en_density = sum(sum((new_roi)));
[row,column] = size(new_roi);
xv = (1:column);
yv = (1:row).';
first_Moment_X(j,1) = sum(xv .* new_roi, 'all');
Schwerp_X = first_Moment_X(j) / en_density;
Schwerpunkte_X(j) = Schwerp_X;
first_Moment_Y(j,1) = sum(yv .* new_roi, 'all');
Schwerp_Y = first_Moment_Y(j) / en_density;
Schwerpunkte_Y(j) = Schwerp_Y;
sec_Moment_X(j,1) = sum((xv - Schwerp_X).^2 .* new_roi, 'all');
Sigma_X_plural(j) = sec_Moment_X(j)/en_density;
sec_Moment_Y(j,1) = sum((yv - Schwerp_Y).^2 .* new_roi, 'all');
Sigma_Y_plural(j) = sec_Moment_Y(j) / en_density;
sec_Moment_XY(j,1) = sum((xv - Schwerp_X) .* (yv - Schwerp_Y) .* new_roi, 'all');
Sigma_XY_plural(j) = sec_Moment_XY(j) / en_density;
%Berechnung des Strahldurchmessers nach Formel 18 und 19 in Norm
dxPixel(j) = 2*sqrt(2)*(sqrt(Sigma_X_plural(j) + Sigma_Y_plural(j)+sign(Sigma_X_plural(j)-Sigma_Y_plural(j)).*(sqrt(((Sigma_X_plural(j)-Sigma_Y_plural(j)).^2)+4*(Sigma_XY_plural(j).^2)))));
dyPixel(j) = 2*sqrt(2)*(sqrt(Sigma_X_plural(j) + Sigma_Y_plural(j)-sign(Sigma_X_plural(j)-Sigma_Y_plural(j)).*(sqrt(((Sigma_X_plural(j)-Sigma_Y_plural(j)).^2)+4*(Sigma_XY_plural(j).^2)))));
dx(j,1) = dxPixel(j) * Pixelgroesse; %Umrechnung von Pixel in Mikrometer
dy(j,1) = dyPixel(j) * Pixelgroesse; %Umrechnung von Pixel in Mikrometer
dz(j,1) = zaxe; %Nur um alles am selben Fleck zu haben
end
the code above gibes me the mistake message:
Warning: Integer operands are required for colon operator when used as index.
> In Laser_V2_Bersin_Sven (line 85)
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in Laser_V2_Bersin_Sven (line 85)
new_roi = filtered(Schwerpunkte_X(i) - delta_x(j) : Schwerpunkte_X(i) + delta_x(j), Schwerpunkte_Y(i) - delta_y(j) : Schwerpunkte_Y(i) + delta_y(j));
can anyone help me please?
  3 commentaires
the cyclist
the cyclist le 30 Avr 2022
Is this question different from this one you asked an hour ago?
Bersin Tekmen
Bersin Tekmen le 30 Avr 2022
@Torsten The elliptical part, I calculated before. It is the diameter of a laser point. In some cases it is calculated inaccurately so I want to use the calculated diameter as the new region of interest (roi) to start a second and more accurate calculation.

Connectez-vous pour commenter.

Réponse acceptée

dpb
dpb le 30 Avr 2022
Modifié(e) : dpb le 30 Avr 2022
In
new_roi = filtered(Schwerpunkte_X(i) - delta_x(j) : Schwerpunkte_X(i) + delta_x(j), ...
Schwerpunkte_Y(i) - delta_y(j) : Schwerpunkte_Y(i) + delta_y(j));
you're trying to use the expression with colon operator as a subscripting expression into the array filtered but you've defined
delta_x(j) = dx(i) * 1.5;
delta_y(j) = dy(i) * 1.5;
above as fractional values. There is no such thing as addressing an array element with a non-integer expression that this will evaluate to. It's the same as if you had written
new_roi=filtered(3.5:8.5);
which obviously is a syntax error.
Either define your delta variables as integral steps or use some form of rounding(*) in the locations in which you subsequently use them as indices.
(*) floor(), ceil(), round() depending on your desires -- have to ensure stay in range of the array, of course.
  2 commentaires
dpb
dpb le 2 Mai 2022
Glad to help...
If that solved the issue, go ahead and Accept Answer to let others know if nothing else...

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 30 Avr 2022
There is a thorough discussion in the FAQ:
Read it. The following link will also be useful:

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by