Error using horzcat Dimensions of arrays.
Show older comments
Hi,
Can anyone please help me to solve this error? Thanks!
clc;
% Load the video
video = VideoReader('test1.mp4');
% Initialize the background model
background = read(video, 1);
for i = 2:video.NumberOfFrames
frame = read(video, i);
background = imlincomb(0.5, background, 0.5, frame);
end
% Initialize variables to store the vehicle count and position
vehicle_count = 0;
vehicle_positions = [];
% Loop through each frame in the video
for i = 1:video.NumberOfFrames
frame = read(video, i);
% Subtract the background
difference = abs(frame - background);
difference = imbinarize(rgb2gray(difference), 0.1);
% Remove small objects
difference = bwareaopen(difference, 500);
% Find connected components
components = bwconncomp(difference);
% Loop through each connected component
for j = 1:components.NumObjects
% Get the pixels of the current component
pixels = components.PixelIdxList{j};
% Calculate the bounding box of the component
[rows, cols] = ind2sub(components.ImageSize, pixels);
bbox = [min(cols), min(rows), max(cols) - min(cols), max(rows) - min(rows)];
% Check if the component is a vehicle
if bbox(3) * bbox(4) > 50000
% Increment the vehicle count
vehicle_count = vehicle_count + 1;
% Store the position of the vehicle
vehicle_positions = [vehicle_positions; bbox(1) + bbox(3) / 2, bbox(2) + bbox(4) / 2];
end
end
end
% Display the results
disp(['Number of vehicles: ', num2str(vehicle_count)]);
disp(['Vehicle positions: ', num2str(vehicle_positions)]);
The error is:
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in traffic_count (line 52)
disp(['Vehicle positions: ', num2str(vehicle_positions)]);
Accepted Answer
More Answers (0)
Categories
Find more on Vehicle Network Toolbox 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!