필터 지우기
필터 지우기

Index exceeds the number of array elements in image processing.

조회 수: 1 (최근 30일)
Puru Raj
Puru Raj 2022년 6월 24일
답변: DGM 2022년 6월 24일
The given code below shows an error that index exceeds the number of array elements. And for different video sample it runs for different no. of frames. How to fix this?
% Calculate the diameter.
grayImage = rgb2gray(thisFrame);%to convert image from rgb to gray
Unrecognized function or variable 'thisFrame'.
EdgeFrame = edge(grayImage,"sobel");%to detect edges
Graycrop = imcrop(EdgeFrame,[60 100 150 10]); %To crop the image
t = zeros(2,10);
for i=1:10
r = find(EdgeFrame(:,i));
L = length(r);
t(1,i)=r(1);
t(2,i)=r(L);
end
upper_edge = max(t(1,:));
lower_edge = min(t(2,:));
Diameter(frame) = abs(upper_edge - lower_edge);

답변 (2개)

Glenn
Glenn 2022년 6월 24일
To me it seems like the image 'thisFrame', is not always the same size, can that be correct?
It seems like you could either (1) use the cropped version inside your for-loop, or (2) deal with the variable image size in the initiation of your for-loop.
Possible solutions:
(1)
r = find(Graycrop(:,i));
(2)
dimSize = size(EdgeFrame, 2);
Graycrop = imcrop(EdgeFrame,[60 100 150 dimSize]); %To crop the image
t = zeros(2, dimSize);
for i=1:dimSize
r = find(EdgeFrame(:,i));
% moreover, I think you can add some efficiency here:
t(:,i) = r([1, end]);
end

DGM
DGM 2022년 6월 24일
Obviously, not all columns of the edgemap will contain nonzero elements, so r will often be zero-length. Also, Graycrop isn't used for anything, so I don't know if that's intended.
Either way, this isn't exactly an efficient or robust way to find the diameter of an object. Consider the example:
A = imread('acircle.png');
A = rgb2gray(A);
mk = A>128; % binarize
% using regionprops
S = regionprops(mk,'equivdiameter');
d = S.EquivDiameter
d = 190.8529
% this is the simplified version of your method
h = nnz(any(mk,1)) % object height
h = 190
% or likewise
w = nnz(any(mk,2)) % object width
w = 190

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by