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

1 次查看(过去 30 天)
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!

回答(2 个)

Cris LaPierre
Cris LaPierre 2022-9-22
编辑:Cris LaPierre 2022-9-22
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 个评论
Bartosz
Bartosz 2022-9-22
编辑:Bartosz 2022-9-22
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:

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2022-9-22
Centroids are not generally integers.
htextinsCent is not a function: it is an array that results from a call to insertText
  2 个评论
Bartosz
Bartosz 2022-9-22
Changing the data type from double to unit16 caused the error to change to:
Index in position 3 exceeds array bounds (must not exceed 3).
Error in Vision (line 72)
vidIn = htextinsCent(vidIn, [centroid(object, 1) centroid(object, 2)], [centroid(object,1)-6 centroid(object,2)-9]);
Walter Roberson
Walter Roberson 2022-9-22
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Image Processing and Computer Vision 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by