Detection Green Balls
In the following pictures, a green ball is photographed. Detection the green ball is our mission.
.........................................
Collect Data
I use matlab function roipoly to collect HSV data.
There are 19 images as training samples.
The data obtained is (8022,3 ) double array.
Its columns are H,S,V values respectively.
The code to collect HSV data.
close all
imagepath = './train';
Samples = [];
for k=1:19
% Load image
I = imread(sprintf('%s/%03d.png',imagepath,k));
I = rgb2hsv(I);
H = I(:,:,1);
S = I(:,:,2);
V = I(:,:,3);
% Collect samples
disp('');
disp('INTRUCTION: Click along the boundary of the ball. Double-click when you get back to the initial point.')
disp('INTRUCTION: You can maximize the window size of the figure for precise clicks.')
figure(1),
mask = roipoly(I);
figure(2), imshow(mask); title('Mask');
sample_ind = find(mask > 0);
h = H(sample_ind);
s = S(sample_ind);
v = V(sample_ind);
Samples = [Samples; [h s v]]; % append Samples
disp('INTRUCTION: Press any key to continue. (Ctrl+c to exit)')
pause
end
visualization code:
figure,
scatter3(Samples(:,1),Samples(:,2),Samples(:,3),'.');
title('Pixel Color Distribubtion');
xlabel('Red');
ylabel('Green');
zlabel('Blue');
Choose Model.
In this session, I choose Multivariate Gaussian Model.
where are matrices.
so, right now we need to use maximum likelihood estimation to learn the parameters .
Parameters Learning
Likelihood function .
because ,so ,and because ,
such that, , then .
Results
In this example, I chose 0.95 as the threshold. Any pixels whose probability are greater than 0.95 are marked.
At last, the centers of all marked pixels are found by matlab function bwconncomp.
The detection function is displayed as the following:
function [segI, loc] = detectBall(I)
% hsv data
mu = [0.1565,0.6163,0.5992];
sig = [0.0003,-0.0002,-0.0002;-0.0002,0.0191,0.0059;-0.0002,0.0059,0.0024];
thre = 0.95;
I = im2double(I);
I = rgb2hsv(I);
mage = reshape(I, 120*160,3);
GMM = mvnpdf(mage, mu,sig);
GMM = reshape(GMM, 120,160);
mage = GMM > thre;
bw_biggest = false(size(mage));% all zeros
CC = bwconncomp(mage);
numPixels = cellfun(@numel,CC.PixelIdxList);
[biggest,idx] = max(numPixels);
bw_biggest(CC.PixelIdxList{idx}) = true;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Compute the location of the ball center
%
S = regionprops(CC,'Centroid');
loc = S(idx).Centroid; % find center
segI = bw_biggest;
end