Использование Matlab vision.blobAnalysis bbox area-center

Я использую этот код ниже.

  function videoAnalysis()
foregroundDetector = vision.ForegroundDetector('NumGaussians', 3, 'NumTrainingFrames', 50);

[filename, pathname] = uigetfile( ...
    {'*.*',  'All Files (*.*)'}, ...
    'Select a video file');

videoReader = vision.VideoFileReader(fullfile(pathname,filename));
% fRate = videoReader.info.VideoFrameRate;
% disp(fRate);

for i = 1:150
    frame = step(videoReader);
    foreground = step(foregroundDetector, frame);
end
se = strel('square', 3);
filteredForeground = imopen(foreground, se);

blobAnalysis = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...
    'AreaOutputPort', false, 'CentroidOutputPort', false, ...
    'MinimumBlobArea', 350 , 'MaximumBlobArea', 15000, ...
    'MajorAxisLengthOutputPort' , true, 'MaximumCount', 5);

bbox = step(blobAnalysis, filteredForeground);

result = insertShape(frame, 'Rectangle', bbox, 'Color', 'green');

numCars = size(bbox, 1);
result = insertText(result, [10 10], numCars, 'BoxOpacity', 1, 'FontSize', 14);

videoPlayer = vision.VideoPlayer('Name', 'Detected Cars');

videoPlayer.Position(3:4) = [650,400];  % window size: [width, height]
% se = strel('square', 3); % morphological filter for noise removal

i = 0;
while ~isDone(videoReader)
%     if ( mode(i,10) == 0 )
%         disp(i);
%     end
%     i = i + 1;
    frame = step(videoReader); % read the next video frame

    % Detect the foreground in the current video frame
    foreground = step(foregroundDetector, frame);

    % Use morphological opening to remove noise in the foreground
    filteredForeground = imopen(foreground, se);

    % Detect the connected components with the specified minimum area, and
    % compute their bounding boxes
    bbox = step(blobAnalysis, filteredForeground);

    % Draw bounding boxes around the detected cars
    result = insertShape(frame, 'Rectangle', bbox, 'Color', 'blue');
%     imshow(result);
%     

    % Display the number of cars found in the video frame
    numCars = size(bbox, 1);
    result = insertText(result, [10 10], numCars, 'BoxOpacity', 1, 'FontSize', 14);

    %if there is moving object
    if numCars == 0
        result = insertText(result, [100 20], 'You can pass now', 'BoxOpacity', 1, 'FontSize', 14, 'BoxColor', 'green');
    else
        result = insertText(result, [100 20], 'Please wait', 'BoxOpacity', 1, 'FontSize', 14, 'BoxColor', 'red');
    end

    step(videoPlayer, result);  % display
end

release(videoReader); % close

end

В основном код ищет видео и обнаруживает движущийся объект между видеокадрами и рисует ограничивающие рамки вокруг измененных пикселей. Мне нужна информация о центре и площади этих ограничительных рамок. Для этого я предполагаю, что мне нужно изменить параметры AreaOutputPort и CentroidOutputPort в blobAnalysis на TRUE. Но если я это сделаю, Matlab выдаст ошибку: Матрица POSITION должна иметь четыре столбца для формы Rectangle. Как я могу получить эти значения?

Спасибо.


person ekrem777    schedule 07.05.2014    source источник


Ответы (2)


Если вы установите AreaOutputPort и CentroidOutputPort на true, вы получите три выхода вместо одного.

Вместо

bbox = step(blobAnalysis, filteredForeground);

использовать

[areas, centroids, bbox] = step(blobAnalysis, filteredForeground);

То, как вы сейчас это делаете, bbox, в конечном итоге представляет собой одномерный массив, содержащий области, поэтому insertShape выдает ошибку.

person Dima    schedule 07.05.2014

Я нашел новый способ вычисления площади и центроида:

for i=1:size(bbox, 1)
    centroid(i,:) = [ bbox(i,1)+bbox(i,3)/2 ; bbox(i,2)+bbox(i,4)/2 ];
    area(i,1) =  bbox(i,3)*bbox(i,4);
end
person ekrem777    schedule 08.05.2014
comment
Это даст вам центр и площадь ограничительной рамки, которые не совпадают с центроидом и площадью капли. - person Dima; 09.05.2014