Detection Green Balls

6 篇文章 0 订阅

Detection Green Balls

In the following pictures, a green ball is photographed. Detection the green ball is our mission.

.........................................


Collect Data

In this problem, I want to map RGB data into HSV data to utilize the color as features.

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


Thanks for reading, give me a comment if you have any doubts.





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值