1. A defect detecting apparatus comprising: a measuring unit that measures a height of a microstructure formed on each of a plurality of dies on a semiconductor wafer in a vertical direction of each die; a determining unit that determines a first focal position and a second focal position as at least two focal positions different from each other in the height direction based on the measured height; an imaging unit that captures images of the microstructure at the determined first focal position and second focal position, with respect to every division region obtained by dividing each die in plurality; a storage unit that stores therein the images of each division region at the first and second focal positions together with identification information for identifying a position of each division region within each die as a first and a second inspection target image; a model image creating unit that creates an average image as a first and a second model image for every identification information, the average image being obtained by respectively averaging, among the first and second inspection target images, the first and second inspection target images of respective division regions having the corresponding identification information over the dies; and a detecting unit that detects presence or absence of a defect of the microstructure at the first and second focal positions by extracting a difference between the first and second model images and the first and second inspection target images corresponding to the identification information of the first and second model images as a first and a second difference image, respectively.