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

20 views (last 30 days)
Hello, I would like to detect white points in real time in ip camera, but i can't manage with this error:
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in Vision (line 40)
vidIn = htextinsCent(vidIn, [centroid(object, 1) centroid(object, 2)], [((centroid(object,1))-6)
((centroid(object,2))-9)]);
...(length(bbox(:,1))) this section has arguments from 1 to 10.
Camera properly connects with Matlab and I can preview images captured from video.
The video is in 1080x1920.
Matlab Code:
thresh = 0.5;
rgbFrame = (zeros(1080,1920));
cam = ipcam('rtsp://camera_ip/video1.main-video','login','password');
hblob = vision.BlobAnalysis('AreaOutputPort', false, ... % Set blob analysis handling
'CentroidOutputPort', true, ...
'BoundingBoxOutputPort', true', ...
'MinimumBlobArea', 600, ...
'MaximumCount', 10);
hshapeinsWhiteBox = vision.ShapeInserter('BorderColor', 'Custom', ...
'CustomBorderColor', [1 0 0]);
htextins = insertText(rgbFrame, [500 500],'Number of White Object(s): %2d', 'FontSize', 18, ...
'BoxColor','red', 'BoxOpacity', 0.4);
htextinsCent = insertText(rgbFrame,[100 100],'text', 'FontSize', 18, ...
'BoxColor','red', 'BoxOpacity', 0.4);
hVideoIn = vision.VideoPlayer('Name', 'Final Video', ... % Output video player
'Position', [100 100 1920+20 1080+20]);
nFrame = 0;
while(nFrame < 100)
rgbFrame = snapshot(cam); % Acquire single frame
bwredFrame = im2bw(rgbFrame(:,:,1),thresh); % obtain the white component from red layer
bwgreenFrame = im2bw(rgbFrame(:,:,2),thresh); % obtain the white component from green layer
bwblueFrame = im2bw(rgbFrame(:,:,3),thresh); % obtain the white component from blue layer
binFrame = bwredFrame & bwgreenFrame & bwblueFrame; % get the common region
binFrame = medfilt2(binFrame, [1 1]);
[centroid, bbox] = step(hblob, binFrame); % Get the centroids and bounding boxes of the blobs
rgbFrame(1:15,1:215,:) = 0; % put a black region on the output stream
vidIn = hshapeinsWhiteBox(rgbFrame, bbox); % Instert the white box
for object = 1:1:(length(bbox(:,1))) % Write the corresponding centroids
vidIn = htextinsCent(vidIn, [centroid(object, 1) centroid(object, 2)], [((centroid(object,1))-6) ((centroid(object,2))-9)]);
end
end
---------------------------------------------------------------------------------------------------
Below is the data I get from the program until the error occurs:
Thanks for the help!

Answers (2)

Cris LaPierre
Cris LaPierre on 22 Sep 2022
Edited: Cris LaPierre on 22 Sep 2022
I believe the issue here is that many of the values of vidIn are 0, which is not a valid index of htextinsCent. In MATLAB, your indices must be logical, or integer values >0. bigger picture, I'm not sure what you are trying to do with your for loop. Did you mean to be calling a function instead of indexing an array?
Also, I think the insertText should be performed after you have captured your snapshot. See this example.
  3 Comments
Bartosz Kuzian
Bartosz Kuzian on 22 Sep 2022
Edited: Bartosz Kuzian on 22 Sep 2022
for object = 1:1:(length(bbox(:,1))) % Write the corresponding centroids
vidIn = step(htextinsCent,vidIn, [centroid(object, 1) centroid(object, 2)], [centroid(object,1)-6 centroid(object,2)-9]);
end
ok, but then:
Error using ss (line 349)
The "A" and "E" matrices must be square.
Error in step (line 113)
sys = ss(a,b,c,d);
Error in Vision (line 72)
vidIn = step(htextinsCent,vidIn, [centroid(object, 1) centroid(object, 2)], [centroid(object,1)-6 centroid(object,2)-9]);
I did't use the "step" function for reason this error and because in the information about the "step" function there is:

Sign in to comment.


Walter Roberson
Walter Roberson on 22 Sep 2022
Centroids are not generally integers.
htextinsCent is not a function: it is an array that results from a call to insertText
  2 Comments
Walter Roberson
Walter Roberson on 22 Sep 2022
htextinsCent is not a function, it is an array resulting from the earlier call to insertText. You are trying to index the array at locations determined by the contents of an image, together with some centroids. The array is RGB (because insertText returns RGB), so the third dimension is 3, but you are trying to index the third dimension at [centroid(object,1)-6 centroid(object,2)-9] which is likely to be greater than 3.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by