Index in position 1 is invalid. Array indices must be positive integers or logical values.
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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!
0 comentarios
Respuestas (2)
Cris LaPierre
el 22 de Sept. de 2022
Editada: Cris LaPierre
el 22 de Sept. de 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 comentarios
Walter Roberson
el 22 de Sept. de 2022
Centroids are not generally integers.
htextinsCent is not a function: it is an array that results from a call to insertText
2 comentarios
Walter Roberson
el 22 de Sept. de 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.
Ver también
Categorías
Más información sobre Image Processing and Computer Vision en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!