Simple SummaryMastitis is one of the most serious diseases in dairy husbandry, and its timely detection is critical for improving the efficiency of treatment and reducing breeding risks. However, the traditional “contact-based” manual detection method is complex and unsuitable for large-scale production practices. In recent years, the rapid development of deep learning technology has brought new possibilities. We present a novel approach for cow mastitis detection based on thermal infrared image segmentation technology. By automatically segmenting the key parts of the cow’s eyes and udders in the thermal infrared image, it becomes possible to determine mastitis based on temperature. The results show that this method can meet the requirements of the timely and accurate detection of cow mastitis in large-scale dairy farms.AbstractThermal infrared technology is utilized for detecting mastitis in cows owing to its non-invasive and efficient characteristics. However, the presence of surrounding regions and obstacles can impede accurate temperature measurement, thereby compromising the effectiveness of dairy mastitis detection. To address these problems, we proposed the CLE-UNet (Centroid Loss Ellipticization UNet) semantic segmentation algorithm. The algorithm consists of three main parts. Firstly, we introduced the efficient channel attention (ECA) mechanism in the feature extraction layer of UNet to improve the segmentation accuracy by focusing on more useful channel features. Secondly, we proposed a new centroid loss function to facilitate the network’s output to be closer to the position of the real label during the training process. Finally, we used a cow’s eye ellipse fitting operation based on the similarity between the shape of the cow’s eye and the ellipse. The results indicated that the CLE-UNet model obtained a mean intersection over union (MIoU) of 89.32% and an average segmentation speed of 0.049 s per frame. Compared to somatic cell count (SCC), this method achieved an accuracy, sensitivity, and F1 value of 86.67%, 82.35%, and 87.5%, respectively, for detecting mastitis in dairy cows. In conclusion, the innovative use of the CLE-UNet algorithm has significantly improved the segmentation accuracy and has proven to be an effective tool for accurately detecting cow mastitis.