Как подсчитать количество транспортных средств, пересекающих линию, и отобразить классифицированное количество (2 колес, машины, автобус) в конце видео в результате - PullRequest
0 голосов
/ 23 мая 2019

Для подсчета я пытаюсь получить доступ к центроиду каждого bbox в каждом кадре и проверить, если перпендикулярное расстояние центроида и линии = 0, но я не могу получить доступ к этим центроидам.Также мне нужно отобразить как строку, так и ограничивающую рамку в конечном результате, но она не отображает рамку, она только показывает линию.Код приведен ниже.

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

videoReader = vision.VideoFileReader('visiontraffic.avi');
for i = 1:150
   frame = step(videoReader); % read the next video frame
   foreground = step(foregroundDetector, frame);
 end

 blobAnalysis = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...
     'AreaOutputPort', false, 'CentroidOutputPort', false, ...
     'MinimumBlobArea', 150);

    shapeInserter = 
   vision.ShapeInserter('Shape','Lines','BorderColor','Custom',...
 'CustomBorderColor', uint8([255 0 0]));

  x1=160;
  y1=210;
  x2=600;
  y2=210;
  Line = int32([x1 y1 x2 y2]);

 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
  count=0;

  while ~isDone(videoReader)

     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);

   %equation of line
  coefficients=polyfit([x1, x2], [y1,y2],1);
  m=coefficients(1);
  b=coefficients(2);

   %Access Centroids of each bbox present in the frame       
   stats = regionprops(bbox, {'Centroid','Area'});
    if ~isempty([stats.Area])
    areaArray = [stats.Area];
    [junk,idx] = max(areaArray);
    c = stats(idx).Centroid;
    c = floor(fliplr(c));
    distance=(m*(stats(idx).c(1))-(stats(idx).c(2))+b)/(sqrt((m*m)+1));
    if(distance==0)
       count=count+1;
    end      
end 

%Draw a line
result=insertShape(frame,'Line',line,'Color','Black','Linewidth',3);

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

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

step(videoPlayer, result);  % display the results

end

release(videoReader); % close the video file
release(videoReader); % close the video file

Я хочу, чтобы в конечном результате было показано общее количество транспортных средств, пересекающих линию, и их классификация.Также мне нужен код для отображения как ограничительной рамки, так и строки. (В настоящее время отображается только строка).

...