We propose an extended weighted median filter which removes random-valued impulse noise with preserving detailed structures in a grayscale image. This filter combines the median filter with a square window and that with adaptive ones constructed by using 8-neighborhood minimum spanning trees (MSTs). The former one has superior capability in the impulse noise removal. However, it tends to destroy detailed structures in an image. To cope with this problem, the proposed method calculates a weighted median by using the pixels both in the square window and the adaptive one which fits to a detailed structure in the image. In this paper, the superior performance of the proposed method is verified through the experiments with grayscale natural and artificial images by comparing with some MST-based noise removal methods.