replacing invalid samples by average of pixel