Aiming at the characteristics of small number and many types of rail surface defect samples, as well as the problems of unstable transfer learning effect and threshold segmentation being easily affected by environmental factors in real scenes, an improved Markov defect segmentation method with zero samples is proposed. Firstly, the collected data is processed by Gabor function to highlight the defect features and reduce the data dimension to obtain the reduced dimension feature map; Kmeans clustering is performed on the processed feature map to reduce the distribution of data and reduce the influence of reflection and shadow, and the clustering result is used as the pre-classification matrix; an improved Markov random field two-layer graph model is constructed and inferred through the reduced dimension feature map and the pre-classification matrix; the local geometric structure of the defect part is analyzed according to the eigenvalues of the classification matrix inferred by the model; finally, the defect area is marked and the defect segmentation is completed. The experimental part uses a self-sampling data set, and the final conclusion is drawn based on the comparative experiment and ablation experiment. The experimental results show that the pixel accuracy, average pixel accuracy, weighted intersection-over-union ratio, and average intersection-over-union ratio of this method on the self-sampling data set are respectively 93.6%、80.7%、89.4%、68.2% , which exceeds the accuracy of other comparative detection algorithms.