码迷,mamicode.com
首页 > 其他好文 > 详细

Detecting Cars in a Video of TrDffic

时间:2020-03-28 23:50:41      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:检测   using   pre   mat   represent   ram   msi   show   matlab   

Display Separated Color Channels of RGB Image

imSize = 200;
RGB = reshape(ones(imSize,1)*reshape(jet(imSize),1,imSize*3),[imSize,imSize,3]);
imshow(RGB)
title(‘Original RGB Image‘)

% Separate the three color channels.
[R,G,B] = imsplit(RGB);

% Display a grayscale representation of each color channel.
figure
subplot(1,3,1)
imshow(R)
title(‘Red Channel‘)
subplot(1,3,2)
imshow(G)
title(‘Green Channel‘)
subplot(1,3,3)
imshow(B)
title(‘Blue Channel‘)

% Display a color representation of each color channel. 
% % Create an all-black channel.
allBlack = zeros(size(RGB,1,2),class(RGB));
justR = cat(3,R,allBlack,allBlack);
justG = cat(3,allBlack,G,allBlack);
justB = cat(3,allBlack,allBlack,B);

figure
montage({justR,justG,justB},‘Size‘,[1 3], ...
 "BackgroundColor",‘w‘,"BorderSize",10);
title(‘Color Representation of the Red, Green, and Blue Color Channels‘);

Detecting Cars in a Video of TrDffic

This example uses VideoReader (MATLAB?), implay, and other Image Processing Toolbox functions to detect light-colored cars in a video of traffic.

在处理视频数据时,从视频中选择一个有代表性的帧并在该帧上开发算法。一个图像有许多结构时,在尝试检测感兴趣的对象之前,尽可能地简化图像。

imextendedmax —— This function returns a binary image that identifies regions with intensity values above a specified threshold, called regional maxima. All other objects in the image with pixel values below this threshold become the background.为了消除深色的车,确定图像中这些物体的平均像素值。

You can use the pixel region tool in mplayto view pixel values.

imopen——This function uses morphological processing to remove small objects from a binary image while preserving large objects. When using morphological processing, you must decide on the size and shape of the structuring element used in the operation.

You can use the pixel region tool in mplayto estimate the width of these objects.

% Step 1: Access Video with VideoReader
trafficVid = VideoReader(‘traffic.mj2‘)
% Step 2: Explore Video with IMPLAY
implay(‘traffic.mj2‘);
% Step 3: Develop Your Algorithm
% % 留下阈值高出部分(二值图像)
darkCarValue = 50;
darkCar = rgb2gray(read(trafficVid,71));
noDarkCar = imextendedmax(darkCar, darkCarValue);
imshow(darkCar)
figure, imshow(noDarkCar)
% 留下小对象,删除大对象
sedisk = strel(‘disk‘,2);
noSmallStructures = imopen(noDarkCar, sedisk);
figure
imhist(noSmallStructures)
noSmallStructures = bwareaopen(noSmallStructures, 150);
figure,imshow(noSmallStructures)
% Step 4: Apply the Algorithm to the Video
% % 预先分配一个内存用于存储之前处理过的视频(每一帧)
nframes = trafficVid.NumFrames;
I = read(trafficVid, 1);
taggedCars = zeros([size(I,1) size(I,2) 3 nframes], class(I));
for k = 1 : nframes
    singleFrame = read(trafficVid, k);
    % Convert to grayscale to do morphological processing.
    I = rgb2gray(singleFrame);
    % Remove dark cars.
    noDarkCars = imextendedmax(I, darkCarValue);
    % Remove lane markings and other non-disk shaped structures.
    noSmallStructures = imopen(noDarkCars, sedisk);
    % Remove small structures.
    noSmallStructures = bwareaopen(noSmallStructures, 150);
    % Get the area and centroid of each remaining object in the frame. The
    % object with the largest area is the light-colored car. Create a copy
    % of the original frame and tag the car by changing the centroid pixel
    % value to red.
    taggedCars(:,:,:,k) = singleFrame;
    stats = regionprops(noSmallStructures, {‘Centroid‘,‘Area‘});
    if ~isempty([stats.Area])
        areaArray = [stats.Area];
        [junk,idx] = max(areaArray);
        c = stats(idx).Centroid;
        c = floor(fliplr(c));
        width = 2;
        row = c(1)-width:c(1)+width;
        col = c(2)-width:c(2)+width;
        taggedCars(row,col,1,k) = 255;
        taggedCars(row,col,2,k) = 0;
        taggedCars(row,col,3,k) = 0;
    end
end
frameRate = trafficVid.FrameRate;
implay(taggedCars,frameRate);

Detecting Cars in a Video of TrDffic

标签:检测   using   pre   mat   represent   ram   msi   show   matlab   

原文地址:https://www.cnblogs.com/RSheng16/p/12590046.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!