Looking to identify a football from an image
55 views (last 30 days)
Show older comments
I currently have a still image of a football about to be kicked. I would like to identify the ball and it's center. 

0 Comments
Answers (2)
Kevin Holly
on 30 Sep 2022
RGB = imread('YourImage.png');
% Convert RGB image to lab space
I = rgb2lab(RGB);
% Apply thresholds
BW = (I(:,:,1) >= 14.5 ) & (I(:,:,1) <= 89.77) & ...
(I(:,:,2) >= 0.12) & (I(:,:,2) <= 12.5) & ...
(I(:,:,3) >= 3.8) & (I(:,:,3) <= 33.0);
% Open mask
se = strel('disk', 13,0);
BW = imopen(BW, se);
% Filter out smaller objects
BW=bwareafilt(BW,[50000 Inf]);
% Find Centroid and Area of object
rp = regionprops(BW,"Centroid","Area")
% Display masked image for verfication purposes
maskedRGBImage = RGB;
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
imshow(maskedRGBImage)
% Add marker on centroid
hold on
scatter(rp.Centroid(1),rp.Centroid(2),'b','filled')
Image Analyst
on 3 Oct 2025
You can use transfer learning to identify footballs in a variety of images with a variety of colors, orientations, sizes, etc. See this answer:
4 Comments
Vicente
on 3 Dec 2025 at 15:12
Edited: Vicente
on 3 Dec 2025 at 16:02
@Image Analyst I am deeply sorry for not responding to you. Your answer was very helpful; I managed to obtain a RAW photo with as little post-processing as possible.
Now, if you'll allow me, I'd like to ask you another question related to the PPT you created about Lab calibration.
I have this RAW photo (converted to RGB so i can show it to you) that i wish to calibrate to Lab:

I have only applied white balance to this photo (no noise removal, color correction, or background correction). And this was the result:

Now i wish to calibrate it to Lab. I dont have the specific XYZ values of the color chips but i do have the Lab values specified by the seller. So, i did a RGB -> Lab direct calibration as you demostrate on your PPT:

What could be causing the strange colors?
Its the noise or maybe because i jump from RGB to Lab directly?
What can i do if i dont have the specified color chip XYZ values, only the Lab ones?
Image Analyst
on 3 Dec 2025 at 22:25
Well the white balancing looks good. Remember, you only want to white balance on something that is truly white or gray, like the white Post-it ® or whatever it is. Or you could use the background if you don't include all the objects in it. The best way to white balance is to do it by the camera. I'm not sure whether that affects the raw image or just the PNG, BMP, etc. images. But you could do a check. Power down/unplug the camera (to reset everything) and take a picture then white balance and take another picture then compare the raw images.
If you white balance by post processing the picture, then what you get is what you get.
Anyway the white balanced image looks pretty good.
In the last image, the color checker chart colors look correct so I'm guessing you did RGB->LAB, then did another transform to see if you can get the inverse transform to see if it looks the same. That is unnecessary. Once you have your LAB image, you're done. You can do the color analysis on the LAB channels directly. There is no need to transform it back to RGB. If you simply want to "standardize" (not calibrate) to some master RGB image, then you can do an RGB to RGB transform.
There could be some reasons for the inverse transform giving weird colors.
(1) Probably the most likely is that at some point in the process you converted to uint8 values instead of double. That could clip some values at 255 instead of letting them be bigger like 280 or 300. Make sure you carry out everything in double.
(2) Another reason could be that you don't really have a linear image, so either when you converted it from raw into MATLAB it did some processing you weren't aware of, like applying some ICC mapping profile that ended up using uint8 and clipping the values.
(3) It could be that the values of the color checker chart are saturated. Take a histogram of the chart and look at the red, green, and blue histograms separately. You should see a series of 24 or 25 humps, not of which if close to 255, or has a hump clipped at 255. If any of the chips is saturated in RGB space then your calibration transform will be all messed up and then after that all bets are off.
(4) Another reason might be that you went directly from RGB to LAB instead of XYZ. If you look at the equations of XYZ you'll see that it closely matches that of RGB so the transforms don't have to "torque" the transform too much to go from RGB to XYZ. Now look at the equations to go from RGB to LAB. There are weird terms like to the one-third power and those really cause "strain" when trying to get RGB to fit them. I found that once I had a fabric that had an L of about 94 (brighter than the 90 of the chart I used to train the transform). When I went directly into LAB it said the L was 108 rather than 94 like it should have. Part of this is because it was out of the training space and so it was extrapolating wildly out there. It wouldn't have been so bad if it was within the gamut of the training chips but it just goes to show you how tortuous and unpredictable the equations can be going directly to LAB. When I went from RGB to XYZ, and then used the analytical formulas to convert XYZ into LAB, the L value was very close to the expected 94. So that's the way I do it now.
If you use the built-in MATLAB functions rgb2lab or rgb2xyz then I'm pretty sure they use a 3x3 transform like the ICC profile. The profile was chosen to be 3x3 to make on-device transforms quick for scanners and printers. If you're doing super accurate color measurement and want it to match gold-standard spectrophotometers, then a 3x3 transform is not good enough. You need more, as detailed in my above attached presentation.
I'm attaching some files that may help. Also, you should probably start your own thread rather than carry on in @Michael Sposato's 3 year old discussion.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!