Our brains are constantly making assumptions as they perceive the world around us, interpreting and interpolating or "joining the dots" when parts of a scene, sound or other input to our senses are missing. Sometimes the brain gets things wrong. Here's an example, the checker shadow illusion. Squares marked A and B are the same shade of grey, but influenced by the variation of shades around them.
Image attribution: Edward H. Adelson, CC BY-SA 4.0 via Wikimedia Commons. https://creativecommons.org/licenses/by-sa/4.0/
If you don't believe me, cut out the squares in your favourite photo editor and paste the pieces onto a white background. Alternatively, use the eyedropper tool to sample the RGB components.
This, along with visual hallucinations and pareidolia (seeing faces in places) are probably responsible for many ghost sightings.