Error using horzcat Dimensions of arrays.
조회 수: 1 (최근 30일)
이전 댓글 표시
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)]);
댓글 수: 0
채택된 답변
Jan
2023년 2월 7일
편집: Jan
2023년 2월 7일
num2str(x) replies a CHAR matrix, if x contains several rows:
a = num2str(rand(2))
You cannot concatenate a CHAR vector and a CHAR matrix horizontally. Maybe you want:
disp('Vehicle positions:');
for k = 1:height(vehicle_positions)
fprintf('%g ', vehicle_positions(k, :));
fprintf('\n');
end
or:
disp(['Vehicle positions: ', mat2str(vehicle_positions)]);
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!