Infrared Thermography and Machine Learning for Mastitis Detection in Dairy Cows: A Pilot Case Study in Egyptian Farms
Simple Summary
Abstract
1. Introduction
2. Materials and Methods
2.1. Animals and Farm Description
2.2. Thermal Imaging
2.3. Data Loading and Preprocessing
2.3.1. Chemical Analysis of TMR
2.3.2. Milk Yield and Sampling
2.3.3. Blood Sampling
2.4. Mastitis Detection Using Machine Learning and Statistical Analysis
2.4.1. Acquisition of Thermograms
2.4.2. Data Augmentation and Balancing
2.4.3. Dataset
2.4.4. CLAHE Contrast Enhancement
2.4.5. Deep Feature Extraction
Transfer Learning with EfficientNetB3
Span and Level Feature Representation
- Level features (Global Average Pooling-GAP) computes the spatial average of each feature map channel, capturing the overall intensity distribution and global texture patterns across the entire image.
- Span features (Global Max Pooling-GMP) captures the maximum activation in each feature channel, highlighting the presence of the most discriminative local features regardless of their spatial location.
Feature Standardization
2.4.6. Machine-Learning Classification
Train/Test Split
Classifiers (Model Training and Evaluation)
Evaluation Metrics
2.5. Statistical Analysis—Logistic Regression
2.5.1. Rationale for Logistic Regression
2.5.2. SCC vs. CMT and ML Prediction: Simple Logistic Regression
2.5.3. SCC Data Cleaning
2.5.4. Outcome Encoding
2.5.5. Model Fitting and Visualization
2.5.6. L1-Penalized Logistic Regression: Multi-Variable Analysis
2.5.7. Variables Analyzed
2.5.8. Handling Complete Separation
2.5.9. Statistical Significance
2.5.10. Visualization
2.6. Model Evaluation Strategy
2.6.1. Internal Validation—Held-Out Test Set
2.6.2. Animal-Level Stratified Group Cross-Validation
2.6.3. Qualitative External Application—Serial Images
3. Results
3.1. Performance of Machine Learning Models
3.2. Exploratory Biomarker Association Analysis
3.3. The Impacts of Different Dietary Factors
4. Discussion
4.1. Interpretation of Machine-Learning Performance
4.2. Biomarker and SCC-Association Findings
4.3. Feeding-System Findings and Farm-Level Confounding
4.4. Limitations and Future Work
5. Conclusions
Supplementary Materials
Author Contributions
Funding
Institutional Review Board Statement
Data Availability Statement
Conflicts of Interest
Abbreviations
References
- Velasco-Bolaños, J.; Ceballes-Serrano, C.C.; Velásquez-Mejía, D.; Riaño-Rojas, J.C.; Giraldo, C.E.; Carmona, J.U.; Ceballos-Márquez, A. Application of udder surface temperature by infrared thermography for diagnosis of subclinical mastitis in Holstein cows located in tropical highlands. J. Dairy Sci. 2021, 104, 10310–10323. [Google Scholar] [CrossRef] [PubMed]
- Franco-Martínez, L.; Muñoz-Prieto, A.; Contreras-Aguilar, M.D.; Želvytė, R.; Monkevičienė, I.; Horvatić, A.; Kuleš, J.; Mrljak, V.; Cerón, J.J.; Escribano, D. Changes in saliva proteins in cows with mastitis: A proteomic approach. Res. Vet. Sci. 2021, 140, 91–99. [Google Scholar] [CrossRef] [PubMed]
- Adkins, P.R.F.; Middleton, J.R. Methods for diagnosing mastitis. Vet. Clin. Food Anim. Pract. 2018, 34, 479–491. [Google Scholar] [CrossRef] [PubMed]
- Zigo, F.; Vasil’, M.; Ondrašovičová, S.; Výrostková, J.; Bujok, J.; Pecka-Kielb, E. Maintaining Optimal Mammary Gland Health and Prevention of Mastitis. Front. Vet. Sci. 2021, 8, 607311. [Google Scholar] [CrossRef] [PubMed]
- El-Sayed, A.; Kamel, M. Bovine Mastitis Prevention and Control in the Post-Antibiotic Era. Trop. Anim. Health Prod. 2021, 53, 236. [Google Scholar] [CrossRef] [PubMed]
- Coşkun, G.; Aytekin, İ. Early Detection of mastitis by using infrared thermography in holstein-friesian dairy cows via classification and regression tree (CART) analysis. Selçuk. J. Agric. Food Sci. 2021, 35, 115–124. [Google Scholar] [CrossRef]
- Swami, S.V.; Patil, R.A.; Gadekar, S.D. Studies on the prevalence of subclinical mastitis in dairy animals. J. Entomol. Zool. Stud. 2017, 5, 1297–1300. [Google Scholar]
- Duarte, C.M.; Freitas, P.P.; Bexiga, R. Technological advances in bovine mastitis diagnosis: An overview. J. Vet. Diagn. Investig. 2015, 27, 665–672. [Google Scholar] [CrossRef] [PubMed]
- Lakshmi, R. Bovine mastitis and its diagnosis. Int. J. Appl. Res. 2016, 2, 213–216. [Google Scholar] [CrossRef]
- Araújo, V.M.; Rili, I.; Gisiger, T.; Gambs, S.; Vasseur, E.; Cellier, M.; Diallo, A.B. AI-Powered Cow Detection in Complex Farm Environments. Smart Agric. Technol. 2025, 10, 100770. [Google Scholar] [CrossRef]
- Khoei, T.T.; Slimane, H.O.; Kaabouch, N. Deep learning: Systematic review, models, challenges, and research directions. Neural Comput. Appl. 2023, 35, 23103–23124. [Google Scholar] [CrossRef]
- Alshehri, M. Blockchain-assisted internet of things framework in smart livestock farming. Internet Things 2023, 22, 100739. [Google Scholar] [CrossRef]
- Kok, Z.H.; Mohamed Shariff, A.R.; Alfatni, M.S.M.; Khairunniza-Bejo, S. Support vector machine in precision agriculture: A review. Comput. Electron. Agric. 2021, 191, 106546. [Google Scholar] [CrossRef]
- Polat, B.; Colak, A.; Cengiz, M.; Yanmaz, L.E.; Oral, H.; Bastan, A.; Kaya, S.; Hayirli, A. Sensitivity and Specificity of Infrared Thermography in Detection of Subclinical Mastitis in Dairy Cows. J. Dairy Sci. 2010, 93, 3525–3532. [Google Scholar] [CrossRef] [PubMed]
- Usamentiaga, R.; Venegas, P.; Guerediaga, J.; Vega, L.; Molleda, J.; Bulnes, F. Infrared Thermography for Temperature Measurement and Non-Destructive Testing. Sensors 2014, 14, 12305–12348. [Google Scholar] [CrossRef] [PubMed]
- Santana, R.C.M.; Guimarães, E.d.; Caracuschanski, F.D.; Brassolatti, L.C.; Silva, M.L.d.; Garcia, A.R.; Pezzopane, J.R.M.; Alves, T.C.; Tholon, P.; Santos, M.V.d.; et al. Machine learning techniques associated with infrared thermography to optimize the diagnosis of bovine subclinical mastitis. Vet. Med. Int. 2025, 2025, 5585458. [Google Scholar] [CrossRef] [PubMed]
- Colak, A.; Polat, B.; Okumus, Z.; Kaya, M.; Yanmaz, L.E.; Hayirli, A. Short Communication: Early Detection of Mastitis Using Infrared Thermography in Dairy Cows. J. Dairy Sci. 2008, 91, 4244–4248. [Google Scholar] [CrossRef] [PubMed]
- Lokhorst, C.; De Mol, R.M.; Kamphuis, C. Invited Review: Big Data in Precision Dairy Farming. Animal 2019, 13, 1519–1528. [Google Scholar] [CrossRef] [PubMed]
- Ebrahimie, E.; Ebrahimi, F.; Ebrahimi, M.; Tomlinson, S.; Petrovski, K.R. A large-scale study of indicators of subclinical mastitis in dairy cattle by attribute weighting analysis of milk composition features: Highlighting the predictive power of lactose and electrical conductivity. J. Dairy Res. 2018, 85, 193–200. [Google Scholar] [PubMed]
- Zhou, X.; Xu, C.; Wang, H. The Early Prediction of Common Disorders in Dairy Cows Monitored by Automatic Systems with Machine Learning Algorithms. Animals 2022, 12, 1251. [Google Scholar] [CrossRef] [PubMed]
- AOAC. Official Methods of Analysis of the Association of Official Analytical Chemists: Official Methods of Analysis of AOAC International, 21st ed.; AOAC: Washington, DC, USA, 2019. [Google Scholar]
- Van Soest, P.J.; Robertson, J.B.; Lewis, B.A. Methods for dietary fibre, neutral detergent fibre and nonstarch polysaccharides in relation to animal nutrition. J. Dairy Sci. 1991, 74, 3583–3597. [Google Scholar] [PubMed]
- NRC. Nutrient Requirements of Dairy Cattle; National Research Council, National Academy Press: Washington, DC, USA, 2001. [Google Scholar]
- ISO 4832; Microbiology of Food and Animal Feeding Stuffs—Horizontal Method for the Enumeration of Coliforms—Colony-Count Technique. The International Organization for Standardization: Geneva, Switzerland, 2006.
- ISO 6888-1; Microbiology of the Food Chain—Horizontal Method for Enumerating Coagulase Positive Staphylococci (Staphylococcus aureus and Other Species) Part 1: Method Using Baird-Parker Agar Medium. ISO: Geneva, Switzerland, 2021.
- Gornall, A.G.; Bardawill, C.J.; David, M.M. Determination of serum proteins by means of the biuret reaction. J. Biol. Chem. 1949, 177, 751–766. [Google Scholar] [CrossRef]
- Doumas, B.T.; Watson, W.A.; Biggs, H.G. Albumin standards and the measurement of serum albumin with bromcresol green. Clin. Chim. Acta 1971, 31, 87–96. [Google Scholar] [CrossRef] [PubMed]
- Trinder, P. Determination of blood glucose using an oxidase-peroxidase system with a non-carcinogenic chromogen. J. Clin. Pathol. 1969, 22, 158–161. [Google Scholar] [PubMed]
- Taggart, A.K.; Kero, J.; Gan, X.; Cai, T.Q.; Cheng, K.; Ippolito, M.; Ren, N.; Kaplan, R.; Wu, K.; Wu, T.J.; et al. (D)-beta-Hydroxybutyrate inhibits adipocyte lipolysis via the nicotinic acid receptor PUMA-G. J. Biol. Chem. 2005, 280, 26649–26652. [Google Scholar] [PubMed]
- Elshafey, B.G.; Elfadadny, A.; Metwally, S.; Saleh, A.G.; Ragab, R.F.; Hamada, R.; Mandour, A.S.; Hendawy, A.O.; Alkazmi, L.; Ogaly, H.A.; et al. Association between biochemical parameters and ultrasonographic measurement for the assessment of hepatic lipidosis in dairy cows. Ital. Ital. J. Anim. Sci. 2023, 22, 136–147. [Google Scholar] [CrossRef]
- Girdauskaitė, A.; Grigė, S.; Sabeckienė, I.; Džermeikaitė, K.; Krištolaitytė, J.; Miknienė, Z.; Antanaitis, R. Associations of Blood Lactate Dehydrogenase Activity with Blood Biochemical and Automated Milk Monitoring Parameters in Early-Lactation Dairy Cows. Agriculture 2026, 16, 502. [Google Scholar] [CrossRef]
- Ježek, J.; Malovrh, T.; Klinkon, M. Serum immunoglobulin (IgG, IgM, IgA) concentration in cows and their calves. Acta Agric. Slov. Supl. 2012, 100, 295–298. [Google Scholar]
- Zuiderveld, K. Contrast limited adaptive histogram equalization. In Graphics Gems IV; Academic Press: Cambridge, MA, USA, 1994; pp. 474–485. [Google Scholar]
- Tan, M.; Le, Q.V. EfficientNet: Rethinking model scaling for convolutional neural networks. In Proceedings of the 36th International Conference on Machine Learning (ICML), Beach, CA, USA, 9–15 June 2019. [Google Scholar]
- Raghu, M.; Zhang, C.; Kleinberg, J.; Bengio, S. Transfusion: Understanding transfer learning for medical imaging. arXiv 2019, arXiv:1902.07208. [Google Scholar]
- Hosmer, D.W.; Lemeshow, S. Applied Logistic Regression, 2nd ed.; John Wiley & Sons: Hoboken, NJ, USA, 2000. [Google Scholar]
- Firth, D. Bias reduction of maximum likelihood estimates. Biometrika 1993, 80, 27–38. [Google Scholar] [CrossRef]
- Tian, F.; Wang, Z.; Yu, S.; Xiong, B.; Wang, S. Clinical mastitis detection by on-line measurements of milk yield, electrical conductivity and deep learn. J. Phys. Conf. Ser. 2020, 1635, 012046. [Google Scholar] [CrossRef]
- Bobbo, T.; Biffani, S.; Taccioli, C.; Penasa, M.; Cassandro, M. Comparison of machine learning methods to predict udder health status based on somatic cell counts in dairy cows. Sci. Rep. 2021, 11, 13642. [Google Scholar] [CrossRef] [PubMed]
- Khan, M.F.; Thorup, V.M.; Luo, Z. Delineating mastitis cases in dairy cows: Development of an IoT-enabled intelligent decision support system for dairy farms. IEEE Trans. Ind. Inform. 2024, 20, 9508–9517. [Google Scholar] [CrossRef]
- Pan, L.; Chen, X.; Han, D.; Li, N.; Chen, D.; Wang, J.; Chen, J.; Huo, X. Machine learning-based clinical mastitis detection in dairy cows using milk electrical conductivity and somatic cell count. Front. Vet. Sci. 2025, 12, 1671186. [Google Scholar] [CrossRef] [PubMed]
- Asogan, A.; Sazali, N.; Veerendra, A.S.; Samylingam, L.; Aslfattahi, N.; Kok, C.K.; Kadirgama, K. A review on the impact of AI-enabled thermal imaging and IoT sensor fusion on early detection of mastitis in dairy cattle. Biosens. Bioelectron. 2026, 28, 100735. [Google Scholar] [CrossRef]
- Bobbo, T.; Matera, R.; Biffani, S.; Gómez, M.; Cimmino, R.; Pedota, G.; Neglia, G. Exploring the sources of variation of electrical conductivity and total and differential somatic cell count in Italian Mediterranean buffaloes. J. Dairy Sci. 2024, 107, 508–515. [Google Scholar] [CrossRef] [PubMed]
- Dreyer, C.B.; Losand, H.; Spiekers, H.; Hummel, J. Influence of fat-to-protein ratio and udder health parameters on the milk urea content of dairy cows. J. Dairy Sci. 2025, 108, 2527–2546. [Google Scholar] [PubMed]
- Zeleke, A.W.; Dimonaco, N.J.; Lawther, K.; Lavery, A.; Ferris, C.; Moorby, J.; Huws, S.A. Reducing crude protein content in the diet of lactating dairy cows improved nitrogen-use-efficiency and reduced N excretion in urine, whilst having no obvious effects on the rumen microbiome. J. Anim. Sci. Biotechnol. 2025, 16, 113. [Google Scholar] [PubMed]
- Grodkowski, G.; Szwaczkowski, T.; Krzysztof Koszela, K.; Wojciech Mueller, W.; Tomaszyk, K.; Ton Baars, T.; Sakowski, T. Early detection of mastitis in cows using the system based on 3D motions detectors. Sci. Rep. 2022, 12, 21215. [Google Scholar] [CrossRef] [PubMed]
- Sepúlveda-Varas, P.; Proudfoot, K.L.; Weary, D.M.; von Keyserlingk, M.A.G. Changes in behaviour of dairy cows with clinical mastitis. Appl. Anim. Behav. Sci. 2016, 175, 8–13. [Google Scholar] [CrossRef]
- Wang, M.; Tan, H.; Li, Y.; Chen, X.; Chen, D.; Wang, J.; Chen, J. Toward five-part differential of leukocytes based on electrical impedances of single cells and neural network. Cytom. Part A 2023, 103, 439–446. [Google Scholar]
- Pacheco, V.M.; de Sousa, R.V.; da Silva Rodrigues, A.V.; de Souza Sardinha, E.J.; Martello, L.S. Thermal Imaging Combined with Predictive Machine Learning Based Model for the Development of Thermal Stress Level Classifiers. Livest. Sci. 2020, 241, 104244. [Google Scholar] [CrossRef]








