The paper presents a new technique of image enhancement. The described algorithm enables the suppression of noise and contrast enhancement. The interesting feature of this new algorithm is that its iterative use leads to the image segmentation. The algorithm is based on a concept of a virtual particle, which performs a special kind of a random walk — the so called self-avoiding random walk. The transition probabilities of the random walk are determined by the median distribution. The segmentation effect obtained using this method, together with the ability of the algorithm to eliminate impulse and Gaussian noise makes it an interesting preprocessing tool for image analysis.