Get the losing data

3 views (last 30 days)
THUONG NGUYEN VAN
THUONG NGUYEN VAN on 1 May 2013
this program for tracking red color in real time, i set 300 frames, I would like get the centroid of red masker and save them to center(300x2) for the next step. How can I set the center(k,:) = [161,121](the center of image) when I lost data.
Here is the code
redThresh = 0.25; % Threshold for red detection
vidDevice = imaq.VideoDevice('winvideo', 1, 'YUY2_320x240', ... % Acquire input video stream
'ROI', [1 1 320 240], ...
'ReturnedColorSpace', 'rgb');
VidDevice.FrameRate = '30.0000';
vidInfo = imaqhwinfo(vidDevice); % Acquire input video property
hblob = vision.BlobAnalysis('AreaOutputPort', false, ... % Set blob analysis handling
'CentroidOutputPort', true, ...
'BoundingBoxOutputPort', true', ...
'MinimumBlobArea', 400, ...
'MaximumBlobArea', 3000, ...
'MaximumCount', 1);
hshapeinsRedBox = vision.ShapeInserter('BorderColor', 'Custom', ... % Set Red box handling
'CustomBorderColor', [1 0 0], ...
'Fill', true, ...
'FillColor', 'Custom', ...
'CustomFillColor', [1 0 0], ...
'Opacity', 0.4);
htextins = vision.TextInserter('Text', 'Number of Red Object: %2d', ... % Set text for number of blobs
'Location', [7 2], ...
'Color', [1 0 0], ... // red color
'FontSize', 12);
htextinsCent = vision.TextInserter('Text', '+ X:%4d, Y:%4d', ... % set text for centroid
'LocationSource', 'Input port', ...
'Color', [1 1 0], ... // yellow color
'FontSize', 14);
hVideoIn = vision.VideoPlayer('Name', 'Final Video', ... % Output video player
'Position', [300 300 vidInfo.MaxWidth+20 vidInfo.MaxHeight+30]);
nFrame = 0; % Frame number initialization
center = [0 0];
k = 0;
A = [0 0];
%%Processing Loop
while(nFrame < 300);
rgbFrame = step(vidDevice); % Acquire single frame
rgbFrame = flipdim(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,:) = 0; % 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
k = k+1
if isempty(hblob)
center(k+1,:) = [161 121];
else
centX = centroid(object,1); centY = centroid(object,2);
center(k+1,:) = [centX centY];
end
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
nFrame = nFrame+1;
end
%%Clearing Memory
release(hVideoIn); % Release all memory and buffer used
release(vidDevice);
% clear all;
clc;

Answers (1)

Walter Roberson
Walter Roberson on 1 May 2013
I would think that hblob is not going to be empty because it defines a behavior object rather than an image output. The centroid output from "step" might be empty -- but if it is, then length(bbox(:,1)) is going to be 0 and so the "for" loop would not be entered to have the existing isempty() encountered.
In your existing code, you should be assigning to centX and centY when you assign to center. Except, that is, for the fact that you won't get there if there are no blobs.
If the routine can even return an empty blob (e.g., if it is somehow tracking with memory from the previous step), then shouldn't you be looking at the bbox size rather than at hblob ? The bbox would be representing empty if its third or fourth element are 0, not if isempty() anything.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!