Calculate Green Then we perform an arithmetic operation on the matrices as a whole to try to create one matrix that represents an intensity of green.

justGreen = g - r/2 - b/2;
colorPlanesPlot(r,g,b,justGreen);

close all

Step 3: Isolate Object of Interest

Threshold the image Now we can set a threshold to separate the parts of the image that we consider to be green from the rest.

bw = justGreen > 50;
imshow(bw);

Remove small unwanted objects We can use special functions provided by the Image Processing toolbox to quickly perform common image processing tasks. Here we are using BWAREAOPEN to remove groups of pixels less than 30.

ball1 = bwareaopen(bw, 30);
imshow(ball1);

Step 4: Find center of green object

Now we are using REGIONPROPS to extract the centroid of the group of pixels representing the ball.