Filter löschen
Filter löschen

[Computer Vision] Size of colored Object detected with my algorithm

1 Ansicht (letzte 30 Tage)
Marco Lacitignola
Marco Lacitignola am 30 Jul. 2013
Kommentiert: Image Analyst am 16 Nov. 2016
Hi @ all!
I make a code that detects and tracks colored objects. It works quite fine! Anyway I need also to calculate and to store the size of the detected objects for post elaboration (like area, perimeter etc etc) and unfortunately I don't know how to do that! Can someone help me?
There is my code:
redThresh = 0.8; % Threshold for red component of color detection
greenThresh = 0.4; % Threshold for green component of color detection
blueThresh = 0.4; % Threshold for blue component of color detection
vidDevice = imaq.VideoDevice('winvideo', 1, 'YUY2_640x480', ... % Acquire input video stream
'ROI', [1 1 640 480], ...
'ReturnedColorSpace', 'rgb');
vidInfo = imaqhwinfo(vidDevice); % Acquire input video property
hblob = vision.BlobAnalysis('AreaOutputPort', true, ... % Set blob analysis handling
'CentroidOutputPort', true, ...
'BoundingBoxOutputPort', true', ...
'MinimumBlobArea', 400, ...
'MaximumCount', 50);
hshapeinsWhiteBox = vision.ShapeInserter('BorderColor', 'Custom', ...
'CustomBorderColor', [1 0 0]); % Set white box handling
htextins = vision.TextInserter('Text', 'Number of Colored Object(s): %2d', ... % Set text for number of blobs
'Location', [7 2], ...
'Color', [1 1 1], ... // white color
'Font', 'Courier New', ...
'FontSize', 12);
htextinsCent = vision.TextInserter('Text', '+ X:%6.2f, Y:%6.2f', ... % set text for centroid
'LocationSource', 'Input port', ...
'Color', [0 0 0], ... // black color
'FontSize', 12);
hVideoIn = vision.VideoPlayer('Name', 'Final Video', ... % Output video player
'Position', [100 100 vidInfo.MaxWidth+20 vidInfo.MaxHeight+30]);
nFrame = 0; % Frame number initialization
nFrameMax = 1000; % Max frame number to acquire
k = 1;
%%Processing Loop
while(nFrame < nFrameMax)
rgbFrame = step(vidDevice); % Acquire single frame
rgbFrame = flipdim(rgbFrame,2); % obtain the mirror image for displaying
redFrame = rgbFrame(:,:,1); % Red component of RGB Frame
greenFrame = rgbFrame(:,:,2); % Green component of RGB Frame
blueFrame = rgbFrame(:,:,3); % Blue component of RGB Frame
% Loop of Detection based on Thresholds previously defined
for i = 1:480
for j = 1:640
if redFrame(i,j) >= redThresh
redFrameThres(i,j) = 1;
else redFrameThres(i,j) = 0;
end
if greenFrame(i,j) <= greenThresh
greenFrameThres(i,j) = 1;
else greenFrameThres(i,j) = 0;
end
if blueFrame(i,j) <= blueThresh
blueFrameThres(i,j) = 1;
else blueFrameThres(i,j) = 0;
end
end
end
binFrame = redFrameThres & greenFrameThres & blueFrameThres; % get the common region
binFrameMatrix(:,:,k) = binFrame; % Storage of binFrame
[AREA, CENTROID, BBOX] = step(hblob, binFrame); % Get the area, centroids and bounding boxes of the blobs
rgbFrame(1:15,1:215,:) = 0; % put a black region on the output stream
vidIn = step(hshapeinsWhiteBox, rgbFrame, bbox); % Insert the white box
for object = 1:1:length(bbox(:,1)) % Write the corresponding centroids
vidIn = step(htextinsCent, vidIn, [centroid(object,1) centroid(object,2)], [centroid(object,1)-6 centroid(object,2)-9]);
end
vidIn = step(htextins, vidIn, uint8(length(bbox(:,1)))); % Count the number of blobs
step(hVideoIn, vidIn); % Output video stream
nFrame = nFrame+1;
k = k+1;
end
%%Clearing Memory
release(hVideoIn); % Release all memory and buffer used
release(vidDevice);
clear all;
clc;
  2 Kommentare
JENNIFER JACOB
JENNIFER JACOB am 14 Nov. 2016
how did u find the exact threshold values for these colors?
Image Analyst
Image Analyst am 16 Nov. 2016
Jennifer, see the Color Thresholder on the Apps tab of the tool ribbon. Or else see my File Exchange for color segmentation demos.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Iain
Iain am 30 Jul. 2013
have a look at regionprops.

Kategorien

Mehr zu Image and Video Ground Truth Labeling finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by