In-field counting and localization of cotton status is important for intelligent management of agriculture. However, uneven distribution and severe occlusion in natural field environments poses a huge challenge. To solve aforementioned problem, a joint cotton counting and localization algorithm based on density-guided optimal transport is proposed. Firstly, VGG19 is used as the backbone to extract image features, and a coordinate attention mechanism is introduced to capture the location information in the feature map. To avoid the limitation of Gaussian kernel on dense crops, optimal transport theory is introduced firstly in agricultural intelligence, which can directly establish the transport relationship between the predicted density map and ground-truth point map. A density-guided transport cost function is further proposed to better handle the density variation in cotton images. It is used as a loss function to supervise the density map regression, and finally realize the in-field cotton counting and localization. The proposed algorithm is validated on our constructed in-field cotton dataset with 400 images. The experimental results show that the proposed algorithm achieves lower counting and localization errors. Compared with the state-of-the-arts, the counting error MAE and RMSE decreased by 10.54 and 11.57, respectively, and the Precision and Recall increased by 1.7% and 3.8% , respectively.