1. Optical apparatus, comprising: an image sensor, which is configured to output a signal responsively to an optical image formed on the image sensor; objective optics, which are configured to focus optical radiation from an object so as to form the optical image on the image sensor, while superimposing a characteristic structure on the optical image; and a processor, which is coupled to process the signal so as to generate an electronic image of the object while detecting, responsively to the characteristic structure, a deviation in an alignment of the objective optics relative to the image sensor.