2016-01-24 56 views
1

這是一個來自Arindam Bose的用於跟蹤紅色對象的Matlab示例代碼。 我已經改變了這段代碼來跟蹤視頻流中的對象。 最初是爲了跟蹤來自相機的紅色物體。如何更改對象跟蹤中的顏色

http://www.mathworks.com/matlabcentral/fileexchange/28757-tracking-red-color-objects-using-matlab

不過我也想追蹤與其他顏色,如綠色,黑色,白色等對象了。我正在研究代碼,但無法真正看到在哪裏更改此顏色信息。

也許是這行代碼負責改變顏色?

diffFrame = imsubtract(rgbFrame(:,:,1), rgb2gray(rgbFrame)); % Get red component of the image 

我也改變了treshold,但沒有成功:

redThresh = 0.15; % Threshold for red detection 

但不知道如何改變顏色oject綠色或其他顏色。

感謝您的任何建議。

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
% Program Name : Red Object Detection and Tracking 
% Author : Arindam Bose 
% Version : 1.05 
% Description : How to detect and track red objects in Live Video 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%% Initialization 
redThresh = 0.15; % Threshold for red detection 

%vidDevice = imaq.VideoDevice('winvideo', 1, 'YUY2_640x480', ... % Acquire input video stream 
%'ROI', [1 1 640 480], ... 
%'ReturnedColorSpace', 'rgb'); 

vidInfo = VideoReader('MyVideo.avi'); 
videoResult = VideoWriter('C:\MyProject\Y_Videos\ResultVideo.avi'); % Finales Video mit Koordinaten der roten Blobs 
open(videoResult); % Öffne finales Video 

% vidInfo = readframe(video); % Acquire input video property 

hblob = vision.BlobAnalysis('AreaOutputPort', false, ... % Set blob analysis handling 
'CentroidOutputPort', true, ... 
'BoundingBoxOutputPort', true', ... 
'MinimumBlobArea', 10, ... 
'MaximumBlobArea', 950, ... 
'MaximumCount', 30); 
hshapeinsRedBox = vision.ShapeInserter('BorderColor', 'Custom', ... % Set Red box handling 
'CustomBorderColor', [255 255 255], ... 
'Fill', true, ... 
'FillColor', 'Custom', ... 
'CustomFillColor', [0 0 0], ... 
'Opacity', 0.3); 
htextins = vision.TextInserter('Text', 'Red Objects: %2d', ... % Set text for number of blobs 
'Location', [7 2], ... 
'Color', [255 255 255], ... // red color 
'FontSize', 14); 
htextinsCent = vision.TextInserter('Text', '+ X:%4d, Y:%4d', ... % set text for centroid 
'LocationSource', 'Input port', ... 
'Color', [255 255 255], ... // white color 
'FontSize', 14); 

hVideoIn = vision.VideoPlayer('Name', 'Final Video', ... % Output video player 
'Position', [100 100 610 500]); 
nFrame = 0; % Frame number initialization 
%% Processing Loop 
while(nFrame < 500) 
rgbFrame = readFrame(vidInfo); 
%rgbFrame = step(vidDevice); % Acquire single frame 
%rgbFrame = flip(rgbFrame,2); % obtain the mirror image for displaying 
diffFrame = imsubtract(rgbFrame(:,:,1), rgb2gray(rgbFrame)); % Get red component of the image 
diffFrame = medfilt2(diffFrame, [3 3]); % Filter out the noise by using median filter 
binFrame = im2bw(diffFrame, redThresh); % Convert the image into binary image with the red objects as white 
[centroid, bbox] = step(hblob, binFrame); % Get the centroids and bounding boxes of the blobs 
centroid = uint16(centroid); % Convert the centroids into Integer for further steps 
rgbFrame(1:20,1:165,:) = 100; % put a black region on the output stream 
vidIn = step(hshapeinsRedBox, rgbFrame, bbox); % Instert the red box 

for object = 1:1:length(bbox(:,1)) % Write the corresponding centroids 
centX = centroid(object,1); 
centY = centroid(object,2); 
vidIn = step(htextinsCent, vidIn, [centX centY], [centX-6 centY-9]); 
end 
vidIn = step(htextins, vidIn, uint8(length(bbox(:,1)))); % Count the number of blobs 
step(hVideoIn, vidIn); % Output video stream 

writeVideo(videoResult,vidIn) 

nFrame = nFrame+1; 
end 

%% Clearing Memory 
release(hVideoIn); % Release all memory and buffer used 
%% release(vidDevice); 
%%clear all; 
close(videoResult); 

clc; 

回答

0

你的猜測是正確的。彩色圖像是n x m x 3矩陣,所以img(:,:,1)是紅色通道,img(:,:,2)是綠色的,而img(:,:,3)是藍色的。

+0

謝謝!是否有可能獲得白色物體,如白色的球? – hexer

+0

這不會是一個問題,你只需要將所需的顏色分解爲RGB,然後相應地組合RGB通道。在* white *的特例中,我認爲'rgb2gray'就足夠了。 – flawr

+0

謝謝!它現在正在工作! – hexer