I have problem in detecting a ball moving in a flume (image below). figure 1. a sample of a frame with ball moving right to left on some particles in a flume

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) enter image description here 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

I2=imcrop(I, [263.5 85.5 1468 940]);
I3=I2(:,:,1)>0.57 & I2(:,:,1)<0.75;
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;
[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

  • $\begingroup$ Are the backgrounds always composed of such textures with repetitive structures? $\endgroup$ Jan 18, 2017 at 11:09
  • $\begingroup$ the background is without the exposed ball. but when water moves, light will disturb the exact subtraction. $\endgroup$
    – H. Farhadi
    Jan 18, 2017 at 11:23
  • $\begingroup$ can you post the video? $\endgroup$ Jan 14, 2018 at 12:33
  • $\begingroup$ Your second picture, with the ball under a reflection, is going to be difficult no matter what. Even for human eyes, and vision systems with 500 million years of evolutionary pressure, it's difficult. So if you can -- change the lighting (maybe a screen over the tank?) and film some more. $\endgroup$
    – TimWescott
    Nov 29, 2020 at 19:54
  • $\begingroup$ I think a good rules-based approach would be to convert to HSV, segment the image based on the ball's hue, and then reject any object that doesn't have enough connected pixels in a small enough space (in case there's blue stones in there). You should end up with a circle with holes in it (because of the white dots), and maybe some excrescences if there's blue stones in there that look like ball. Then do a best fit for a circle of about the right size to get the actual position of the ball. $\endgroup$
    – TimWescott
    Nov 29, 2020 at 19:58

1 Answer 1


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.

  • $\begingroup$ Thanks for your helpful response. The ball is moving from right to left all the length stochastically. I wanted to measure the ball velocity. so I need to track the position of the ball in each frame. $\endgroup$
    – H. Farhadi
    Jan 18, 2017 at 19:38
  • $\begingroup$ I tried RGB normalization, that removes the light rather than shadow. I hope I could show you the results $\endgroup$
    – H. Farhadi
    Jan 18, 2017 at 20:33
  • $\begingroup$ and something else about meanShift tracking. I can't use a video tracking since I need to properly detect the ball I need to undistort the frames so I can get the right metrics. in video tracking that's not possible $\endgroup$
    – H. Farhadi
    Jan 18, 2017 at 20:47
  • $\begingroup$ An intuitive method occured to me,so you have your ball moving around AND you know its color, so try this, combine optical flow and HSV color based detection togother. $\endgroup$
    – MimSaad
    Jan 19, 2017 at 11:15
  • $\begingroup$ by the way, for uploading images or photos try imgur. $\endgroup$
    – MimSaad
    Jan 19, 2017 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.