The median-type filter is an effective technique to remove salt and pepper (SAP) noise; however, such a mechanism cannot always effectively remove noise and preserve details due to the local diversity singularity and local non-stationarity. In this paper, a two-step SAP removal method was proposed based on the analysis of the median-type filter errors. In the first step, a median-type filter was used to process the image corrupted by SAP noise. Then, in the second step, a novel-designed adaptive nonlocal bilateral filter is used to weaken the error of the median-type filter. By building histograms of median-type filter errors, we found that the error almost obeys Gaussian-Laplacian mixture distribution statistically. Following this, an improved bilateral filter was proposed to utilize the nonlocal feature and bilateral filter to weaken the median-type filter errors. In the proposed filter, (1) the nonlocal strategy is introduced to improve the bilateral filter, and the intensity similarity is measured between image patches instead pixels; (2) a novel norm based on half-quadratic estimation is used to measure the image patch- spatial proximity and intensity similarity, instead of fixed L-1 and L-2 norms; (3) besides, the scale parameters, which were used to control the behavior of the half-quadratic norm, were updated based on the local image feature. Experimental results showed that the proposed method performed better compared with the state-of-the-art methods.