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

3 vues (au cours des 30 derniers jours)
I used ginput(2) to get initial and goal configuration for path planner, however, when i want to check their feasibility with respect to map and the obstacles i got this error: Index in position 1 is invalid. Array indices must be positive integers or logical values.
map_original = imread('env5.jpg');
% Resize map
map_reaized = imresize(map_original,[500 500]);
% Call convert2binary() function to conver Image to binary using thresholding
map_binary = convert2binary(map_reaized);
map = imbinarize(map_binary);
% get the source and destination nodes from inputs
imshow(map);
[p1, p2] = ginput(2);
source = p1';
goal = p2';
% Check feasibility
if ~feasiblePoint(source,map)
error('source lies on an obstacle or outside map');
end
if ~feasiblePoint(goal,map)
error('goal lies on an obstacle or outside map');
end
function [binary] = convert2binary(map_original)
% Following MATLAB function will take a grayscale
% or an RGB image as input and will return a
% binary image as output
[x, y, z] = size(map_original);
% if Read Image is an RGB Image then convert
% it to a Gray Scale Image For an RGB image
% the value of z will be 3 and for a Grayscale
% Image the value of z will be 1
if z==3
map_original = rgb2gray(map_original);
end
% change the class of image
% array from 'unit8' to 'double'
map_original = double(map_original);
% Calculate sum of all the gray level
% pixel's value of the GraySacle Image
sum = 0;
for i = 1:x
for j = 1:y
sum = sum + map_original(i, j);
end
end
% Calculate Threshold value by dividing the
% calculated sum by total number of pixels
% total number of pixels = rows*columns (i.e x*y)
threshold = sum/(x*y);
% Create a image array having same number
% of rows and column as Original image
% with all elements as 0 (Zero).
binary = zeros(x, y);
% iterate over all the pixels of Grayscale
% Image and Assign 1 to binary(i, j), if gray
% level value is >= threshold value
% else assign 0 to binary(i, j) .
for i = 1:x
for j = 1:y
if map_original(i, j) >= threshold
binary(i, j) = 1;
else
binary(i, j)=0;
end
end
end
end
function feasible = feasiblePoint(point,map)
feasible = true;
% check if collission-free spot and inside maps
if ~(point(1)>=1 && point(1)<= size(map,1) && point(2)>= 1 && point(2)<= size(map,2) && map(point(1),point(2)) == 1)
feasible = false;
end

Réponse acceptée

KSSV
KSSV le 7 Déc 2021
When you use ginput to extract the points, you may end up getting decimals and you are trying to index these points; as matlab indices should be possitive integers or logical you are ending up with error. The work around is, round your extracted points to integers. Change this function:
function feasible = feasiblePoint(point,map) % point are decimals
feasible = true;
% check if collission-free spot and inside maps
if ~(point(1)>=1 && point(1)<= size(map,1) && point(2)>= 1 && point(2)<= size(map,2) && map(point(1),point(2)) == 1)
feasible = false;
end
to
function feasible = feasiblePoint(point,map)
point = round(point) ; % <--- point is rounded to avoid error
feasible = true;
% check if collission-free spot and inside maps
if ~(point(1)>=1 && point(1)<= size(map,1) && point(2)>= 1 && point(2)<= size(map,2) && map(point(1),point(2)) == 1)
feasible = false;
end

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by