The accuracy of pipeline temperature monitoring using the Brillouin Optical Time Domain Analysis system depends on the Brillouin Gain Spectrum in the Brillouin Optical Time Domain Analysis system. The Non-Local Means noise reduction algorithm, due to its ability to use the data patterns available within the two-dimensional measurement data space, has been used to improve the Brillouin Gain Spectrum in the Brillouin Optical Time Domain Analysis system. This paper studies a new Non-Local Means algorithm optimized through the Black Widow Optimization Algorithm, in view of the unreasonable selection of smoothing parameters in other Non-Local Means algorithms. The field test demonstrates that, the new algorithm, when compared to other Non-Local Means methods, excels in preserving the detailed information within the Brillouin Gain Spectrum. It successfully restores the fundamental shape and essential characteristics of the Brillouin Gain Spectrum. Notably, at the 25 km fiber end, it achieves a 3 dB higher Signal-to-Noise Ratio compared to other Non-Local Means noise reduction algorithms. Furthermore, the Brillouin Gain Spectrum values exhibit increases of 9.4% in Root Mean Square Error, 12.5% in Sum of Squares Error, and 10% in Full Width at Half Maximum. The improved method has a better denoising effect and broad application prospects in pipeline safety.