How can I properly detect a ball moving in a film capture? I have problem in detecting a ball moving in a flume (image below).


since converting the frames into grayscale format didn't work I converted it into rgb and make it more saturated. problems are:
1- in the first frames which ball is in the right position, light disturbed the ball detection (image subtraction didn't work well)

So I did it without subtraction.

this is my problem in detecting the ball in the first frames. in the rest frames the problem is its shadow and the white dots on the ball. I can't see the exact ball to find it's exact position in each frame (its shadow lead to some uncertainties)

anyway, I'm using matlab. I shared the code below (subtraction is not working well because of light, note that ball moves since water make it move in the flume).

Any Ideas about how can I detect the ball more precisely? handle the light in the first frames? handle the ball's shadow?

thanks in advance.

% the code starts

I=imread('frame3860.png');
I=imadjust(I,stretchlim(I));
I=lensdistort(I,-0.3);
I2=imcrop(I, [263.5 85.5 1468 940]);
I2=imrotate(I2,-1,'bilinear','crop');
I2=rgb2hsv(I2);
I2(:,:,2)=I2(:,:,2)*1.9;
I3=I2(:,:,1)&gt;0.57 &amp; I2(:,:,1)&lt;0.75;
I4=bwareaopen(I3,200);
I4=medfilt2(I4);
I4=imfill(I4,'holes');
str=strel('disk',1);
I5=imdilate(I4,str);
I5=I5-I4;
e = edge(I5, 'canny');
radii = 30:1:70;
h = circle_hough(e, radii, 'same', 'normalise');
[~,maxIndex] = max(h(:)); %find the global maximum of your accumulator array
[i,j,k] = ind2sub(size(h), maxIndex);
radius = radii(k);
center.x = j;
center.y = i;
N = 200;
theta=linspace(0,2*pi,N);
rho=ones(1,N)*radius;
[X,Y] = pol2cart(theta,rho); 
cent(1,:)=[j,i]; % centroid of the ball in each frame


% code finished

If you have any idea for a better ball detection in my situation I will be  grateful.

P.S. I removed the images since I didn't have the reputation needed to post more than 2 images

thanks in advance
First, for removing shadows try to normilize your RGB image prior to converting it to HSV color model. For more information on this regard see :
http://aishack.in/tutorials/normalized-rgb/

In case the ball is always moving, Optical flow would be a good option I guess. Simply calculate optical flow and mark the region with highest flow as the ball.

The other option would be emplying a tracking algorithm, which, I feel MeanShift would be a good option here. However, a tracking algorithm requires you to pinpoint the object of the interest in first few frames. If the object is fixed (it's always the same ball) then tracking would diffinitly work.