Abstract The elbow is an important constituent of oil and gas pipeline systems and plays a key role in changing the direction of pipelines. Corrosion defects pose a significant risk to the safe operation of elbows. Magnetic flux leakage (MFL) detection has been developed as a suitable technique for identifying defects in pipelines. To address the distortion of elbow defect signals in the images arising from variations in the liftoff value of the leakage detector, this paper proposed an image identification method based on an improved YOLOv5 network. The differences in defect images are simulated by analyzing the liftoff value of the magnetization unit. A defect image enhancement method of multiscale retinex with color restoration fusion homomorphic filtering (MSRCR-HF) is employed to enhance the features of defective MFL signal images. To further improve the accuracy of the model, the YOLOv5 network is optimized by integrating the convolutional block attention module (CBAM) and the space-to-depth-nonstrided convolution (SPD-Conv) module. The results show that the proposed image enhancement method effectively accentuates the features of defect images. Moreover, the suggested image identification method exhibits superior accuracy in identification. The mean average precision (mAP) values for the original image set and the enhanced image set are 85.0% and 91.4%, respectively. Consequently, the proposed method is shown to be highly viable for the automatic identification of MFL defects in small-diameter pipe elbows.