Abstract:When dairy cows suffer from mastitis, the temperature difference between their udder surface and eye surface is relatively large. Therefore, the temperature difference between the udder and eye can be used as an indicator for mastitis detection. To address the issues of low accuracy, false detections, and missed detections in existing thermal infrared image-based mastitis detection methods, an improved YOLO v11n-seg method for mastitis detection in dairy cows was proposed, which utilized both thermal infrared and visible light registered images. To achieve more precise segmentation of the cow’s udder and eyes under limited computational resources, targeted improvements were made. Firstly, the ADown convolution module was used to replace some of the ordinary convolution layers in the baseline model (YOLO v11n-seg) for efficient feature extraction, which was beneficial for model deployment and usage in resource-constrained environments. Secondly, the MLCA attention mechanism was introduced at the end of the backbone network, significantly enhancing the feature extraction capability for smallscale objects. Finally, the RepGFPN structure was adopted in the neck network to optimize feature fusion and information transmission, further improving segmentation accuracy. The improved YOLO v11n-seg model achieved an average segmentation accuracy of 90.3% for cow eyes and 97.9% for udders. Compared with the baseline model, the improved YOLO v11n-seg model increased the average segmentation accuracy by 7.1 percentage points and 0.7 percentage points, respectively, while reducing the number of model parameters by 14.3% and the computational cost by 12.5%. The temperature difference between the udder and eye, extracted from the segmentation mask and temperature matrix, was compared with the set temperature difference threshold and verified by the somatic cell count method. The results showed that the accuracy of mastitis detection in dairy cows reached 88.46%. This proved that the proposed method can achieve udder and eye segmentation in dairy cows and can be applied to mastitis detection.