| Parameter | Value |
|---|---|
| Image format | BMP (Bitmap) |
| Input resolution | 224 × 224 pixels |
| Color space | BGR → LAB (CLAHE on L channel) → BGR |
| Classes | Healthy, Mastitis |
| Original images | 976 (708 healthy, 268 mastitis) |
| Training split before augmentation | 780 images (566 healthy, 214 mastitis) |
| Held-out test split | 196 original images (142 healthy, 54 mastitis) |
| Training set after augmentation | 4708 images (2354 per class) |
| Classifier | Type | Key Parameters |
|---|---|---|
| Logistic Regression | Linear | max_iter = 2000, L2 penalty |
| Random Forest | Ensemble (Bagging) | n_estimators = 300 |
| Extra Trees | Ensemble (Bagging) | n_estimators = 300 |
| AdaBoost | Ensemble (Boosting) | Default (SAMME.R) |
| SVM (RBF) | Kernel-based | kernel = rbf, probability = true |
| KNN | Instance-based | k = 7, Euclidean distance |
| MLP | Neural Network | layers = (256,128), max_iter = 500 |
| Decision Tree | Tree-based | Gini impurity |
| Gaussian Naive Bayes | Probabilistic | Gaussian likelihood |
| LDA | Discriminant analysis | SVD solver |
| Variable | Unit | Category |
|---|---|---|
| SCC | cells/mL | Milk Quality |
| IgG | mg/100 mL | Immunoglobulin |
| IgA | mg/100 mL | Immunoglobulin |
| IgE | IU/mL | Immunoglobulin |
| APP | % | Acute Phase Protein |
| BHBA | mmol/L | Metabolic |
| NEFA | µmol/L | Metabolic |
| LDH | IU/L | Enzyme |
| Glucose | mmol/L | Metabolic |
| Albumin | g/dL | Protein |
| Globulin | g/dL | Protein |
| Total Protein | g/dL | Protein |
| Fat | % | Milk Composition |
| SNF | % | Milk Composition |
| Protein | % | Milk Composition |
| Lactose | % | Milk Composition |
| EC | mS/cm | Electrical Conductivity |
| Density | g/mL | Milk Physical |
| Model | TP | FP | TN | FN |
|---|---|---|---|---|
| Logistic Regression | 38 | 24 | 118 | 16 |
| Random Forest | 26 | 19 | 123 | 28 |
| Extra Trees | 27 | 20 | 122 | 27 |
| AdaBoost | 31 | 25 | 117 | 23 |
| SVM (RBF) | 41 | 19 | 123 | 13 |
| KNN | 35 | 18 | 124 | 19 |
| MLP | 40 | 13 | 129 | 14 |
| Decision Tree | 32 | 36 | 106 | 22 |
| Gaussian NB | 21 | 20 | 122 | 33 |
| LDA | 33 | 31 | 111 | 21 |
| Model | Case | Precision | Recall | F1-Score | Support | Acc | AUC | AUC 95% CI |
|---|---|---|---|---|---|---|---|---|
| Logistic Regression | Healthy | 0.88 | 0.83 | 0.86 | 142 | 0.7959 | 0.8633 | 0.8033–0.9185 |
| Mastitis | 0.61 | 0.70 | 0.66 | 54 | ||||
| Random Forest | Healthy | 0.81 | 0.87 | 0.84 | 142 | 0.7602 | 0.8303 | 0.7734–0.8823 |
| Mastitis | 0.58 | 0.48 | 0.53 | 54 | ||||
| Extra Trees | Healthy | 0.82 | 0.86 | 0.84 | 142 | 0.7602 | 0.8483 | 0.7928–0.8968 |
| Mastitis | 0.57 | 0.50 | 0.53 | 54 | ||||
| AdaBoost | Healthy | 0.84 | 0.82 | 0.83 | 142 | 0.7551 | 0.7784 | 0.7076–0.8459 |
| Mastitis | 0.55 | 0.57 | 0.56 | 54 | ||||
| SVM (RBF) | Healthy | 0.90 | 0.87 | 0.88 | 142 | 0.8367 | 0.8963 | 0.8425–0.9408 |
| Mastitis | 0.68 | 0.76 | 0.72 | 54 | ||||
| KNN | Healthy | 0.87 | 0.87 | 0.87 | 142 | 0.8112 | 0.8439 | 0.7798–0.9018 |
| Mastitis | 0.66 | 0.65 | 0.65 | 54 | ||||
| MLP | Healthy | 0.90 | 0.91 | 0.91 | 142 | 0.8622 | 0.9184 | 0.8740–0.9557 |
| Mastitis | 0.75 | 0.74 | 0.75 | 54 | ||||
| Decision Tree | Healthy | 0.83 | 0.75 | 0.79 | 142 | 0.7041 | 0.6695 | 0.5869–0.7461 |
| Mastitis | 0.47 | 0.59 | 0.52 | 54 | ||||
| Gaussian NB | Healthy | 0.79 | 0.86 | 0.82 | 142 | 0.7296 | 0.8146 | 0.7531–0.8689 |
| Mastitis | 0.51 | 0.39 | 0.44 | 54 | ||||
| LDA | Healthy | 0.84 | 0.78 | 0.81 | 142 | 0.7347 | 0.7518 | 0.6720–0.8253 |
| Mastitis | 0.52 | 0.61 | 0.56 | 54 |
| Biomarker | p-Value | Interpretation |
|---|---|---|
| APP | 0.16788 | Not significant |
| LDH (IU/L) | 0.19468 | Not significant |
| BHBA (mmol/L) | 0.21476 | Not significant |
| SCC (cells/mL) | 0.27087 | Not significant |
| IgE (IU/mL) | 0.53880 | Not significant |
| Globulin (g/dL) | 0.56715 | Not significant |
| Albumin (g/dL) | 0.67739 | Not significant |
| Total protein (g/dL) | 0.73666 | Not significant |
| Glucose (mmol/L) | 0.83311 | Not significant |
| IgG (mg/100 mL) | 0.86934 | Not significant |
| IgA (mg/100 mL) | N/A | Not significant; stable p-value not returned |
| NEFA (µmol/L) | N/A | Not significant; stable p-value not returned |
Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content. |
© 2026 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license.
Share and Cite
Elmasry, A.S.; Elwakeel, E.A.; Allam, A.M.; Attia, M.F.A.; Elmaria, A.T.; Badr, E.E.M.; Sallam, S.M.A. Infrared Thermography and Machine Learning for Mastitis Detection in Dairy Cows: A Pilot Case Study in Egyptian Farms. Vet. Sci. 2026, 13, 640. https://doi.org/10.3390/vetsci13070640
Elmasry AS, Elwakeel EA, Allam AM, Attia MFA, Elmaria AT, Badr EEM, Sallam SMA. Infrared Thermography and Machine Learning for Mastitis Detection in Dairy Cows: A Pilot Case Study in Egyptian Farms. Veterinary Sciences. 2026; 13(7):640. https://doi.org/10.3390/vetsci13070640
Chicago/Turabian StyleElmasry, Aya S., Eman A. Elwakeel, Ali M. Allam, Marwa F. A. Attia, Alaa. T. Elmaria, Elsayed. E. M. Badr, and Sobhy M. A. Sallam. 2026. "Infrared Thermography and Machine Learning for Mastitis Detection in Dairy Cows: A Pilot Case Study in Egyptian Farms" Veterinary Sciences 13, no. 7: 640. https://doi.org/10.3390/vetsci13070640
APA StyleElmasry, A. S., Elwakeel, E. A., Allam, A. M., Attia, M. F. A., Elmaria, A. T., Badr, E. E. M., & Sallam, S. M. A. (2026). Infrared Thermography and Machine Learning for Mastitis Detection in Dairy Cows: A Pilot Case Study in Egyptian Farms. Veterinary Sciences, 13(7), 640. https://doi.org/10.3390/vetsci13070640

