Ship detection and identification play pivotal roles in ensuring navigation safety and facilitating efficient maritime traffic management. Aiming at ship detection in complex environments, which often faces problems such as the dense occlusion of ship targets, low detection accuracy, and variable environmental conditions, in this paper, we propose a ship detection algorithm CSD-YOLO (Context guided block module, Slim-neck, Deformable large kernel attention-You Only Look Once) based on the deformable large kernel attention (D-LKA) mechanism, which was improved based on YOLOv8 to enhance its performance. This approach integrates several innovations to bolster its performance. Initially, the utilization of the Context Guided Block module (CG block) enhanced the c2f module of the backbone network, thereby augmenting the feature extraction capabilities and enabling a more precise capture of the key image information. Subsequently, the introduction of a novel neck architecture and the incorporation of the slim-neck module facilitated more effective feature fusion, thereby enhancing both the accuracy and efficiency of detection. Furthermore, the algorithm incorporates a D-LKA mechanism to dynamically adjust the convolution kernel shape and size, thereby enhancing the model’s adaptability to varying ship target shapes and sizes. To address data scarcity in complex marine environments, the experiments utilized a fused dataset comprising the SeaShips dataset and a proprietary dataset. The experimental results demonstrate that the CSD-YOLO algorithm outperformed the YOLOv8n algorithm across all model evaluation metrics. Specifically, the precision rate (precision) was 91.5%, the recall rate (recall) was 89.5%, and the mean accuracy (mAP) was 91.5%. Compared to the benchmark algorithm, the Recall was improved by 0.7% and the mAP was improved by 0.4%. These results indicate that the CSD-YOLO algorithm can effectively meet the requirements for ship target recognition and tracking in complex marine environments.