video = VideoReader('test1.mp4');
background = read(video, 1);
for i = 2:video.NumberOfFrames
background = imlincomb(0.5, background, 0.5, frame);
for i = 1:video.NumberOfFrames
difference = abs(frame - background);
difference = imbinarize(rgb2gray(difference), 0.1);
difference = bwareaopen(difference, 500);
components = bwconncomp(difference);
for j = 1:components.NumObjects
pixels = components.PixelIdxList{j};
[rows, cols] = ind2sub(components.ImageSize, pixels);
bbox = [min(cols), min(rows), max(cols) - min(cols), max(rows) - min(rows)];
if bbox(3) * bbox(4) > 50000
vehicle_count = vehicle_count + 1;
vehicle_positions = [vehicle_positions; bbox(1) + bbox(3) / 2, bbox(2) + bbox(4) / 2];
disp(['Number of vehicles: ', num2str(vehicle_count)]);
disp(['Vehicle positions: ', num2str(vehicle_positions)]);