I'm tring to implement matlab code for Kalman filter as object tracking .There are sample images provided with code.But I tried for another images with different dimensions.It makes .error-'Index exceeds matrix dimensions'

4 views (last 30 days)
Testtrackkalman.m
clear all
NoOfImg = 8;
Xmsaved = zeros(2, NoOfImg); Xhsaved = zeros(2, NoOfImg);
for k = 1:NoOfImg [xm, ym] = GetBallPos(k); [xh, yh] = TrackKalman(xm, ym);
hold on
plot(xm, ym, 'r*')
plot(xh, yh, 'bs')
pause(1)
Xmsaved(:, k) = [xm ym]';
Xhsaved(:, k) = [xh yh]';
end
figure hold on plot(Xmsaved(1,:), Xmsaved(2,:), '*') plot(Xhsaved(1,:), Xhsaved(2,:), 's')
Trackkalman.m
if true
% code
end
function [xh, yh] = TrackKalman(xm, ym)
%
%
persistent A H Q R
persistent x P
persistent firstRun
if isempty(firstRun) dt = 1;
A = [ 1 dt 0 0
0 1 0 0
0 0 1 dt
0 0 0 1 ];
H = [ 1 0 0 0
0 0 1 0 ];
Q = 1.0*eye(4);
R = [ 50 0
0 50 ];
x = [0, 0, 0, 0]';
P = 100*eye(4);
firstRun = 1;
end
xp = A*x; Pp = A*P*A' + Q;
K = Pp*H'*inv(H*Pp*H' + R);
z = [xm ym]'; x = xp + K*(z - H*xp); P = Pp - K*H*Pp;
xh = x(1); yh = x(3);
function [xc, yc] = GetBallPos1(index) % % persistent imgBg persistent firstRun
if isempty(firstRun) imgBg = imread('Process1/bg1.tif',1);
firstRun = 1;
end
xc = 0; yc = 0;
imgWork = imread(['Process1/', int2str(index), '.tif']); imshow(imgWork)
fore = imabsdiff(imgWork, imgBg); fore = (fore(:,:,1) > 10) | (fore(:,:,2) > 10) | (fore(:,:,3) > 10);
L = logical(fore); stats = regionprops(L, 'area', 'centroid'); area_vector = [stats.Area]; [tmp, idx] = max(area_vector);
centroid = stats(idx(1)).Centroid;
xc = centroid(1) + 15*randn; yc = centroid(2) + 15*randn;

Accepted Answer

Image Analyst
Image Analyst on 27 Mar 2016
stats is probably empty. What does this show
numBlobs = size(stats)
  4 Comments
Sajith Galgamuwa
Sajith Galgamuwa on 28 Mar 2016
centroid = stats.Centroid;
xc = centroid(1:2:end); yc = centroid(2:2:end);
Again some errors..*
Too many outputs requested. Most likely cause is missing [] around left hand side that
has a comma separated list expansion.
Error in GetBallPos (line 27) centroid = stats.Centroid;
Error in TestTrackKalman (line 9) [xm, ym] = GetBallPos(k);
Image Analyst
Image Analyst on 28 Mar 2016
Look at my code. Then look at what the error says is the most likely cause. Anything strike you? Notice how I used brackets, and you didn't, and the error message says that the most likely cause is that you didn't use them. Knowing that, do you still have a question?

Sign in to comment.

More Answers (3)

Sajith Galgamuwa
Sajith Galgamuwa on 28 Mar 2016
centroid = [stats.centroid];
xc = centroid(1:2:end);
yc = centroid(2:2:end);
I have corrected but,,,,-
Reference to non-existent field 'centroid'.
Error in GetBallPos (line 26)_
centroid = [stats.centroid];
Error in TestTrackKalman (line 9)_
[xm, ym] = GetBallPos(k);

Sajith Galgamuwa
Sajith Galgamuwa on 28 Mar 2016
Edited: Sajith Galgamuwa on 28 Mar 2016
centroid = [stats.Centroid];
xc = centroid(1:2:end);
yc = centroid(2:2:end);
Now that part o.k..Thanks

Sajith Galgamuwa
Sajith Galgamuwa on 28 Mar 2016
But kalman filter part has some problems.
Error using - Matrix dimensions must agree.
Error in TrackKalman (line 37)
x = xp + K*(z - H*xp);
Error in TestTrackKalman (line 10)
[xh, yh] = TrackKalman(xm, ym)

Categories

Find more on Image Processing and Computer Vision in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!