index exceed array (the video file is mp4 of 144p)

7 views (last 30 days)
clc
%%clear screen
video_name ='vish3.mp4'; %%video file
vid = VideoReader(video_name); %%reading the video file from the path
nframes = vid.NumberOfFrames; %defining number of frames
Height = vid.Height; %frame height
Width = vid.Width; %frame width
thr = 10; %threshold value(the mean 8-bit value of the original image)dividing into 2 parts background noise and foreground noise
%% main program kalman introduction
dt=10;
A = [1 0 dt 0;
0 1 0 dt; %%state transistion matrix as per dynamics of moving object
0 0 1 0 ;
0 0 0 1 ;
];
B =[(dt^2)/2, (dt^2)/2, dt, dt]';
u = 4e-3;
%We are observing the X, and Y position. So our observation is: y = [X Y]
H = [1 0 0 0;
0 1 0 0];
%covariance matrix calculations
State_Uncertainty = 10; %state variable are independent so it an diagonal matrix
S = State_Uncertainty * eye(size(A,1));
Meas_Uncertainty = 1;
R = Meas_Uncertainty * eye(size(H,1));
Dyn_Noise_Variance = (0.01)^2;%external force modeled as disturbance
%[(1/2)*(dt^2) (1/2)*(dt^2) dt dt 1 1]'; %Constant Acceleration
% [(1/2)*(dt^2) (1/2)*(dt^2) dt dt]'; %Constant Velocity
Q = [(dt^2)/4 0 (dt^3)/2 0;
0 (dt^2)/4 0 (dt^3)/2; %x and y are independent variable
(dt^3/2) 0 (dt^2) 0;
0 (dt^3)/2 0 (dt^2);
];
Input = []; %%variables
x = [];
Kalman_Output = []; %%initial values
x = [Height/2; Width/2; 0; 0;];
%background extraction
background_frame = BackgroundExt(video_name); %background frame based on the samples
%calculating the noise
moving = zeros(Height,Width,nframes);
labeled_frames = zeros(Height,Width,nframes);
bb=0;
for i=1:nframes-1
current_frame = double(read(vid,i));
moving(:,:,i) = (abs(current_frame(:,:,1) - background_frame(:,:,1)) > thr)...
|(abs(current_frame(:,:,2) - background_frame(:,:,2)) > thr)... %%calculating absolute value and threshold value for tracking noise
|(abs(current_frame(:,:,3) - background_frame(:,:,3)) > thr);
moving(:,:,i) = bwmorph(moving(:,:,i),'erode',2); %%morphing the moving image(eroding the image)
labeled_frames(:,:,i) = bwlabel(moving(:,:,i),4); %Label connected components in 2-D binary image
stats{i} = regionprops(labeled_frames(:,:,i),'basic');% measuring image property
[n_obj,features] = size(stats{i});
area = 0;
if(n_obj ~= 0)
for k=1:n_obj
if(stats{i}(k).Area > area)
id(i) = k;
area = stats{i}(k).Area;
end
end
centroid(:,:,i) = stats{i}(id(i)).Centroid;
else
centroid(:,:,i) = [rand*200 rand*200]; %%random values for centroid calculation
bb = bb+1;
end
i %indicates the frame number
end
for r=1:nframes-1 %pointing error
frames = read(vid,r);
frames = insertShape(frames,'circle',[centroid(1,1,r) centroid(1,2,r) sqrt(stats{r}(id(r)).Area/pi)],'LineWidth',1);
marked_noise(:,:,:,r) = frames;
end
%updating kalman filter
for r=1:nframes-1
frames = read(vid,r);
frames = insertShape(frames,'circle',[centroid(1,1,r) centroid(1,2,r) 4],'LineWidth',2);
if(mod(r,2) == 0) %%tracking the original object
input = [centroid(1,1,r); centroid(1,2,r)];
else
input=[];
end
x = A*x + B*u; %prediction(next state)
S = A*S*A' + Q; %%error covairance
K = S*H'*inv(H*S*H'+R);%correction(gain)
%updating the estimation\\
if(~isempty(input)) %Check if we have an input
x = x + K*(input - H*x);
end
S = (eye(size(S,1)) - K*H)*S; %update the covariance error
Kalman_Output = H*x; %plotting the measurement
frames = insertShape(frames,'circle',[Kalman_Output(1) Kalman_Output(2) 4],'LineWidth',2,'Color','black');
scenario_1(:,:,:,r) = frames;
r
end
implay(scenario_1,15); %%image output
  3 Comments
vishalkumar solanki
vishalkumar solanki on 29 Apr 2019
Unable to perform assignment because the size of the left side is 360-by-480-by-3 and the size of the right side is 360-by-640-by-3.
Error in randommm (line 74)
marked_noise(:,:,:,r) = frames;
Bob Thompson
Bob Thompson on 29 Apr 2019
My first guess is that not all of the images in vid are consistently sized. I would double check that first.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!